1. 程式人生 > >Bzoj 3122 隨機數生成器

Bzoj 3122 隨機數生成器


輸入含有多組資料,第一行一個正整數T,表示這個測試點內的資料組數。
接下來T行,每行有五個整數p,a,b,X1,t,表示一組資料。保證X1和t都是合法的頁碼。注意:P一定為質數
output

共T行,每行一個整數表示他最早讀到第t頁是哪一天。如果他永遠不會讀到第t頁,輸出-1。
input
3
7 1 1 3 3
7 2 2 2 0
7 2 2 2 1
output
1
3
-1
hint 0<=a<=P-1,0<=b<=P-1,2<=P<=10^9
a^(n−1)≡(Xn+b*inv(a−1))*inv(X1+b*inv(a−1))(modp)

推匯出來這個式子,程式碼一直寫不對

//#include <bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#define X 10005
#define inF 0x3f3f3f3f
#define PI 3.141592653589793238462643383
#define IO  ios::sync_with_stdio(false),cin.tie(0), cout.tie(0);
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
typedef long long ll;
typedef unsigned long long Ull; //2^64
const int maxn = (int)2*1e7 + 10;
//const int MOD = 9973;//(int)1e9 + 7;
//const ll inf = 9223372036854775807;
//const int mod=76532;
int MOD;
void ex_gcd(ll a, ll b, ll &d, ll &x, ll &y) { if (!b) { x = 1; y = 0; d = a; } else { ex_gcd(b, a%b, d, y, x); y -= x * (a / b); }; }
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
ll lcm(ll a, ll b) { return b / gcd(a, b)*a; }
ll inv_exgcd(ll a, ll m) { ll d, x, y;ex_gcd(a, m, d, x, y);return d == 1 ? (x + m) % m : -1; }
ll inv1(ll b) { return b == 1 ? 1 : (MOD - MOD / b)*inv1(MOD%b) % MOD; }    //hdu1576用這個板子會爆除0錯誤
ll crt(int n,int *c,int *m){ll M=1,ans=0;for(int i=0;i<n;++i) M*=m[i];
for(int i=0;i<n;++i) ans=(ans+M/m[i]*c[i] %M *inv_exgcd(M/m[i],m[i]))%M; return ans;}
ll N;
const int mod = (int)1e5 + 10;
ll hs[mod],next[mod],head[mod],id[mod],top;
int read()
{
    int x=0;char ch=getchar();
    while(ch<'0'||ch>'9')ch=getchar();
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x;
}
int read()
{
    int x=0;char ch=getchar();
    while(ch<'0'||ch>'9')ch=getchar();
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x;
}
int T;
ll p,a,b,x1,t;
ll exgcd(ll a,ll b,ll &x,ll &y)
{
	if(b==0){x=1;y=0;return a;}
	ll tmp=exgcd(b,a%b,x,y);
	ll t=x;x=y;y=t-a/b*y;
	return tmp;
}
ll kpow(ll a,ll b,ll p)
{
	a%=p;ll ans=1;
	for(ll i=b;i;i>>=1,a=a*a%p)
		if(i&1)ans=ans*a%p;
	return ans;
}

void insert(ll x,ll y)
{ ll k=x%mod;
    hs[top]=x,id[top]=y,next[top]=head[k],head[k]=top++;
}
int find(ll x)
{
    int k=x%mod;
    for(ll i=head[k];i!=-1;i=next[i])
        if(hs[i]==x) return id[i];
return -1;
}
ll BSGS(ll a,ll b,ll n)
{
    memset(head,-1,sizeof(head));
    top=1;
    if(b==1)return 0;
    int m=sqrt(n*1.0),j;
    ll x=1,p=1;
    for(int i=0;i<m;++i,p=p*a%n)insert(p*b%n,i);
    for(ll i=m; ;i+=m)
    {
        if((j=find(x=x*p%n))!=-1)return i-j;
        if(i>n)break;
    }
    return -1;
}
ll cal1()
{
	ll C=(t-x1+p)%p,x,y;
	ll t=exgcd(b,p,x,y);
	if(C%t)return -1;C/=t;
	x=x*C%p;
	if(x<0)x+=p;
	return x+1;
}
ll cal2()
{
	ll c=inv_exgcd(a-1,p),A=(x1+b*c)%p;//kpow(a-1,p-2,p),A=(x1+b*c)%p,也可以用費馬小定理求出逆元,
	ll C=(t+b*c)%p,x,y;
	ll t=exgcd(A,p,x,y);
	x=x%p+p;
	t=BSGS(a,x*C%p,p);
	if(t!=-1)return t+1;
	return -1;
}
ll solve()
{
	if(x1==t)return 1;
	if(a==0)
	{
		if(b==t)return 2;
		return -1;
	}
	if(a==1)return cal1();
	else return cal2();
}
int main()
{
	T=read();
	while(T--)
	{
		p=read();a=read();b=read();x1=read();t=read();
		printf("%lld\n",solve());
	}
	return 0;
}