154. Factorial
time limit per test: 0.5 sec.
memory limit per test: 4096 KB
memory limit per test: 4096 KB
input: standard input
output: standard output
output: standard output
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
One number Q written in the input (0<=Q<=10^8).
Output
Write "No solution", if there is no such number N, and N otherwise.
Sample test(s)
Input
2
Output
10
Author: | Andrew V. Lazarev |
Resource: | Saratov Subregional School Team Contest, 2002 |
Date: | Spring, 2002 |
1 #include <iostream> 2 #include <cstdio> 3 4 using namespace std; 5 6 int main() 7 { 8 int q; 9 while(cin>>q) 10 { 11 int i,ans,high=2000000000,low=1,mid; 12 if(q==0) { puts("1"); continue;} 13 while(low<=high) 14 { 15 mid=(low+high)>>1; 16 int k=1,cnt=0; 17 for(i=1;k<=mid;i++) 18 { 19 k=k*5; 20 cnt+=mid/k; 21 } 22 if(cnt>=q) 23 { 24 high=mid-1; 25 } 26 else if(cnt<q) 27 { 28 low=mid+1; 29 } 30 } 31 int tek=max(low,max(high,mid)); 32 int Q=0,k=1; 33 for(i=1;k<=tek;i++) 34 { 35 k=k*5; 36 Q+=tek/k; 37 } 38 if(Q==q) printf("%d ",tek); 39 else puts("No solution"); 40 } 41 return 0; 42 }
---恢复内容结束---