Light oj 1138 - Trailing Zeroes (III)

题目:Light oj 1138 - Trailing Zeroes (III)

思路:二分+暴力+求阶乘中某质因子的幂


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <stack>
using namespace std;
long long n;
long long get_5(long long n)
{
    long long ans=0;
    long long five=5;
    while(n>=five)
    {
        ans+=n/five;
        five*=5;
    }
    return ans;
}
long long solve(long long l,long long r)
{
    long long mid=(l+r)/2;
    long long tmp=get_5(mid);
    if(l==r)
    {
        if(get_5(mid)==n)
            return mid;
        else
            return -1;
    }
    if(tmp<n)
    {
        return solve(mid+1,r);
    }
    else if(tmp>n)
    {
        return solve(l,mid);
    }
    else
        return mid;

}
int main()
{
    int t;
    scanf("%d",&t);
    for(int cases=1;cases<=t;cases++)
    {
        scanf("%lld",&n);
        long long ans=solve(0,1000000000000);
        if(ans==-1)
            printf("Case %d: impossible\n",cases);
        else
        {
            while(get_5(ans-1)==n)
                ans--;
            printf("Case %d: %lld\n",cases,ans);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值