跳到正文

个人记录

阶乘分解

zzr8178541919约 2 分钟
原文
文章信息与版本

文章信息

发布
2020年12月4日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 2 分钟

版本

  1. v1当前

阶乘分解:

很容易呀!

不知道我之前怎么想的。。。。

复杂度O(nlogn) O(nlogn)

直接考虑每个质数和质数的次方所产生的的贡献即可

C++
#include<cmath>
#include<cstring>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#define int long long
#define reg register
using namespace std;
const int maxn=1e6+5;
const int mod=998244353;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0' || ch>'9')
	{
		if(ch=='-')
		f=-1;
		ch=getchar();
	}
	while(ch>='0' && ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
int tot=0;
int check[maxn];
int n;
int prime[maxn];
//int ans[maxn];
void Pre(int maxx)
{
	check[1]=1;
	for(reg int i=2;i<=maxx;i++)
	{
		if(!check[i])
		{
			prime[++tot]=i;
		}
		for(reg int j=1;j<=tot && i*prime[j]<=maxx;j++)
		{
			check[i*prime[j]]=1;
			if(i%prime[j]==0)
			break;
		}
	}
}
struct node
{
	int cnt;
	int val;
}ans[maxn];
int tot1=0;
signed main()
{	
	Pre(1000000);
	n=read();
	for(int i=1;i<=tot && prime[i]<=n;i++)
	{
		tot1++;
		ans[tot1].val=prime[i];
		int x=prime[i];
		int res=0;
		int wns=x;
		while(x<=n)
		{
			res=res+n/x;
			x=x*wns;
		}
		ans[tot1].cnt=res;
	}
	for(int i=1;i<=tot1;i++)
	{
		cout<<ans[i].val<<" "<<ans[i].cnt<<endl;
	}
	return 0;
}

/*
*/
原文
存档评论1 条抓取于