n的阶乘后面有多少个0?
6的阶乘 = 1*2*3*4*5*6 = 720,720后面有1个0。
Input
一个数N(1 <= N <= 10^9)
Output
输出0的数量
Input示例
5
Output示例
1
这道题看到的时候就是找规律啦。
对任意一个正整数,对其进行因式分解,其末尾的0必定可以分解2x5,
每一个0必定和一个因子5对应,但是一个数的因式分解中因子5不一定
对应着一个0,还需要一个因子2才能实现与之一一对应。对于n!,因式
分解中,2的因子个数要大于5的因子个数,如果存在一个5,那么它必定
对应n!末尾的一个0,所以本题就是求5的因子的个数。
n/p+[n/p^2]+[n/p^3]+[n/p^4]+...
例如:
5,1~9只能分别出一个5的因子,所以是1.
5,1~24,分解出4个5的因子,所以是4.
5,1~25,25之前就4个5的因子,而25能分解出2个5的因子,所以sum=4+25/5^2=6,
...
以此类推。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#include<limits.h>
#define MOD 1000000007
#define fir first
#define sec second
#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)
#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)
#define mes(x, m) memset(x, m, sizeof(x))
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define INF 1e9+7
#define Pi 4.0*atan(1.0)
#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-12;
const int maxn = 35;
using namespace std;
inline int read(){
int x(0),op(1);
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')op=-1,ch=getchar();}
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*op;
}
int main()
{
int n;
n=read();
int sum=0;
int theFive=5;
while(theFive<=n){
sum+=n/theFive;
theFive*=5;
}
cout<<sum<<endl;
return 0;
}