LightOJ 1138 Trailing Zeroes (二分+阶乘分解+思维)
2016-08-23 20:49
351 查看
1138 - Trailing Zeroes (III)
You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*N. For example, 5!
= 120, 120 contains one zero on the trail.
Each case contains an integer Q (1 ≤ Q ≤ 108) in a line.
PROBLEM SETTER: JANE ALAM JAN
题意:给你一个数字,这个数字代表N!后面有几个0。给出这个数字,计算N的值。
解题思路:
任何质因数都可以写成素数相乘的形式。所以计算一个数的阶乘后面几个0,只需计算这个数包含多少5即可。
因为只有2*5 才会出现 0 又因为2的数量肯定比5的多 所以计算阶乘中5的数量就可以得到该阶乘后有几个0。
50/5=10 10/5=2 所以50!后有10+2=12个0。
可以用二分法,找出这个点。想到用二分这道题也就没什么难度了。
PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
= 120, 120 contains one zero on the trail.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.Each case contains an integer Q (1 ≤ Q ≤ 108) in a line.
Output
For each case, print the case number and N. If no solution is found then print 'impossible'.Sample Input | Output for Sample Input |
3 1 2 5 | Case 1: 5 Case 2: 10 Case 3: impossible |
PROBLEM SETTER: JANE ALAM JAN
题意:给你一个数字,这个数字代表N!后面有几个0。给出这个数字,计算N的值。
解题思路:
任何质因数都可以写成素数相乘的形式。所以计算一个数的阶乘后面几个0,只需计算这个数包含多少5即可。
因为只有2*5 才会出现 0 又因为2的数量肯定比5的多 所以计算阶乘中5的数量就可以得到该阶乘后有几个0。
50/5=10 10/5=2 所以50!后有10+2=12个0。
可以用二分法,找出这个点。想到用二分这道题也就没什么难度了。
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define LL long long LL judge(LL mid) { LL sum=0; while(mid) { sum+=mid/5; mid/=5; } return sum; } int main() { int t; scanf("%d",&t); int flag=1; while(t--) { LL n; scanf("%lld",&n); LL l=1,r=1000000000; LL mid; while(l<=r) { mid=(l+r)>>1; if(judge(mid)>=n) r=mid-1; else l=mid+1; } if(n==judge(l)) printf("Case %d: %lld\n",flag++,l); else printf("Case %d: impossible\n",flag++); } return 0; }
相关文章推荐
- LightOJ Trailing Zeroes (III) 1138【二分搜索+阶乘分解】
- LightOJ - 1138 Trailing Zeroes (III) (二分+阶乘分解)
- lightoj- 1138 - Trailing Zeroes (III)【思维】【二分查找】
- LightOJ 1138 Trailing Zeroes (III)(二分 + 思维)
- LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 & 二分
- LightOJ 1138 - Trailing Zeroes (III)(二分&阶乘末尾0的个数)
- 【二分 && 唯一分解定理】LightOJ - 1138 Trailing Zeroes (III)
- C - Trailing zeroes(二分+阶乘) LightOJ - 1138
- 【lightoj 1138】Trailing Zeroes (III) (二分)
- LightOJ 1138 Trailing Zeroes (III)(二分)
- LightOJ-1138 Trailing Zeroes (III) 唯一分解定理 算n!的某个因数个数
- LightOJ - 1138 - Trailing Zeroes (III) (二分)
- Lightoj 1138 Trailing Zeroes (III) 【二分】
- LightOJ-1138 Trailing Zeroes (III) (二分搜索)
- LightOJ 1138 - Trailing Zeroes (III)【二分】
- lightoj-1138-Trailing Zeroes (III) -二分查找
- LightOJ-1138 Trailing Zeroes (III) (二分搜索)
- Trailing Zeroes (III) (r二分)【LightOj】-1138
- LightOJ 1138 Trailing Zeroes (III) (阶乘末尾0的个数)
- lightoj 1138 - Trailing Zeroes (III)【二分】