#include <iostream> #include <algorithm> #include <string.h> #include <cstdio> #include <cmath> using namespace std; typedef long long LL; struct Node{ int idx; LL val; Node(int cidx=0, LL cval=0){ idx=cidx; val=cval; } bool operator <(const Node &rhs)const{ return val<rhs.val||( val == rhs.val && idx<rhs.idx ); } }P[1000000]; LL pow_mod(LL a, LL n, LL mod) { LL ans=1; while(n) { if(n&1)ans=(ans*a)%mod; n>>=1; a=(a*a)%mod; } return ans; } int bitsearch(LL d,int n) { int L=0,R=n-1; while(L<=R) { int mid=(L+R)>>1; if(P[mid].val==d) return P[mid].idx; if(P[mid].val<d) L=mid+1; else R=mid-1; } return -1; } LL log_mod(LL a, LL b, LL n) { LL m,v,e=1; m=(sqrt(n+0.5))+1; v=pow_mod(a,n-m-1,n); P[0]=Node(0,1); for(int i=1; i<m; i++) { e=(e*a)%n; P[i]=Node(i,e); } sort(P,P+m); int cnt=1; for(int i=0; i<m; i++) if(P[i].val!=P[cnt-1].val) P[cnt++]=P[i]; for(int i=0; i<m; i++) { int loc=bitsearch(b,cnt); if(loc!=-1){ return i*m+loc; } b=(b*v)%n; } return -1; } int main() { LL P,B,N; while(scanf("%I64d%I64d%I64d",&P,&B,&N)==3) { LL d =log_mod(B,N,P); if(d==-1)puts("no solution"); else printf("%I64d\n",d); } return 0; }
时间: 2024-10-17 08:04:57