2018年东北农业大学春季校赛 E-wyh的阶乘(求n!的0的个数)

链接:https://www.nowcoder.com/acm/contest/93/E
来源:牛客网

题目描述

这个问题很简单,就是问你n的阶乘末尾有几个0?

输入描述:

输入第一行一个整数T(1<=T<=100),代表测试组数
接下来T行,每行一个数n(1<=n<=10^9)

输出描述:

对于每组测试数据,输出对应答案
示例1

输入

5
1
2
3
4
5

输出

0
0
0
0
1

只有2*5能得到0,而5出现的频率又小于2,即计算5出现的次数。
ans = n/5+n/(5^2)+n/(5^3)……
其中n/5表示不大于n的数中能被5整除的数的个数贡献1个5;n/25再贡献一个;n/125在贡献一个……
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cstdlib>
 6 #include<cmath>
 7 #include<vector>
 8 #include<stack>
 9 #include<queue>
10 #define lson l, m, rt<<1
11 #define rson m+1, r, rt<<1|1
12 #define IO ios::sync_with_stdio(false);cin.tie(0);
13 #define INF 0x3f3f3f3f
14 #define MAXN 500010
15 const int MOD=1e9+7;
16 typedef long long ll;
17 using namespace std;
18 ll n, x, cnt;
19 ll solve(ll x)
20 {
21     ll cnt=0;
22     while(x){
23         cnt += x/5;
24         x /= 5; 
25     }
26     return cnt;
27 }
28 int main()
29 {
30     IO;
31     while(cin >> n){
32         for(int i = 0; i < n; i++){    
33             cin >> x; 
34             cout << solve(x) << endl;
35         }
36     }
37     return 0;
38 } 

 

posted @ 2018-04-05 17:15  Surprisez  阅读(350)  评论(0编辑  收藏  举报