原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1038.html
题目传送门 - 51Nod1038
题意
题解
在模质数意义下,求高次剩余,模板题。
不好意思,我只是来存代码的。
代码
#include <bits/stdc++.h> using namespace std; const int N=105; int T,A,B,P; int Fac_p[N],Fac_tot,g; int Pow(int x,int y,int mod){ int ans=1; for (;y;y>>=1,x=1LL*x*x%mod) if (y&1) ans=1LL*ans*x%mod; return ans; } bool Get_g_Check(int P,int x){ for (int i=1;i<=Fac_tot;i++) if (Pow(x,(P-1)/Fac_p[i],P)==1) return 0; return 1; } int Get_g(int P){ Fac_tot=0; int v=P-1; for (int i=2;i*i<=v;i++) if (v%i==0){ Fac_p[++Fac_tot]=i; while (v%i==0) v/=i; } if (v>1) Fac_p[++Fac_tot]=v; for (int i=2;;i++) if (Get_g_Check(P,i)) return i; return -1; } unordered_map <int,int> Map; int BSGS(int A,int B,int P){ // Solve x : A^x = B (mod p) // ans = aM+b int M=(int)sqrt(1.0*P),AM=Pow(A,M,P),AI=Pow(A,P-2,P); Map.clear(); for (int b=0,pw=B;b<M;b++,pw=1LL*pw*AI%P) Map[pw]=b+1; int Alim=(P+M-1)/M; for (int a=0,pw=1;a<Alim;a++,pw=1LL*pw*AM%P) if (Map[pw]) return a*M+Map[pw]-1; return -1; } int exgcd(int a,int b,int &x,int &y){ if (!b){ x=1,y=0; return a; } int res=exgcd(b,a%b,y,x); y-=(a/b)*x; return res; } vector <int> ans; void Get_ans(int a,int c,int p){ // ax = c (mod p) // ax + py = c int x,y,g=exgcd(a,p,x,y); ans.clear(); if (c%g) return; a/=g,c/=g; int P=p/g; x=(1LL*x*c%P+P)%P; while (x<p) ans.push_back(x),x+=P; } int main(){ scanf("%d",&T); while (T--){ scanf("%d%d%d",&P,&A,&B); g=Get_g(P); int t=BSGS(g,B,P); Get_ans(A,t,P-1); if (ans.size()<1) puts("No Solution"); else { for (vector <int> :: iterator i=ans.begin();i!=ans.end();i++) (*i)=Pow(g,*i,P); sort(ans.begin(),ans.end()); for (vector <int> :: iterator i=ans.begin();i!=ans.end();i++) printf("%d ",*i); puts(""); } } return 0; }