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 的个数为 Q 个的数?
分析:之前做过类似的一道题,每包含一个5,就有一个0,不过由于N的范围太大,所以用二分来做。
代码:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int N=500000000; int solve(int n) //统计0的个数 { int sum=0; while(n) { sum+=n/5; n=n/5; } return sum; } int he(int n) //二分 { int l,r,mid; l=0,r=N; while(l<=r) { mid=(l+r)/2; if(solve(mid)<n) l=mid+1; else r=mid-1; } if(solve(l)==n) return l; return 0; } int main() { int t,k=1; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); if(he(n)==0) printf("Case %d: impossible\n",k++); else printf("Case %d: %d\n",k++,he(n)); } }
LightOJ - 1138 N - Trailing Zeroes (III)
最新推荐文章于 2020-02-07 22:18:38 发布