lightoj1138Trailing Zeroes (III) 二分

来源:互联网 发布:国产电视推荐 知乎 编辑:程序博客网 时间:2024/06/03 20:16

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

Hint

题意

n尾部0的个数 找到这个n

题解:

AC代码

#include <cstdio>#include <iostream>#include <queue>#include <map>#include <cstring>#include <algorithm>using namespace std;typedef long long LL;const int N = 5e5+5;int a[N];int l,n,m;/*求一个数阶乘尾部0的个数*/int check(LL k){    int ans = 0;    while (k){        k/=5;        ans+=k;    }    return ans;}int main(){    int t;    scanf("%d",&t);    int kase = 1;    while (t--){        int q;        scanf("%d",&q);        printf("Case %d: ",kase++);        //右边界好大!!        LL l = 0,r = 1e9;        while (l<=r){            LL mid = (l+r)>>1;            if (check(mid)>=q) r = mid-1;            else l = mid+1;        }        if (check(r+1)!=q) printf("impossible\n");        else printf("%lld\n",r+1);    }    return 0;}
原创粉丝点击