Factorial(hdu 1124)

Description

The most important part of a GSM network is so called Base Transceiver Station (BTS). These transceivers form the areas called cells (this term gave the name to the cellular phone) and every phone connects to the BTS with the strongest signal (in a little simplified view). Of course, BTSes need some attention and technicians need to check their function periodically.

ACM technicians faced a very interesting problem recently. Given a set of BTSes to visit, they needed to find the shortest path to visit all of the given points and return back to the central company building. Programmers have spent several months studying this problem but with no results. They were unable to find the solution fast enough. After a long time, one of the programmers found this problem in a conference article. Unfortunately, he found that the problem is so called "Travelling Salesman Problem" and it is very hard to solve. If we have N BTSes to be visited, we can visit them in any order, giving us N! possibilities to examine. The function expressing that number is called factorial and can be computed as a product 1.2.3.4....N. The number is very high even for a relatively small N.

The programmers understood they had no chance to solve the problem. But because they have already received the research grant from the government, they needed to continue with their studies and produce at least some results. So they started to study behaviour of the factorial function.

For example, they defined the function Z. For any positive integer N, Z(N) is the number of zeros at the end of the decimal form of number N!. They noticed that this function never decreases. If we have two numbers N1 < N2, then Z(N1) <= Z(N2). It is because we can never "lose" any trailing zero by multiplying by any positive number. We can only get new and new zeros. The function Z is very interesting, so we need a computer program that can determine its value efficiently.

Input

There is a single positive integer T on the first line of input. It stands for the number of numbers to follow. Then there is T lines, each containing exactly one positive integer number N, 1 <= N <= 1000000000.

Output

For every number N, output a single line containing the single non-negative integer Z(N).

Sample Input

6
3
60
100
1024
23456
8735373

Sample Output

0
14
24
253
5861
2183837

Hint

poj1401

题解:

我们知道0的来源就是2和5的相乘,,那么我们就可以统计2和5 的个数 以小的个数为准,但是很显然2的个数大于5的个数,

注意(4 = 2 * 2 )所以4 是两个2 同样 8是 3 个 2 ,那么我们就可以统计 从 1 到 n 中5 的个数,当然 25 , 50 要统计两遍 ,125 ,250等 要统计 3遍 ,因为 25 = 5 * 5,,50 = 2 *5 *5 ,,125 = 5 * 5 * 5。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<string>
 6 #include<cmath>
 7 #include<iomanip>
 8 #include<map>
 9 #include<stack>
10 #include<vector>
11 #include<queue>
12 #include<set>
13 #include<utility>
14 #include<list>
15 #include<algorithm>
16 #include <ctime>
17 #define max(a,b)   (a>b?a:b)
18 #define min(a,b)   (a<b?a:b)
19 #define swap(a,b)  (a=a+b,b=a-b,a=a-b)
20 #define memset(a,v)  memset(a,v,sizeof(a))
21 #define X (sqrt(5)+1)/2.0
22 #define maxn 320007
23 #define N 200005
24 #define INF 0x3f3f3f3f
25 #define PI acos(-1)
26 #define lowbit(x) (x&(-x))
27 #define read(x) scanf("%d",&x)
28 #define put(x) printf("%d\n",x)
29 #define memset(x,y) memset(x,y,sizeof(x))
30 #define Debug(x) cout<<x<<" "<<endl
31 #define lson i << 1,l,m
32 #define rson i << 1 | 1,m + 1,r
33 #define mod 1000000009
34 #define e  2.718281828459045
35 #define eps 1.0e18
36 #define ll long long
37 using namespace std;
38
39
40 int main()
41 {
42     int t;
43     cin>>t;
44     while(t--)
45     {
46         int n,res=0;
47         cin>>n;
48         while (n)
49         {
50             res+=n/5;
51             n/=5;
52         }
53         cout<<res<<endl;
54     }
55     return 0;
56 }

原文地址:https://www.cnblogs.com/baiyi-destroyer/p/9560678.html

时间: 2024-10-14 06:21:35

Factorial(hdu 1124)的相关文章

hdu 1124 数论

