Trailing Zeroes (III) LightOJ - 1138 二分+找规律

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

 

题意:求出现一个最小的数的阶乘满足末尾有连续q个0。

思路:一个最小的数末尾要出现0一定要是能被5或10整除的,0的连续长度与数组大小正相关,故二分数字得解。

 1 #include<set>
 2 #include<map>
 3 #include<stack>
 4 #include<cmath>
 5 #include<queue>
 6 #include<cstdio>
 7 #include<string>
 8 #include<cstring>
 9 #include<iostream>
10 #include<limits.h>
11 #include<algorithm>
12 #define mem(a,b) memset(a,b,sizeof(a))
13 using namespace std;
14 const int INF=1e9;
15 typedef unsigned long long ll;
16 typedef long long LL;
17 int main()
18 {
19     int T,cases=1;
20     scanf("%d",&T);
21     while(T--)
22     {
23         int q;
24         LL ans=-1;
25         scanf("%d",&q);
26         int l=1,r=q*5,mid;//为什么右端点是q*5,因为q*5>=ans.想一想为什么(hint:刚才那个序列。。。)
27         while(l<=r)
28         {
29             mid=(l+r)/2;
30             int m=mid,wu=5,count=0;
31             while(m>=wu)
32             {
33                 int p=m;
34                 p/=wu;
35                 count+=p;
36                 wu*=5;
37             }
38             if(count==q)
39             {
40                 ans=mid;
41                 break;
42             }
43             else if(count>q)
44                 r=mid-1;
45             else
46                 l=mid+1;
47         }
48         if(ans==-1)
49             printf("Case %d: impossible\n",cases++);
50         else
51             printf("Case %d: %lld\n",cases++,ans/5*5);
52     }
53 }
View Code

代码转自(https://blog.csdn.net/duan_1998/article/details/72566106)

 

posted @ 2018-08-23 21:38  llllrj  阅读(206)  评论(0编辑  收藏  举报