逆欧拉函数

这里写图片描述

bzoj
不知道为什么bzoj是PE

这里写图片描述

那么由上述式子可以逆推得出,
N=φ(N)(p1/(p1-1))(p2/(p2-1))…(pk/(pk-1))
那我们用dfs,来递归实现。

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstdlib>
#define LL long long
#define M 10000009
using namespace std;
LL N,ans[M],prime[M];
int k,cnt,cnt2;
bool np[M];
void prepare()
{
    np[0]=1,np[1]=1;
    for(int i=2;i<M;i++)
    {
        if(!np[i]) 
        {
            prime[++cnt]=i;
        }
        for(int j=1;j<=cnt&&1ll*i*prime[j]<M;j++)
        {
            np[i*prime[j]]=1;
            if(i%prime[j]==0) break;
        }
    }
}
LL q_mul(LL a,LL b,LL mod)//快速加 
{
    LL s=0;
    while(b)
    {
        if(b&1) s=(s+a)%mod;
        a=(a+a)%mod;
        b>>=1;
    }
    return s;
} 
LL q_pow(LL a,LL b,LL mod)//快速幂 
{
    LL s=1;
    while(b)
    {
        if(b&1) s=q_mul(s,a,mod);
        a=q_mul(a,a,mod);
        b>>=1;
    }
    return s;
}

bool MR(LL n)
{
    if (n==2) return 1;
    if (n%2==0) return 0;
    int lg=0;LL w=n-1;
    while (w%2==0) lg++,w/=2;
    for (int i=1;i<=9;i++)
    {
        if (n==prime[i]) return 1;
        LL x=q_pow(prime[i],w,n);
        if (x==1||x==n-1) continue;
        for (int j=1;j<=lg;j++)
        {
            LL y=q_mul(x,x,n);
            if (x!=1&&x!=n-1&&y==1) return 0;
            x=y;
        }
        if (x!=1) return 0;
    }
    return 1;
}

bool pd_prime(LL x)
{
    if(x<M) return (!np[x]);
    else return MR(x);
}

void dfs(int dep,LL x,LL res)
{
    if(x+1>prime[cnt]&&pd_prime(x+1)) {ans[++cnt2]=res*(x+1);}

    LL tres=res,tx=x,s;
    for(int i=dep;i>=1;i--)
    {
        if(x%(prime[i]-1)==0)
        {
            s=1;
            tres=res;
            tx=x/(prime[i]-1);
            while(tx%s==0)
            {
                tres*=prime[i];
                dfs(i-1,tx/s,tres);
                s*=prime[i];
            }
        }
    } 
    if(x==1){ans[++cnt2]=res;return;} 
}

int main()
{
    freopen("arc.in","r",stdin);
    freopen("arc.out","w",stdout);
    prepare();//cout<<MR(17);
    scanf("%lld%d",&N,&k);

    dfs(cnt,N,1);
    sort(ans+1,ans+cnt2+1);
    printf("%lld ",ans[1]);k--;
    for(int i=2;k&&i<=cnt2;i++)
    {
        if(ans[i]!=ans[i-1]) printf("%lld ",ans[i]),k--;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值