LightOJ 1138 - Trailing Zeroes (III) 二分

题目:http://www.lightoj.com/volume_showproblem.php?problem=1138

题意:找到一个最小的数n,使n!末尾0的个数等于要求的个数,找不到输出impossible

思路:因为2 * 5 = 10,可以发现,某个数n阶乘末尾0的个数等于从1到n内所有数字含有因子5的个数,因此二分枚举n,求含有因子5的个数,找到一个最接近题目要求的n,向下减成5的倍数,然后判断是不是满足题目要求

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
 
typedef long long ll;
int cas;
ll work(ll m)//从1到m内所有数字含有因子5的个数
{
    ll tmp = 0;
    while(m != 0) m /= 5, tmp += m;
    return tmp;
}
int main()
{
    int t;
    ll n, sum;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%lld", &n);
        ll l = 5, r = 5 * n, res;
        while(l <= r)
        {
            ll mid = (l + r) / 2;
            ll tmp = work(mid);
            if(tmp >= n) r = mid - 1, res = mid;
            else l = mid + 1;
        }
        while(res % 5 != 0) res--;
        if(work(res) != n) printf("Case %d: impossible\n", ++cas);
        else printf("Case %d: %lld\n", ++cas, res);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值