acwing197

acwing197 阶乘分解

\(N!\)质因子不会超过\(N\),筛\(1\sim N\)质数\(p\),考虑阶乘中包含多少质因子\(p\)

\(p\)的倍数

\[\sum_{i=1}^{\lfloor log_PN\rfloor} \lfloor \frac N{p^i}\rfloor =\sum_{p^k\le N}\lfloor \frac N{p^k}\rfloor \]

#include<cstdio>
using namespace std;
const int N = 1e6 + 10;
int n,num,vis[N],prime[N],ans;
void euler(){
	for(int i = 2;i <= n;++i){
		if(!vis[i]) vis[i] = prime[++num] = i;
		for(int j = 1;j <= num;++j){
			if(vis[i] < prime[j] || i * prime[j] > n) break;
			vis[i * prime[j]] = prime[j];
		}
	}
}
int main(){
	scanf("%d",&n);
	euler();
	for(int i = 1;i <= num;++i){
		printf("%d ",prime[i]);
		int x = n;
		for(;x;x /= prime[i])
			ans += x / prime[i];
		printf("%d\n",ans);
		ans = 0;
	}
}
posted @ 2020-09-24 21:08  INFP  阅读(113)  评论(0编辑  收藏  举报