逆歐拉函數


φ(x)=xi=1n(11pi)

逆推可得出x的關係式
x=φ(x)p1p11p2p21...pnpn1

然後 dfs
//copy
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#define ll long long
using namespace std;
const int maxm=1e7;
ll prime[999999],cnt;
bool vis[11000000];
ll ans[maxm],tot;
void pre()
{
    vis[1]=1;
    for(int i=1;i<=maxm;i++)
    {
        if(!vis[i])
            prime[++cnt]=i;
        for(int j=1;j<=cnt&&i*prime[j]<=maxm;j++)
        {
            vis[prime[j]*i]=1;
            if(i%prime[j]==0)
                break;
        }
    }
}
ll mul(ll x,ll y,ll mod)
{
    ll ans=0;
    while(y)
    {
        if(y%(1ll*2)) ans=(ans+x%mod)%mod;
        y/=(1ll*2);
        x=(x%mod+x%mod)%mod;
    } 
    return ans;
}
ll fast_pow(ll x,ll y,ll m)
{  
    ll ans=1;  
    x%=m;  
    while(y)  
    {  
        if(y%2)  
          ans=mul(ans, x, m);  
        y/=2;  
        x=mul(x, x, m);  
    }  
    return ans;  
}  
bool Miller_Rabin(ll n)  
{  
    if(n == 2) return 1;  
    if(n<2||!(n&1)) return 0;  
    ll m=n-1;  
    ll k=0;  
    while((k&1)==0)  
    {  
        k++;  
        m/=2;  
    }  
    for(int i=0;i<12;i++)  
    {  
        ll a=rand()%(n - 1)+1;  
        ll x=fast_pow(a, m, n);  
        ll y=0;  
        for(int j=0; j<k; j++)  
        {  
            y=mul(x,x,n);  
            if(y==1&&x!=1&&x!=n-1) return 0;  
            x = y;  
        }  
        if(y!=1) return 0;  
    }  
    return 1;  
}  
void dfs(ll x,ll y,ll z)
{
    if(x+1>prime[cnt]&&Miller_Rabin(x+1))
        ans[++tot]=(x+1)*y;
    for(int i=z;i>=1;i--)
    if(x%(prime[i]-1)==0)
    {
        ll t1=x/(prime[i]-1);
        ll t2=y;
        ll c=1;
        while(t1%c==0)
        {
            t2*=prime[i];
            dfs(t1/c,t2,i-1);
            c*=prime[i];
        }
    }
    if(x==1)
    {
        ans[++tot]=y;
        return;
    }
}
int main()
{
    pre();
    ll n,k;
    scanf("%lld%lld",&n,&k);
    dfs(n,1,cnt);
    sort(ans+1,ans+tot+1);
    for(int i=1;i<=k;i++)
        cout<<ans[i]<<endl;
    return 0;
} 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章