复杂度O(sqrt(P)*logP)
#include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #include<iostream> #include<map> using namespace std; int mod,y,z,k,T,n; map<int,int> mp; int power(int x,int y,int mod) { int ans=1; while (y) { if (y&1) ans=(long long)ans*x%mod; x=(long long)x*x%mod; y>>=1; } return ans; } void BSGS(int y,int z,int mod) { if (y==0 && z==0) {printf("1\n");return;} if (y==0 && z!=0) {printf("Orz, I cannot find x!\n");return;} mp.clear(); int temp=1,p=power(y,mod-2,mod),k=ceil(sqrt(mod)); mp[z]=k+1; for (int i=1;i<k;i++) { temp=(long long)temp*p%mod; int t=(long long)temp*z%mod; if (!mp[t]) mp[t]=i; } temp=1;p=power(y,k,mod); for (int i=0;i<k;i++,temp=(long long)temp*p%mod) { if (mp[temp]) { if (mp[temp]==k+1) printf("%d\n",i*k); else printf("%d\n",i*k+mp[temp]); return; } } printf("Orz, I cannot find x!\n"); } int main() { scanf("%d%d",&n,&T); for (int i=1;i<=n;i++) { scanf("%d%d%d",&y,&z,&mod); y%=mod; if (T==1) printf("%d\n",power(y,z,mod)); else if (T==2) { z%=mod; if (y==0 && z!=0) printf("Orz, I cannot find x!\n"); else printf("%d\n",(long long)z*power(y,mod-2,mod)%mod); } else z%=mod,BSGS(y,z,mod); } return 0; }