2018-11-05 09:50:49

$$\phi(x)=x \times \prod_{i=1}^{r} (1-\frac{1}{p_i})$$

$O(\sqrt n)$ 求 $\phi(n)$ 就不多说了,相信大家都会。~~其实是我懒~

代码

#include<cstdio>
#include<algorithm>
#include<iostream>
#define int long long
#define R register

using namespace std;

inline void in(R int &x)
{
int f=1;x=0;char s=getchar();
while(!isdigit(s)){if(s=='-')f=-1;s=getchar();}
while(isdigit(s)){x=x*10+s-'0';s=getchar();}
x*=f;
}

int n,m,ans=2147483647666LL;

int gcd(R int x,R int y){return y==0 ? x:gcd(y,x%y);}

inline int phi(R int x)
{
R int res=x;
for(R int i=2;i*i<=x;i++)
{
if(x%i==0)
{
res=res/i*(i-1);
while(x%i==0)x/=i;
}
}
if(x>1) res=res/x*(x-1);
return res;
}

inline int ksm(R int x,R int y)
{
R int res=1;
for(;y;y>>=1,x=x*x%n)
if(y&1)res=res*x%n;
return res;
}
signed main()
{
in(n),in(m);
if(gcd(n,m)!=1)
{
puts("Let's go Blue Jays!");
return 0;
}
R int tmp=phi(n);
for(R int i=1;i*i<=tmp;i++)
{
if(tmp%i!=0)continue;
if(ksm(m,i)%n==1)
{
ans=i;
break;
}
if(ksm(m,tmp/i)%n==1)ans=min(ans,tmp/i);
}
printf("%lld",ans);
}
• star
首页