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

核心思想就是,求后缀0的个数;

ll num(ll x)
{
    ll temp = 0;
    while(x)
    {
        temp += x/5;
        x/=5;
    }
    return temp;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll num(ll x)
{
    ll temp = 0;
    while(x)
    {
        temp += x/5;
        x/=5;
    }
    return temp;
}
int main()
{
    int T,cnt = 1;
    scanf("%d",&T);
    while(T--)
    {
        ll n;
        scanf("%lld",&n);
        ll l = 1,r = 1e9,mid,ans = 0;
        while(l <= r)
        {
            mid = (l+r)/2;
            if(num(mid) > n)
                r = mid-1;
            else if(num(mid) < n)
                    l = mid+1;
                else
                {
                    ans = mid;
                    r = mid-1;
                }
        }
        if(ans)
            printf("Case %d: %lld\n",cnt++,ans);
        else
            printf("Case %d: impossible\n",cnt++);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Nothing_227/article/details/82762324