LightOJ 1138 二分
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 |
#include<bits/stdc++.h> using namespace std; #define ll long long #define mod 1000000007 #define pi (4*atan(1.0)) const int N=1e5+10,M=4e6+10,inf=1e9+10; int a[20]={5,25,125,625,3125,15625,78125,390625,1953125,9765625,48828125,244140625,1220703125}; int check(int x) { int sum=0; for(int i=0;i<13;i++) sum+=x/a[i]; return sum; } int main() { int x,y,z,i,t; int T,cas=1; scanf("%d",&T); while(T--) { scanf("%d",&x); int st=0; int en=500000000; while(st<en) { int mid=(st+en)>>1; if(check(mid)>=x) en=mid; else st=mid+1; } printf("Case %d: ",cas++); if(check(st)==x) printf("%d\n",st); else printf("impossible\n"); } return 0; }