light oj 1138 - Trailing Zeroes (III)(阶乘末尾0)

Posted 风子磊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了light oj 1138 - Trailing Zeroes (III)(阶乘末尾0)相关的知识,希望对你有一定的参考价值。

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

Output for Sample Input

3

1

2

5

Case 1: 5

Case 2: 10

Case 3: impossible

水题一发,直接二分查找。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define LL long long
using namespace std;
LL n;
LL erfen()
{
    LL ans=-1, l=5, r=400000020;//想想r的值为什么这样定(因为此时r!等于10^8+1)
    while(l<=r)
    {
        LL mid=(l+r)/2;
        LL sum=0, s=mid;
        while(s>0)
            sum+=s/5,s/=5;
        if(sum==n)
        {
            r=mid-1;
            ans=mid;
        }
        else if(sum>n)
            r=mid-1;
        else
            l=mid+1;
    }
    return ans;
}
int main()
{
    int T, t=1;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%lld", &n);
        LL s=erfen();
        if(s!=-1)
        printf("Case %d: %lld\n", t++, s);
        else
            printf("Case %d: impossible\n", t++);
    }
}

 

以上是关于light oj 1138 - Trailing Zeroes (III)(阶乘末尾0)的主要内容,如果未能解决你的问题,请参考以下文章

Light OJ 1028 - Trailing Zeroes (I) (数学-因子个数)

Trailing Zeroes (III) LightOJ - 1138(二分)

LightOJ Trailing Zeroes (III) 1138二分搜索+阶乘分解

LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 & 二分

light oj 1058

(light OJ 1005) Rooks dp