码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-01-05 20:47:34      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:--   cells   lin   idt   val   test   int   sam   not   

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)

标签:--   cells   lin   idt   val   test   int   sam   not   

原文地址:https://www.cnblogs.com/zhulei2/p/8206494.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!