LightOJ-1138-Trailing Zeroes (III)-二分+求N!末尾0
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
题意:
给出一个Q,求出最小的n的阶乘的末尾0(后导)的个数为Q 。
思路:
0是由2*5组成的,也就是相当于一个偶数乘上一个5的倍数,而偶数肯定是跟在5的前面的,所以只需要求出5的倍数(包括5)的个数即可。
注意:
N的范围、求的是最小的N(需要取区间左边)
1 #include<stdio.h> 2 #include<iostream> 3 const int N=500000020; 4 using namespace std; 5 6 int findzero(int x) 7 { 8 int sum=0; 9 while(x) 10 { 11 sum=sum+x/5; 12 x=x/5; 13 } 14 return sum; 15 } 16 17 int erfen(int x) 18 { 19 int L=0,R=N,mid; 20 while(L<=R) 21 { 22 mid=L+((R-L)>>1); 23 if(findzero(mid)<x) 24 { 25 L=mid+1; 26 } 27 else 28 { 29 R=mid-1; 30 } 31 // else if(findzero(mid)==x) 32 // { 33 // return L; 34 // } 35 } 36 if(findzero(L)==x) 37 return L; 38 return 0; 39 } 40 41 42 int main() 43 { 44 int n,tt=1,t,q; 45 scanf("%d",&t); 46 while(t--) 47 { 48 scanf("%d",&q); 49 int ans=erfen(q); 50 if(ans==0) 51 printf("Case %d: impossible\n",tt++); 52 else 53 printf("Case %d: %d\n",tt++,ans); 54 55 } 56 return 0; 57 }