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 |
水题一发,直接二分查找。
#include#include #include #define LL long longusing namespace std;LL n;LL erfen(){ LL ans=-1, l=5, r=400000020;//想想r的值为什么这样定(因为此时r!等于10^8+1) while(l<=r) { LL mid=(l+r)/2; LL sum=0, s=mid; while(s>0) sum+=s/5,s/=5; if(sum==n) { r=mid-1; ans=mid; } else if(sum>n) r=mid-1; else l=mid+1; } return ans;}int main(){ int T, t=1; scanf("%d", &T); while(T--) { scanf("%lld", &n); LL s=erfen(); if(s!=-1) printf("Case %d: %lld\n", t++, s); else printf("Case %d: impossible\n", t++); }}