# Description

$0\leq a\leq P-1,0\leq b\leq P-1,2\leq P\leq 10^9$

# Code

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <map>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
#define drp(i,st,ed) for (int i=st;i>=ed;--i)

std:: map <int,int> map;

int p;

int ksm(int x,int dep) {
const int MOD=p;
int ret=1;
for (;dep;dep>>=1) {
(dep&1)?(ret=1LL*ret*x%MOD):0;
x=1LL*x*x%MOD;
}
return ret;
}

int BSGS(int a,int b) {
map.clear();
const int MOD=p;
int r=sqrt(MOD)+1,tmp=1LL*b*ksm(a,MOD-2)%MOD;
int wjp=ksm(a,r);
rep(i,0,r) {
tmp=1LL*tmp*a%MOD;
map[tmp]=i;
}
tmp=1;
rep(i,1,r) {
tmp=1LL*tmp*wjp%MOD;
int lxf=map[tmp];
if (lxf) return r*i-lxf;
}
return -2;
}

int main(void) {
freopen("data.in","r",stdin);
freopen("myp.out","w",stdout);
int T; scanf("%d",&T);
for (int a,b,x,t;T--;) {
scanf("%d%d%d%d%d",&p,&a,&b,&x,&t);
const int MOD=p;
if (x==t) {
puts("1");
continue;
}
if (a==0) {
if (b==t) puts("2");
else puts("-1");
continue;
}
if (a==1) {
if (b==0) puts("-1");
else {
t=((t-x)%MOD+MOD)%MOD;
t=1LL*t*ksm(b,MOD-2)%MOD;
printf("%d\n", t+1);
}
continue;
}
int c=(1LL*b*ksm(a-1,MOD-2)%MOD+t)%MOD;
c=1LL*c*ksm((1LL*b*ksm(a-1,MOD-2)%MOD+x)%MOD,MOD-2)%MOD;
printf("%d\n", BSGS(a,c)+1);
}
return 0;
}


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120