LightOJ - 1138 Trailing Zeroes (III)

来源:互联网 发布:中金公司 知乎 编辑:程序博客网 时间:2024/06/08 14:14

Trailing Zeroes (III)
Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu

Submit Status uDebug

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

Source


5                10               15              25             35                 75                125
 5*1             5*2            5*3            5*5              5*7              5*5*3           5*5*5   (5的个数和下面0的最多个数相等) 
5*2=10    10*2=20    15*2=30   25*4=100   35*4=140    75*4=300    125*8=1000 

 5!          5                                              0 
10!       5、10                                       00
15!       5、10、15                               000
20!       5、10、15、20                      0000
25!       5、10、15、20、25              000000
27!       5、10、15、20、25              000000


这题很考验思维,一个数字包含5就可以出现一个0,除于5就是从1到这个数一共包含多少个5,递归求总和,二分就可以                  


#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <map>
using namespace std;
int binary(int x);
int judge(int x);
int ans;


int main()
{
    int t, x, ncase=1;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &x);
        if(binary(x)==0)
        {
            printf("Case %d: impossible\n",ncase++);
        }
        else
        {
            printf("Case %d: %d\n",ncase++,ans);
        }
    }
    return 0;
}






int binary(int x)
{
    int l=0, r=500000000;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(judge(mid)>x)
        {
            r=mid-1;
        }
        else
        {
            l=mid+1;
        }
    }
    r-=r%5;
    if(judge(r)==x)
    {
        ans=r;
        return 1;
    }
    return 0;
}




int judge(int x)
{
    int cnt=0;
    while(x)
    {
        cnt+=x/5;
        x/=5;
    }
    return cnt;
}


0 0