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;
 3 
 4 int solve( int n )
 5 {
 6     int ans = 0, f = 5;
 7     while ( f <= n )
 8     {
 9         ans += n / f;
10         f = f * 5;        
11     }
12     return ans;
13 }
14 
15 int main ()
16 {
17     int t;
18     cin >> t;
19     while ( t-- )
20     {
21         int n;
22         cin >> n;
23         cout << solve(n) << endl;
24     }
25     return 0;
26 }
原文地址:https://www.cnblogs.com/huoxiayu/p/4424353.html