E - Trailing Zeroes (III)

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

 

 

 

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
long long note(long long p){          //求n!所得数末尾0的个数
    long long sum=0;
    while(p){
        sum+=p/5;
        p/=5;
    }
    return sum;
}
int main()
{
    int n;
    cin>>n;
    int k=1;
    while(n--){
        long long m,ans=0;
        long long l=1,r= 1000000000000;
        cin>>m;
        while(l<=r){                                              //二分法
            long long mid=(l+r)/2;
            if(note(mid)==m){
                ans=mid;
                r=mid-1;
            }
            else if(note(mid)>m){
                r=mid-1;
            }
            else{
                l=mid+1;
            }
        }
        printf("Case %d: ",k++);
        if(ans)
        printf("%lld\n",ans);
        else        
        {
            printf("impossible\n");
        }
        
    }
}

猜你喜欢

转载自blog.csdn.net/lbs311709000127/article/details/81267755