n!的素因子分解中的素数p的幂为 [n / p] + [n / p^2] + [n / p ^ 3] +...
n! = 1 * 2 * 3 * ... * p * (p + 1) * ... * (2p) * ... * (p ^ 2) * ....* n
类似筛法,将右边每隔p个数,除去一个p
第一次,一共除去[n / p] 个p
n! / (p ^ (n / p)) = 1 * 2 * 3 * ...* (p - 1) * 1 * (p + 1) * ... * (2p - 1) * 1 * (2p + 1) * ... * (p ^ 2 - 1) * p * (p ^ 2 + 1) * ... * n
观察到,每个p ^ 2个数,仍然为p的倍数,依次往下除,至没有p因子
则n!的素数分解中,p的幂为[n / p] + [n / p^2] + [n / p ^ 3] +...
//nefu 118 求n!结尾有几个0
#include
#include
using namespace std;
typedef long long ll;
int getp(int n,int p)
{
int ans = 0;
ll k = p;
while (k <= n) {
ans += n / k;
k = k * p;
}
return ans;
}
int main()
{
int m,n;
scanf("%d",&m);
for (int i =0 ; i scanf("%d",&n); int a = getp(n,2); int b = getp(n,5); printf("%d\n",min(a,b)); } return 0; }