# 【BZOJ3122】随机数生成器（BSGS，数论）

## 题解

$X_{i+1}=(X_1+c)*a^i-c$的形式

$X_{i+1}+c=a(X_i+c)$

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define RG register
{
RG int x=0,t=1;RG char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
int fpow(int a,int b,int MOD)
{
int s=1;
while(b){if(b&1)s=1ll*s*a%MOD;a=1ll*a*a%MOD;b>>=1;}
return s;
}
const int HashMod=111111;
struct HashTable
{
struct Line{int u,v,next;}e[100000];
int h[HashMod],cnt;
void Add(int u,int v,int w){e[++cnt]=(Line){w,v,h[u]};h[u]=cnt;}
void clear(){memset(h,0,sizeof(h));cnt=0;}
void Insert(int x,int i){Add(x%HashMod,i,x);}
int Query(int x)
{
for(RG int i=h[x%HashMod];i;i=e[i].next)
if(e[i].u==x)return e[i].v;
return -1;
}
}Hash;
int BSGS(int a,int y,int z,int p)
{
if(y%p==0)return -2;
if(z==a)return -1;
Hash.clear();
int m=sqrt(p)+1;
for(RG int i=0,t=z;i<m;++i,t=1ll*t*y%p)Hash.Insert(t,i);
for(RG int i=1,tt=fpow(y,m,p),t=1ll*a*tt%p;i<=m;++i,t=1ll*t*tt%p)
{
int B=Hash.Query(t);if(B==-1)continue;
return i*m-B;
}
return -2;
}
int main()
{
while(T--)
{
if(X1==t){puts("1");continue;}
if(a==0){if(b==t)puts("2");else puts("-1");continue;}
if(a==1)
{
if(b==0){puts("-1");continue;}
t=(t+p-X1)%p;
t=1ll*t*fpow(b,p-2,p)%p;
printf("%d\n",t+1);
continue;
}
c=1ll*b*fpow(a-1,p-2,p)%p;
t=(t+c)%p;X1=(X1+c)%p;
printf("%d\n",BSGS(X1,a,t,p)+1);
}
return 0;
}


posted @ 2018-04-12 22:09  小蒟蒻yyb  阅读(426)  评论(0编辑  收藏  举报