您的位置:首页 > 大数据 > 人工智能

Light oj 1138 - Trailing Zeroes (III)

2013-05-20 14:47 393 查看
题目: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: