题意

X i = a X i 1 + b <mtext>    </mtext> ( m o d <mtext>    </mtext> p ) X_i=a \cdot X_{i-1} + b \ \ (mod \ \ p) Xi=aXi1+b  (mod  p)
给出 X 0 X_0 X0,序列长度 n , a , b , p n,a,b,p n,a,b,p 的值,求 X k = V X_k=V Xk=V 的最小的 k k k ,没有则输出 1 -1 1

题解

<mstyle displaystyle="true" scriptlevel="0"> X n </mstyle> <mstyle displaystyle="true" scriptlevel="0"> = X 0 a n + a n 1 b + a n 2 b + . . . + b </mstyle> <mstyle displaystyle="true" scriptlevel="0"> </mstyle> <mstyle displaystyle="true" scriptlevel="0"> = X 0 a n + b <mtext>    </mtext> 1 a n 1 a </mstyle> <mstyle displaystyle="true" scriptlevel="0"> </mstyle> <mstyle displaystyle="true" scriptlevel="0"> = V </mstyle> \begin{aligned} X_n&amp;=X_0 \cdot a^n+a^{n-1}b+a^{n-2}b+...+b \\ &amp;=X_0 \cdot a^n+b ~~ \frac{1-a^n}{1-a} \\ &amp;=V \end{aligned} Xn=X0an+an1b+an2b+...+b=X0an+b  1a1an=V
V = X 0 a n + b <mtext>    </mtext> 1 a n 1 a V = X 0 a n + b <mtext>   </mtext> ( a n 1 ) a n = V + b X 0 + b a n = V V=X_0 \cdot a^n+b ~~ \frac{1-a^n}{1-a} \\ V=X_0 \cdot a^n+b&#x27; ~ (a^n-1) \\ a^n= \frac{V+b&#x27;}{X_0+b&#x27;} \\ a^n=V&#x27; V=X0an+b  1a1anV=X0an+b (an1)an=X0+bV+ban=V
然后用 B S G S BSGS BSGS 求即可
直接求得话会 T L E TLE TLE, 因为询问太多了,需要预处理
a n = a p m b a^n=a^{p \cdot m-b} an=apmb
a p <mtext>   </mtext> m ( V ) 1 = a b a^{ p ~ \cdot m} \cdot(V&#x27;)^{-1}= a^b ap m(V)1=ab
考虑预处理出 m = 1 e 6 m=1e6 m=1e6 时的 a a a 的幂,即 b [ 0 , 1000000 ) b \in [0,1000000) b[0,1000000)
然后枚举 p p p , 再 H a s h Hash Hash 查找即可

代码

#include<bits/stdc++.h>
#define N 1000010
#define INF 0x3f3f3f3f
#define eps 1e-5
#define pi 3.141592653589793
#define mod 998244353
#define P 1000000007
#define LL long long
#define pb push_back
#define fi first
#define se second
#define cl clear
#define si size
#define lb lower_bound
#define ub upper_bound
#define bug(x) cerr<<#x<<" : "<<x<<endl
#define mem(x) memset(x,0,sizeof x)
#define sc(x) scanf("%d",&x)
#define scc(x,y) scanf("%d%d",&x,&y)
#define sccc(x,y,z) scanf("%d%d%d",&x,&y,&z)
using namespace std;
   
int m,T;
LL x,a,b,p,n,v;
   
LL qpow(LL a,LL b){
    LL res=1;
    while(b){
        if (b&1) res=res*a%p;
        a=a*a%p;
        b>>=1;
    }
    return res;
}
  
struct HASH
{
    const int M = 1999997;
    int la[2000000],nx[2000000],cc[N],key[N],cnt=0;
    void clr(){
        memset(la,-1,sizeof la);
        cnt=0;
    }
    inline int find(int x){
        int h=x%M;
        for (int i=la[h];i!=-1;i=nx[i])
            if (key[i]==x) return cc[i];
        return  -1;
    }
    inline void ins(int x,int w){
        int h=x%M;
        key[++cnt]=x;
        cc[cnt]=w;
        nx[cnt]=la[h];
        la[h]=cnt;
    }
}mp;
 
void slove(){
    b=b*qpow(a-1,p-2)%p;
    LL tm=qpow(b+x,p-2),t=1;
    int block=pow(p,2.0/3);
    mp.clr(); mp.ins(1,1);
    for(int i=1;i<block;i++) t=t*a%p,mp.ins(t,i+1);
    a=qpow(a,block);
    int lim=min(p/block,n/block)+1;
    while(m--){
        scanf("%lld",&v);
        if (x==v) { puts("0");continue; }
        v=(v+b)%p*tm%p;
        if (v==1) { puts("0");continue; }
        LL iv=qpow(v,p-2);
        LL res=1; int fg=0;
        for(int i=1;i<=lim;i++){
            res=res*a%p;
            int t=mp.find(res*iv%p);
            if (t!=-1) {
                LL pos=i*block-t+1;
                if (pos<n){
                    printf("%lld\n",pos);
                }else puts("-1");
                fg=1;
                break;
            }
        }
        if (!fg)puts("-1");
    }
}
   
void fix0(){
    while(m--){
        scanf("%lld",&v);
        if (v==x) puts("0");else
        if (v==b) puts("1");else puts("-1");
    }
}
void fix1(){
    int ib=qpow(b,p-2);
    while(m--){
        scanf("%lld",&v);
        if (b==0){
            if (v==x) puts("0");else puts("-1");
            continue;
        }
        LL pos=(v-x+p)%p*ib%p;
        if (pos<n) printf("%lld\n",pos);else puts("-1");
    }
}
  
int main(){
    sc(T);
    while(T--){
        scanf("%lld%lld%lld%lld%lld",&n,&x,&a,&b,&p);
        sc(m);
        if (a==0) fix0();else
        if (a==1) fix1();else
        slove();
    }
}