传送门
题解:略,几个模板ho在一起就完了。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mop=1e5+4;
inline ll mult(ll x,ll y,ll MOD) {
x%=MOD,y%=MOD;
return ((x*y-(ll)(((long double)x*y+0.5)/MOD)*MOD)%MOD+MOD)%MOD;
}
inline ll fpow(ll a,ll b,ll MOD) {
ll ret=1;
while (b) {
if (b&1) ret=mult(ret,a,MOD);
b>>=1,a=mult(a,a,MOD);
}
return ret;
}
inline ll inv(ll a,ll p) {
if (a%p==0) return -1;
return fpow(a,p-2,p);
}
struct Hash_{
int head[mop],last[mop],etot;
ll dest[mop][2];
void init(){
memset(head,0,sizeof(head));
etot=0;
}
void add(ll a,ll b){
int key=a%mop;
for(int t=head[key];t;t=last[t])
if(dest[t][0]==a) return;
last[++etot]=head[key];
dest[etot][0]=a;
dest[etot][1]=b;
head[key]=etot;
}
ll query(int a){
int key=a%mop;
for(int t=head[key];t;t=last[t])
if(dest[t][0]==a) return dest[t][1];
return -1;
}
}has;
inline ll BSGS(ll g, ll a,ll p){
if (g%p==0) return -1;
has.init();
ll m=(ll)ceil(sqrt(a))+1;
ll cur=1;
for(register int i=0;i<m;++i,cur=mult(cur,g,p)){
if(cur==a) return i;
has.add(cur,i);
}
ll base=inv(cur,p);
cur=mult(base,a,p);
for(register int i=m;i<=p-1;i+=m,cur=mult(cur,base,p)) {
ll j=has.query(cur);
if(~j) return j+i;
}
return -1;
}
int main() {
int kase,type;
scanf("%d%d",&kase,&type);
while (kase--) {
ll a,b,p;
scanf("%lld%lld%lld",&a,&b,&p);
switch (type) {
case 1:{
cout<<fpow(a,b,p)<<endl;
break;
}
case 2:{
ll ans=inv(a,p);
if (~ans) cout<<ans*b%p<<endl;
else cout<<"Orz, I cannot find x!\n";
break;
}
case 3:{
ll ans=BSGS(a,b,p);
if (~ans) cout<<ans<<endl;
else cout<<"Orz, I cannot find x!\n";
break;
}
}
}
return 0;
}