题意:求n!中末尾连续0的个数  其实就是2*5的个数 30! 中有5 10 15 20 25 30  是5的倍数有6个   6=30/5; 6/5=1; 这个1 为25 5  10 15 20  25  30 35 40 45 50 55 60  65 70 75 80  85 90 95 100      100/5=20; 25                     50                    75                     100       20/5=4

hdu 1124

题意:求N!中末尾0的个数 其实就是5的个数 因为2*5=10 肯定n中2的个数比5的个数多#include<iostream> using namespace std; int main() { int n; int t,d; int sum; scanf("%d",&t); while(t--) { sum=0; scanf("%d",&n); sum=n/5;n=sum; while(n/5!=0) { sum+=n/5; n=n/

HDU 1124 Factorial (數論)

http://acm.hdu.edu.cn/showproblem.php?pid=1124 題目好長好長,好可怕,看完腎都萎了,以後肯定活不長,我可不能死在這種小事上,小灰灰我勵志死在少女的超短裙下~~~哈哈,所以我就猥瑣的叫 旁邊的小師妹幫我翻譯了,我是不是很禽獸,嘻嘻~~~ 題目大意呢,就是給一個數,要你求出它的階乘的得到的結果後面有幾個0: 解析: 一看就是簡單數論啦,跟數因子有關,最小素因子而且相乘能得到10的(就是後面有0的)就是2*5啦,因為一個數的階乘2的因子明顯比5的因子要多得

HDU 1124 Factorial (数论)

http://acm.hdu.edu.cn/showproblem.php? pid=1124 題目好長好長,好可怕,看完腎都萎了,以後肯定活不長.我可不能死在這種小事上,小灰灰我勵志死在少女的超短裙下~~~哈哈,所以我就猥瑣的叫 旁邊的小師妹幫我翻譯了,我是不是非常禽獸,嘻嘻~~~ 題目大意呢,就是給一個數,要你求出它的階乘的得到的結果後面有幾個0. 解析: 一看就是簡單數論啦.跟數因子有關.最小素因子并且相乘能得到10的(就是後面有0的)就是2*5啦.因為一個數的階乘2的因子明顯比5的因子要

HDU 1124 Factorial(简单数论)

Factorial Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4237    Accepted Submission(s): 2805 Problem Description The most important part of a GSM network is so called Base Transceiver Station

hdu 1124 Factorial 数论,就是求一个数的阶乘的结果末尾有多少0.

Factorial Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2990    Accepted Submission(s): 1921 Problem Description The most important part of a GSM network is so called Base Transceiver Station

HDU 1124 Factorial (阶乘后缀0)

题意: 给一个数n,返回其阶乘结果后缀有几个0. 思路: 首先将十进制质因数分解得2*5=10.将n!质因数分解,那么分解后,其中应含有min(2个数,5个数)个后缀0. 为何这么说?例如n=15,那么{1 2 3 4 5 6 7 8 9  10 11 12 13 14 15},那么可以产生2的数字有{2,4,6,8,10,12,14},可以产生5的只有{5,10,15},质数中只有2乘以5才能形成10,因为素数只有2是偶数!!!那么min(2个数,5个数)就决定了可以产生10的个数,也就决定了

hdu 1124 OR toj 1065 数论

题目大意:求解n的阶乘末尾0的个数. 分析: 产生0的原因有:(1):2 * 5 (2):乘数末尾有0,如10, 200 不过(2)可以归到(1)中,10 = 2 * 5, 200 = 2 * 2 * 2 * 5 * 5 容易想到将n!分解成质数的乘积以后,只有2 * 5这种组合可以产生末尾的0,且2的个数一定比5的个数多,因为2^n总是比5^n早出现. 所以问题转换为求解n!中有多少个5出现.代码如下: 1 #include <iostream> 2 using namespace std;

HDU 1142 Factorial ( 算术基本定理 + 分解N! )

HDU 1142 Factorial ( 算术基本定理 + 分解N! ) #include <cstdio> int main() { int t, n; scanf( "%d", &t ); while( t-- ) { scanf( "%d", &n ); int cnt = 0; while( n ) { cnt += n/5; n /= 5; } printf( "%d\n", cnt ); } return