BSGS模板
1 #include<cmath> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #define mod 600000 6 using namespace std; 7 int cnt; 8 long long a,b,c; 9 int head[600005]; 10 struct Edge{ 11 int fr; 12 int val; 13 int nxt; 14 long long to; 15 }edge[600005]; 16 void addedge(int u,long long v,int w){ 17 edge[cnt].fr=u; 18 edge[cnt].to=v; 19 edge[cnt].val=w; 20 edge[cnt].nxt=head[u]; 21 head[u]=cnt++; 22 } 23 void init(){ 24 cnt=0; 25 memset(head,-1,sizeof(head)); 26 } 27 long long BSGS(long long A,long long B,long long C){ 28 init(); 29 int m=sqrt(C); 30 long long mul1=1; 31 long long mul2=1; 32 for(int i=1;i<=m;i++){ 33 mul1=(mul1*A)%C; 34 long long ad=(mul1*B)%C; 35 if(!ad)return -1; 36 addedge(ad%mod,ad,i); 37 } 38 for(int i=1;i<=m+3;i++){ 39 mul2=(mul2*mul1)%C; 40 for(int j=head[mul2%mod];j!=-1;j=edge[j].nxt){ 41 long long v=edge[j].to; 42 if(v==mul2)return 1ll*i*m-edge[j].val; 43 } 44 } 45 return -1; 46 } 47 int main(){ 48 scanf("%lld%lld%lld",&c,&a,&b); 49 long long ans=BSGS(a,b,c); 50 if(ans==-1)printf("no solution\n"); 51 else printf("%lld\n",ans); 52 return 0; 53 }