Lightoj 1090 - Trailing Zeroes (II)

题目连接:

  http://www.lightoj.com/volume_showproblem.php?problem=1090

题目大意:

  给出n,r,p,q四个数字1<=n,r,p,q<=1000000,求出的末尾有几个0?

解题思路:

  是不是一下子懵了,数字好大,复杂度好高,精度怎么办···············,就问你怕不怕?

  其实都是纸老虎啦,因为10的因子只有2和5,所以可以打表保存从1到当前数字相乘的积中分别含有2,5的个数。然后算出中分别含有2,5的个数,取其最小就是结果。(ps:一定不要因为直接统计10的个数方便,而去统计10的个数,两者还是有不同的)。

 1 #include <cmath>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <iostream>
 6 using namespace std;
 7 //复杂度O(1)
 8 const int maxn = 1000010;
 9 struct node
10 {
11     int x, y;
12 };
13 node a[maxn];
14 int main ()
15 {
16     int t, n, r, p, q, l = 1;
17     memset (a, 0, sizeof(a));
18     int x , y;
19     x = y = 0;
20     for (int i=2; i<maxn; i++)
21     {//打表大法好\(^o^)/~
22         int num = i;
23         while (num % 2 == 0)
24         {
25             x ++;
26             num /= 2;
27         }
28         num = i;
29         while (num % 5 == 0)
30         {
31             y ++;
32             num /= 5;
33         }
34         a[i].x = x;
35         a[i].y = y;
36     }
37     scanf ("%d", &t);
38     while (t --)
39     {
40         scanf ("%d %d %d %d", &n, &r, &p, &q);
41         int res = min(a[n].x - a[r].x - a[n-r].x + (a[p].x - a[p-1].x) * q, a[n].y - a[r].y - a[n-r].y + (a[p].y - a[p-1].y) * q);
42         printf ("Case %d: %d\n", l++, res);
43     }
44     return 0;
45 }

 

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值