CQOI 板子大赛之 pollard rho
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
ll e, N, c, jzm, p, q, r, d, n;
ll gcd(ll a, ll b){
return !b?a:gcd(b,a%b);
}
ll mul(ll a, ll b, ll p){
ll re=0;
while(b){
if(b&1) re = (re + a) % p;
a = (a + a) % p;
b >>= 1;
}
return re;
}
ll pollardRho(){
ll x=2, y=2;
while(true){
x = (mul(x,x,N)+jzm) % N;
y = (mul(y,y,N)+jzm) % N;
y = (mul(y,y,N)+jzm) % N;
if(x==y) return 1;
ll re=gcd(x>y?x-y:y-x, N);
if(re>1) return re;
}
}
ll exgcd(ll a, ll b, ll &x, ll &y){
if(!b){
x = 1;
y = 0;
return a;
}
ll re=exgcd(b, a%b, x, y);
ll z=x;
x = y;
y = z - (a / b) * y;
return re;
}
ll ksm(ll a, ll b, ll p){
ll re=1;
while(b){
if(b&1) re = mul(re, a, p);
a = mul(a, a, p);
b >>= 1;
}
return re;
}
int main(){
cin>>e>>N>>c;
do{
jzm++;
p = pollardRho();
}while(p==1);
q = N / p;
r = (p - 1) * (q - 1);
ll dddddd;
exgcd(e, r, d, dddddd);
d = (d % r + r) % r;
n = ksm(c, d, N);
cout<<d<<" "<<n<<endl;
return 0;
}