数论Trailing Zeroes (III)light0j1138

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
题意:
给你一个数Q,求一个数N,满足N!尾部恰好有Q个0,如果这样的N不存在,输出impossible
题解:
问N!尾部有多少个0,观察规律会发现,其实只需要关注N!中有多少个5就可以了,每个5可以和其他数字相乘得到一个0,25有两个5,100有两个5,所以30!尾部有7个0,分别是5,10,15,20,25,30,其中25有两个5组成,统计小于等于N的整数中5的个数的函数如下

LL calu(LL x)
{
    LL sum = 0,temp = 5;
    while(x/temp)
    {
        sum += x/temp;
        temp *= 5;
    }
    return sum;
}

首先累加5的倍数的数字,这个时候漏掉了25的倍数的数字的一个5,再累加25的倍数的数字,依次累加即可,然后二分N,显然N不超过10^9,因为小于10^9的数字中至少有10^8个10的倍数

#include 
#include 
#include 
#include 
using namespace std;
typedef long long LL;
const LL maxn = 1e9;

LL calu(LL x)
{
    LL sum = 0,temp = 5;
    while(x/temp)
    {
        sum += x/temp;
        temp *= 5;
    }
    return sum;
}

LL mylower_bound(LL x,LL y,LL val)
{
    while(x < y)
    {
        LL mid = (x + y) >> 1;
        LL midval = calu(mid);
        if(midval >= val) y = mid;
        else x = mid + 1;
    }
    if(val == calu(x)) return x;
    else return -1;
}
int main()
{
    LL T,cas = 1;
    scanf("%lld" ,&T);
    while(T--)
    {
        LL q;
        scanf("%lld" ,&q);
        LL n = mylower_bound(5,maxn,q);
        printf("Case %lld: ",cas++);
        if(n == -1) printf("impossible\n");
        else printf("%lld\n", n);
    }
    return 0;
}

你可能感兴趣的:(数论,数论)