【lightoj 1138】Trailing Zeroes (III) (二分)
2017-08-22 16:58
561 查看
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.
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
3
1
2
5
Sample Output
Case 1: 5
Case 2: 10
Case 3: impossible
还不太懂,先存一下
求一个数n的阶乘中所存在的0的个数:
当0<n<5时,f(n!)=0;
当n>=5时,f(n!)=k+f(k!),其中k=n/5;
代码:
#include<cstdio> #include<cstring> #include<algorithm> #define Max 0x7fffffff using namespace std; int Cal(int x) //计算x后面有几个0 { int ans=0; while(x) { ans+=x/5; x/=5; } return ans; } int main() { int t; int cs=1; scanf("%d",&t); while(t--) { int q; scanf("%d",&q); int left=0,right=Max; int mid; while(left<=right) { mid=(left+right)>>1; if(Cal(mid)>=q) right=mid-1; else left=mid+1; } if(Cal(left)==q) //如果最后一次逼近时满足了条件,则此时left==mid(无论left与right相差1还是相等) printf("Case %d: %d\n",cs++,left); else printf("Case %d: impossible\n",cs++); } return 0; }
相关文章推荐
- lightoj-1138-Trailing Zeroes (III) -二分查找
- LightOJ Trailing Zeroes (III) 1138【二分搜索+阶乘分解】
- lightoj 1138 - Trailing Zeroes (III)(N的阶层后有多少个零+二分)
- lightoj- 1138 - Trailing Zeroes (III)【思维】【二分查找】
- LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 & 二分
- LightOJ-1138 Trailing Zeroes (III) (二分搜索)
- LightOJ 1138 - Trailing Zeroes (III)【二分】
- LightOJ 1138 Trailing Zeroes (III) 二分查找
- lightoj 1138 - Trailing Zeroes (III)【二分】
- LightOJ 1138Trailing Zeroes (III)(二分)
- LightOJ 1138 - Trailing Zeroes (III)(二分&阶乘末尾0的个数)
- 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) 【二分】
- lightoj 1138 - Trailing Zeroes (III)【二分】
- LightOJ-1138 Trailing Zeroes (III) (二分搜索)
- Trailing Zeroes (III)—LightOJ - 1138(二分).