码迷,mamicode.com
首页 > 其他好文 > 详细

Lightoj 1090 - Trailing Zeroes (II)

时间:2015-07-05 23:45:33      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

题目连接:

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

题目大意:

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

解题思路:

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

  因为你是Acmer,这都不应该是问题。因为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 }

 

Lightoj 1090 - Trailing Zeroes (II)

标签:

原文地址:http://www.cnblogs.com/alihenaixiao/p/4623179.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!