2019牛客多校第5场 generator 2 (BSGS)

在这里插入图片描述
在这里插入图片描述

题意

X i = a ⋅ X i − 1 + b    ( m o d    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

题解

X n = X 0 ⋅ a n + a n − 1 b + a n − 2 b + . . . + b = X 0 ⋅ a n + b    1 − a n 1 − a = V \begin{aligned} X_n&=X_0 \cdot a^n+a^{n-1}b+a^{n-2}b+...+b \\ &=X_0 \cdot a^n+b ~~ \frac{1-a^n}{1-a} \\ &=V \end{aligned} Xn=X0an+an1b+an2b+...+b=X0an+b  1a1an=V
V = X 0 ⋅ a n + b    1 − a n 1 − a V = X 0 ⋅ a n + b ′   ( 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' ~ (a^n-1) \\ a^n= \frac{V+b'}{X_0+b'} \\ a^n=V' 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   ⋅ m ⋅ ( V ′ ) − 1 = a b a^{ p ~ \cdot m} \cdot(V')^{-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();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值