Trailing Zeroes (III) (Number Following Zero) LightOJ - 1138

Posted by savedlema on Mon, 07 Oct 2019 10:39:33 +0200

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

Implication: Find out a minimum natural number N, so that the N class contains Q zeros.

Solution: Find out how many 5 in N, because there are one 5 and one 2, then there will be a 0. Since the number of two in a number class must be more than that of five, the number of two in a number class must be higher than that of five in a number class, so the number of two in a number class must be higher than that of five in a number class.

To determine the number of zeros, you only need to determine the number of five. The minimum natural number N is required to be found in the title. Because the data given is very large, the method is adopted here.

Two points will save a lot of time.

 

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int sum(int x)
{
    int ans=0;
    while(x)//Find the number of five in a number
    {
        ans+=x/5;
        x/=5;
    }
    return ans;
}
int main()
{
    int t,o=1;
    int q;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&q);
        int l=0,r=10000000000,mid;
        int ans=0;
        while(l<=r)
        {
            int mid=(l+r)/2;
            int kk=sum(mid);
            if(kk==q)
            {
                ans=mid;
                r=mid-1;
            }
            else if(kk<q)
            {
                l=mid+1;
            }
            else
                r=mid-1;
        }
        if(!ans)
            printf("Case %d: impossible\n",o++);
        else
            printf("Case %d: %d\n",o++,ans);
    }
    return 0;
}