Bzoj 3122 随机数生成器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sxy201658506207/article/details/83589231


输入含有多组数据,第一行一个正整数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;
}

猜你喜欢

转载自blog.csdn.net/sxy201658506207/article/details/83589231