LightOJ 1138 Trailing Zeroes (III)(二分)

题目链接:
LightOJ 1138 Trailing Zeroes (III)
题意:
求最小的自然数n使得n的阶乘结果以Q个0结尾。如果不存在这样的数输出impossible。
1<= Q <= 1e8.
分析:
首先如果存在解的话,n一定是5的倍数。
5的阶乘后面有1个0;
10的阶乘后面有2个0;
15的阶乘后面有3个0;
20的阶乘后面有4个0;
25的阶乘后面有6个0.。
所以对于一个自然数x,它的阶乘结果后面0的个数num = x / 5 + x / 25 + x / 125 + x / 625 + …。
而且对于任意的x’(x’ > x) 它的阶乘结果后面的0的个数num’ >= num。所以可以二分答案解决了!
最后再检验下二分出来的答案是否正好num == Q即可。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <cassert>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
typedef long long ll;

int T, len, cases = 0;
ll Q, base[20];

inline ll check(ll x)
{
    ll res = 0;
    for(int i = 0; i < len; i++){
        res += x / base[i];
    }
    return res;
}

void init()
{
    base[0] = 5, len = 13;
    for(int i = 1; i < len; i++){
        base[i] = base[i - 1] * 5;
    }
}

int main()
{
    init();
    scanf("%d", &T);
    while(T--){
        scanf("%lld", &Q);
        ll high = base[len - 1], low = 0;
        while( high > low){
            ll mid = (high + low) / 2;
            if(check(mid) >= Q) high = mid;
            else low = mid + 1;
        }
        printf("Case %d: ", ++cases);
        if(check(high) == Q) printf("%lld\n", high);
        else printf("impossible\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值