题目描述
快速分解
做pollard_rho算法后模拟题意。
#include<cstdio>
#include<algorithm>
#include<cmath>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
typedef long long ll;
ll e,d,n,nn,r,c,a,xx,yy,t,p,q,mo;
void gcd(ll a,ll b){
if (!b){
xx=1;
yy=0;
t=a;
}
else{
gcd(b,a%b);
swap(xx,yy);
yy-=xx*(a/b);
}
}
ll getny(ll x,ll y){
gcd(x,y);
xx=(xx%y+y)%y;
return xx;
}
int rand() {
static int rand_seed=1542071823;
rand_seed+=rand_seed<<1|1;
return rand_seed;
}
ll qsc(ll x,ll y){
if (!y) return 0;
ll t=qsc(x,y/2);
t=(t+t)%mo;
if (y%2) t=(t+x)%mo;
return t;
}
ll qsm(ll x,ll y){
if (!y) return 1;
ll t=qsm(x,y/2);
t=qsc(t,t);
if (y%2) t=qsc(t,x);
return t;
}
ll f(ll x){
return (qsc(x,x)+a)%mo;
}
ll pollard_rho(ll n){
ll r1,r2,p;
while (1){
a=rand()%100;
r1=r2=2;
do{
r1=f(r1);
r2=f(f(r2));
gcd(abs(r2-r1),n);
p=t;
if (p>1) return p;
}while (r1!=r2);
}
}
int main(){
scanf("%lld%lld%lld",&e,&n,&c);
mo=n;
p=pollard_rho(n);
q=n/p;
r=(p-1)*(q-1);
d=getny(e,r);
nn=qsm(c,d);
printf("%lld %lld\n",d,nn);
}