求逆欧拉函数(arc)

时间:2021-08-02 09:00:39

已知欧拉函数计算公式

  初始公式:φ(n)=n*(1-1/p1)*(1-1/p2).....*(1-1/pm)

     又 n=p1^a1*p2^a2*...*ps^as  欧拉函数是积性函数

  那么:φ(n)=φ(p1^a1)* φ(p2^a2)........φ(pn^an).

  

  

#include<cstdio>
#include<ctime>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#define LL long long
#define maxn 100010
#define MAXN 10000000
using namespace std;
LL n,k,prime[MAXN/],cnt,ans[maxn];
bool mark[MAXN+];
void first()
{
for(int i=;i<=MAXN;i++)
{
if(!mark[i]) prime[++cnt]=i;
for(int j=;j<=cnt;j++)
{
if(i*prime[j]>MAXN) break;
mark[i*prime[j]]=;
if(i%prime[j] ==) break;
}
}
return ;
}
LL mul(LL a,LL b,LL mod)
{
LL ans=;
while(b)
{
if(b&) ans=(ans+a)%mod;
a=(a<<)%mod;b>>=;
}
return ans;
}
LL low(LL a,LL b,LL mod)
{
LL ans=;
while(b)
{
if(b&) ans=mul(ans,a,mod);
b>>=,a=mul(a,a,mod);
}
return ans;
}
bool Is_prime(LL n)
{
if(n<) return ;
if(n==) return ;
if(n% ==) return ;
LL m=n-,j=;
while(m% == )
{
j++;
m>>=;
}
for(int i=;i<=;i++)
{
LL a=rand()%(n-)+;
LL x=low(a,m,n);
LL y;
for(int k=;k<=j;k++)
{
y=mul(x,x,n);
if(y==&&x!=&&x!=n- )
return ;
x=y;
}
if(x!=) return ;
}
return ;
}
void dfs(LL x,LL y,LL mod )
{
if(x==)
{
ans[++ans[]]=y;
return ;
}
if(x+ > prime[cnt] && Is_prime(x+))
ans[++ans[]]=y*(x+);
for(int i=mod;i>=;i--)
if(x%(prime[i]-) == )
{
LL a=x/(prime[i]-),b=y,c=;
while(a%c ==)
{
b*=prime[i];
dfs(a/c,b,i-);
c*=prime[i];
}
}
}
int main()
{
freopen("arc.in","r",stdin);
freopen("arc.out","w",stdout);
scanf("%lld%lld",&n,&k);
srand(time());
first();
dfs(n,,cnt);
sort(ans+,ans++ans[]);
for(int i=;i<=k;i++)
printf("%lld ",ans[i]);
return ;
}