Trailing Zeroes (III)

来源:互联网 发布:pe系统 知乎 编辑:程序博客网 时间:2024/05/20 14:18

Description

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

题意:寻找最小的的自然数,使其阶乘的末尾有n个0;

思路:0是由2×5组成的,又因为含2的因子太多,所以只要判断因子中包含了多少个5;

#include<cstdio>#include<algorithm>#include<cstring>using namespace std;int ans;int solve(int q){ ans=0;while(q){ans+=q/5;q=q/5;}return ans;}int main(){int t,n,k,l,r,h=1;scanf("%d",&t);while(t--){scanf("%d",&n);l=1;r=500000000;while(l<=r){int mid=(l+r)/2;if(solve(mid)>=n)//注意等于号
r=mid-1;elsel=mid+1;}printf("Case %d: ",h++);if(solve(l)==n)printf("%d\n",l);elseprintf("impossible\n");}return 0;}


 

0 0