//#define TEST #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long LL; const int M=1e7+10; bool is_p[M]; int prime[M], primelen; int euler(int N) { int len = 0; memset(is_p, true, sizeof(is_p)); for(int i = 2; i < N; i++) { if(is_p[i]) { prime[len] = i; len++; } for(int j = 0; j < len && prime[j] <= i; j++) { if(i * prime[j] >= N) break; is_p[i * prime[j]] = false; if(i % prime[j] == 0) break; } } return len; } LL qmul(LL a,LL b,LL mod){ return (LL)((__int128_t)a*b%mod); /* LL ans=0; while(b>0) { if(b&1) ans=(ans+a)%mod; a=(a+a)%mod; b>>=1; } return ans; */ } LL qpow(LL a, LL b, LL mod){ if(b==0)return 1ll; LL ans=1; while(b>0){ if(b&1)ans=qmul(ans,a,mod); a=qmul(a,a,mod); b>>=1; } return ans; } LL gcd(LL a,LL b){ while(b){ LL t=a%b; a=b; b=t; } return a; } LL exgcd(LL a,LL b,LL &x,LL &y){ if(b == 0){ x = 1, y = 0; return b; } LL d = exgcd(b, a%b, x, y); LL t = y; y = x - y * (a / b); x = t; return d; } LL solveLinear(LL a,LL b,LL p){ a=(a%p+p)%p;b=(b%p+p)%p; LL x,y; LL d=exgcd(a,p,x,y); if(b%d!=0) return -1; return (qmul(b/d,x,p/d)+p/d)%(p/d); } LL invP(LL a,LL p){ a=(a%p+p)%p; if(a==0) return -1; LL x,y; exgcd(a,p,x,y); return (x%p+p)%p; } int factorlist(LL x,LL z,LL* p,int* k){ int cnt=0; LL y=gcd(z/x,x); LL cap=pow(z,0.25)+1; for(int i=0;prime[i]<=cap;i++){ if(y%prime[i]!=0) continue; cnt++; p[cnt]=prime[i]; while(y%prime[i]==0){ y/=prime[i]; } } if(y>1){ cnt++; p[cnt]=y; } for(int i=1;i<=cnt;i++){ k[i]=0; while(x%p[i]==0){ x/=p[i]; k[i]++; } } p[0]=x; return cnt; } const LL hashmod=1000003; LL phash[hashmod+1000]; LL r_origin[hashmod+1000]; LL bsgs(LL a, LL b, LL p){//计算使得a^k=b modp的最小k if(a % p == b % p) return 1; if(a % p == 0 && b % p != 0) return -1; if(b % p == 1) return 0; LL m = (LL)ceil(sqrt(p)); a = (a%p+p)%p; LL r = (b%p+p)%p; memset(phash,-1,sizeof(phash)); memset(r_origin,-1,sizeof(r_origin)); LL k; phash[r%hashmod] = 0;//建立hash r_origin[r%hashmod]=r; for(LL j = 1; j < m; j++){//baby step r = qmul(r, a, p); k=r%hashmod; while(phash[k]!=-1) k++; phash[k] = j; r_origin[k]=r; } LL am = qpow(a, m ,p);//快速幂计算a^m LL t = 1; for(LL i = 1; i <= m; i++){//giant step t = qmul(t, am, p); k=t%hashmod; if(phash[k]!=-1){ while(phash[k]!=-1){ if(r_origin[k]==t) return m * i - phash[k]; k++; } } } return -1; } LL tonelli(LL P,LL p,int k,LL A,LL invA){ LL n=1; for(int i=0;i0;T--){ LL P,n,A,X; scanf("%lld%lld%lld",&P,&n,&A); X=work(P,n,A); printf("%lld\n",X); } return 0; }