第一问快速幂,第二问扩欧或者快速幂,第三问BSGS,模板题
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#include<queue>
#include<stack>
using namespace std;
#define MAXN 1010
#define MAXM 1010
#define INF 1000000000
#define MOD 1000000007
#define eps 1e-8
#define ll long long
ll a,b,p;
ll mi(ll x,ll y,ll mod){
ll re=1;
while(y){
if(y&1){
(re*=x)%=mod;
}
(x*=x)%=mod;
y>>=1;
}
return re;
}
map<ll,ll>h;
void cal1(){
printf("%lld\n",mi(a,b,p));
}
ll gcd(ll x,ll y){
return !y?x:gcd(y,x%y);
}
void exgcd(ll a,ll b,ll &x,ll &y){
if(!b){
x=1;
y=0;
return ;
}
exgcd(b,a%b,x,y);
ll t=x;
x=y;
y=t-a/b*y;
}
void cal2(){
ll g=gcd(a,p);
if(b%gcd(a,p)){
printf("Orz, I cannot find x!\n");
return ;
}
ll A=a;
ll B=p;
A/=g;
B/=g;
b/=g;
ll x,y;
exgcd(A,B,x,y);
x*=b;
((x%=B)+=B)%=B;
printf("%lld\n",x);
}
void cal3(){
int i;
a%=p;
b%=p;
if(!a&&b){
printf("Orz, I cannot find x!\n");
return ;
}
ll m=ceil(sqrt(p))+1;
h.clear();
ll now=b*a%p;
for(i=1;i<=m;i++){
h[now]=i;
(now*=a)%=p;
}
ll t=mi(a,m,p);
now=t;
for(i=1;i<=m;i++){
if(h[now]){
printf("%lld\n",i*m-h[now]);
return ;
}
(now*=t)%=p;
}
printf("Orz, I cannot find x!\n");
}
int main(){
int tmp,o;
scanf("%d%d",&tmp,&o);
while(tmp--){
scanf("%lld%lld%lld",&a,&b,&p);
if(o==1){
cal1();
}
if(o==2){
cal2();
}
if(o==3){
cal3();
}
}
return 0;
}
/*
*/