LightOj 1138 Trailing Zeroes (III)

题目描述:

  假设有一个数n,它的阶乘末尾有Q个零,现在给出Q,问n最小为多少?

解题思路:

  由于数字末尾的零等于min(因子2的个数,因子5的个数),又因为2<5,那么假设有一无限大的数n,n=2^x=5^y,可知x<<y。

所以我们可以直接根据因子5的个数,算阶乘末尾的零的个数。1<=Q<=10^8,所以可以用二分快速求解。

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 #define LL long long
 8 LL find_zeros (LL n)
 9 {
10     LL ans = 0;
11     while (n)
12     {
13         ans += n / 5;
14         n /= 5;
15     }
16     return ans;
17 }
18 int main ()
19 {
20     LL t, n, l = 1;
21     scanf ("%lld", &t);
22     while (t --)
23     {
24         scanf ("%lld", &n);
25         LL low, high, mid;
26         low = 4;
27         high = 500000000;
28         while (low <= high)
29         {
30             mid = (low + high) / 2;
31             LL   num = find_zeros(mid);
32             if (num < n)
33                 low = mid + 1;
34             if (num >= n)//求出来的n要是最小的,所以这里不能直接返回
35                 high = mid - 1;
36         }
37         if (find_zeros(low) == n)
38             printf ("Case %lld: %lld\n", l++, low);
39         else
40             printf ("Case %lld: impossible\n", l++);
41     }
42     return 0;
43 }

 

转载于:https://www.cnblogs.com/alihenaixiao/p/4462297.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值