light oj 1138 - Trailing Zeroes (III)【规律&&二分】
1138 - Trailing Zeroes (III)
Time Limit: 2 second(s) | Memory Limit: 32 MB |
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 |
Output for Sample Input |
3 1 2 5 |
Case 1: 5 Case 2: 10 Case 3: impossible
|
以前在杭电上做过跟这个类似的题,主要考察的知识点一样,只是那个是给出n求0的个数,这个给出0的个数求n
题解:主要还是求数中含有5的个数
#include<stdio.h> #include<string.h> #define LL long long LL fun(LL x) { LL ans=0; while(x) { ans+=x/5; x/=5; } return ans; } int main() { int t,k=1; LL n; scanf("%d",&t); while(t--) { scanf("%lld",&n); LL l=0,r=500000000,mid,ans; while(r>l) { mid=(l+r)/2; if(fun(mid)>=n) { ans=mid; r=mid; } else l=mid+1; } printf("Case %d: ",k++); if(fun(ans)!=n) printf("impossible\n"); else printf("%lld\n",ans); } return 0; }