(二分搜索 数论)(求阶乘里零个数对应的阶乘)light oj -- 1138

链接

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

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;

#define N 0x3f3f3f3f

int Num0(int n)
{
    int sum = 0;

    while(n)
    {
        sum += n/5;
        n /= 5;
    }
    return sum;
}

void Search(int w) ///二分搜索
{
    int L=0, R=N, mid;

    while(L<=R)
    {
        mid = (L+R)>>1;

        if(w <= Num0(mid))
            R = mid - 1;
        else
            L = mid + 1;
    }

    if(Num0(L)==w)
        printf("%d
", L);
    else
        printf("impossible
");
}

int main()
{
    int iCase=1, n, t;
    scanf("%d", &t);
    while( t-- )
    {
        scanf("%d", &n);

        printf("Case %d: ", iCase++);
        Search(n);
    }
    return 0;
}
勿忘初心
原文地址:https://www.cnblogs.com/YY56/p/4747590.html