阶乘分解

· · 个人记录

阶乘分解:

很容易呀!

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

复杂度 O(nlogn)

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

#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;
}

/*
*/