NEFU 118 n!后面有多少个0

n!后面有多少个0

Time Limit 1000ms

Memory Limit 65536K

description

从输入中读取一个数n,求出n!中末尾0的个数。
							

input

输入有若干行。第一行上有一个整数m,指明接下来的数字的个数。然后是m行,每一行包含一个确定的正整数n,1<=n<=1000000000。
							

output

对输入行中的每一个数据n,输出一行,其内容是n!中末尾0的个数。
							

sample_input

3
3
100
1024

							

sample_output

0
24
253

 数论基础题,应用结论:

  n!的素因子分解中,素数p的幂为:[n/p]+[n/(p^2)]+[n/(p^3)]+……

此题中,分别求出n!素因子分解中2的幂和5的幂,然后求最小值即可

[C++]
 1 #include<iostream>
 2 
 3 using namespace std;
 4 
 5 long long min(long long a,long long b)
 6 {
 7     return a<b?a:b;
 8 }
 9 
10 int main()
11 {
12     long long m,n;
13     cin>>m;
14     while(m--)
15     {
16         cin>>n;
17         long long num2=0,num5=0,result;
18         result=1;
19         while(n/(result*=2))
20             num2+=n/result;
21         result=1;
22         while(n/(result*=5))
23             num5+=n/result;
24         cout<<min(num2,num5)<<endl;
25     }
26 
27     return 0;
28 }

 

posted @ 2013-05-13 16:51  ~~Snail~~  阅读(184)  评论(0)    收藏  举报