Input

输入含有多组数据,第一行一个正整数T,表示这个测试点内的数据组数。

接下来T行,每行有五个整数p,a,b,X1,t,表示一组数据。保证X1和t都是合法的页码。

注意:P一定为质数

Output

共T行,每行一个整数表示他最早读到第t页是哪一天。如果他永远不会读到第t页,输出-1。

Sample Input

3

7 1 1 3 3

7 2 2 2 0

7 2 2 2 1

Sample Output

1

3

-1

HINT

0<=a<=P-1,0<=b<=P-1,2<=P<=10^9

/*
考试的时候没想出来然后打的暴力orz.
其实式子还是挺好推的.
然后用BSGS和exgcd搞.
还有各种可判.
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
#define LL long long
#define MAXN 101
using namespace std;
LL T,p[MAXN],a[MAXN],b[MAXN],x1[MAXN],t[MAXN];
map<LL,int>s;
bool vis[MAXN];
bool flag1=true,flag2=true,flag3=true;
LL read()
{
LL x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*f;
}
void slove1(int i)
{
LL tot,sum;
tot=x1[i],sum=1;
while(true)
{
if(tot==t[i]) {printf("%d\n",sum);break;}
if(!vis[tot]) vis[tot]=true;
else {printf("-1\n");break;}
tot=(a[i]*tot+b[i])%p[i];
sum++;
}
for(int j=0;j<p[i];j++) vis[j]=0;
}
void exgcd(LL a1,LL b1,LL &x,LL &y)
{
if(!b1){x=1;y=0;return ;}
exgcd(b1,a1%b1,y,x),y-=(a1/b1)*x;
return ;
}
void slove2(int i)
{
if(!a[i])
{
if(b[i]==t[i]) printf("2\n");
else printf("-1\n");
return ;
}
LL c,g,tot,x,y;
c=(t[i]-x1[i]+p[i])%p[i];
if(!c){cout<<1<<endl;return;}
g=__gcd(b[i],p[i]);
if(c%g) {cout<<-1<<endl;return ;}
exgcd(b[i],p[i],x,y);
x=(x*c/g)%p[i];x++;
x=(x+p[i])%p[i];
cout<<x<<endl;
}
LL mi(LL a1,LL b1,LL p1)
{
LL tot1=1;//a1%=p1;
while(b1)
{
if(b1&1) tot1=tot1*a1%p1;
a1=a1*a1%p1;
b1>>=1;
}
return tot1;
}
void slove3(int i)
{
LL c,g,y;bool flag;
s.clear();flag=false;
c=mi(a[i]-1,p[i]-2,p[i]);
exgcd((b[i]*c+x1[i])%p[i],p[i],g,y);
if(g<p[i]) g=g%p[i]+p[i];
if(a[i]==1)
{
cout<<g+1<<endl;return ;
}
LL tmp1=(b[i]*c+t[i])%p[i],tmp2=__gcd(b[i]*c+x1[i],p[i]);
if(tmp1%tmp2) {cout<<-1<<endl;return ;}
g=((g*(tmp1/tmp2)+p[i])%p[i]+p[i])%p[i];
LL m=ceil(sqrt(p[i])),tot=1,tt;
for(int j=1;j<=m-1;j++)
{
tot=tot*a[i]%p[i];
if(!s[tot]) s[tot]=j;
}
tot=1;tmp1=mi(a[i],p[i]-m-1,p[i]);s[1]=m+1;
for(int k=0;k<=m-1;k++)
{
tt=s[tot*g%p[i]];
if(tt)
{
if(tt==m+1) tt=0;
flag=true;
cout<<k*m+tt+1<<endl;
break;
}
tot=tot*tmp1%p[i];
}
if(!flag) cout<<-1<<endl;
}
int main()
{
T=read();
for(int i=1;i<=T;i++)
{
p[i]=read(),a[i]=read(),b[i]=read(),
x1[i]=read(),t[i]=read();
if(x1[i]==t[i]) {printf("1\n");continue;}
if(p[i]<=200) slove1(i);
else if(a[i]<2) slove2(i);
else slove3(i);
}
return 0;
}

Bzoj 3122 [Sdoi2013]随机数生成器(BSGS+exgcd)的更多相关文章

  1. bzoj 3122 : [Sdoi2013]随机数生成器 BSGS

    BSGS算法 转自:http://blog.csdn.net/clove_unique 问题 给定a,b,p,求最小的非负整数x,满足$a^x≡b(mod \ p)$ 题解 这就是经典的BSGS算法, ...

  2. 【BZOJ3122】[Sdoi2013]随机数生成器 BSGS+exgcd+特判

    [BZOJ3122][Sdoi2013]随机数生成器 Description Input 输入含有多组数据,第一行一个正整数T,表示这个测试点内的数据组数.   接下来T行,每行有五个整数p,a,b, ...

  3. bzoj 3122 [Sdoi2013]随机数生成器(逆元,BSGS)

    Description Input 输入含有多组数据,第一行一个正整数T,表示这个测试点内的数据组数.    接下来T行,每行有五个整数p,a,b,X1,t,表示一组数据.保证X1和t都是合法的页码. ...

  4. bzoj 3122: [Sdoi2013]随机数生成器【BSGS】

    题目要求的是: \[ ...a(a(a(ax+b)+b)+b)+b...=a^nx+a^{n-1}b+a^{n-2}b+...+b\equiv t(mod\ p) \] 后面这一大坨看着不舒服,所以考 ...

  5. bzoj 3122: [Sdoi2013]随机数生成器

    #include<cstdio> #include<iostream> #include<map> #include<cmath> #define ll ...

  6. 【BZOJ 3122】 [Sdoi2013]随机数生成器 (BSGS)

    3122: [Sdoi2013]随机数生成器 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1442  Solved: 552 Description ...

  7. 【bzoj3122】[Sdoi2013]随机数生成器 BSGS思想的利用

    题目描述 给出递推公式 $x_{i+1}=(ax_i+b)\mod p$ 中的 $p$.$a$.$b$.$x_1$ ,其中 $p$ 是质数.输入 $t$ ,求最小的 $n$ ,使得 $x_n=t$ . ...

  8. BZOJ 3122 SDOI2013 随机数发生器 数论 EXBSGS

    标题效果:给定一列数X(i+1)=(a*Xi+b)%p 最低要求i>0.所以Xi=t 0.0 这个问题可以1A那很棒 首先讨论特殊情况 如果X1=t ans=1 如果a=0 ans=b==t? ...

  9. BZOJ3122: [Sdoi2013]随机数生成器(BSGS)

    题意 题目链接 Sol 这题也比较休闲. 直接把\(X_{i+1} = (aX_i + b) \pmod P\)展开,推到最后会得到这么个玩意儿 \[ a^{i-1} (x_1 + \frac{b}{ ...

随机推荐

  1. 基于socketserver实现并发的socket套接字编程

    一.基于TCP协议 基于tcp的套接字,关键就是两个循环,一个链接循环,一个通信循环 socketserver模块中分两大类:server类(解决链接问题)和request类(解决通信问题) 1.1 ...

  2. Scratch 少儿编程之旅(四)— Scratch入门动画《小猫捉蝴蝶》(中)

    本期内容概括: 了解Scratch的更多操作,用[无限循环]来更改“小猫”角色的代码: 添加[碰到边缘就反弹]积木块指令: 更改角色的旋转模式和造型,让”小猫”走路更生动: 两种[循环]语句的区别: ...

  3. javascript 之 扩展对象 jQuery.extend

    在JQuery的API手册中,extend方法挂载在JQuery 和 JQuery.fn两个不同的对象上,但在JQuery内部代码实现的是相同的,只是功能各不相同. 官方解释: jQuery.exte ...

  4. Python遗传和进化算法框架(一)Geatpy快速入门

    https://blog.csdn.net/qq_33353186/article/details/82014986 Geatpy是一个高性能的Python遗传算法库以及开放式进化算法框架,由华南理工 ...

  5. 编写Postgres扩展之三:调试

    原文:http://big-elephants.com/2015-10/writing-postgres-extensions-part-iii/ 编译:Tacey Wong 在上一篇关于编写Post ...

  6. python的excel处理之openpyxl

    一.颜色处理 cell = sheet.cell(row, col)font = Font(size=12, bold=False, name='Arial', color=colors.BLACK) ...

  7. JavaScript内置一些方法的实现原理--new关键字,call/apply/bind方法--前戏

    new关键字,call/apply/bind方法都和this的绑定有关,在学习之前,首先要理解this. 一起来学习一下this吧 首先.this是一个对象. 对象很好理解,引用类型值,可以实现如th ...

  8. php权限管理

    首先权限管理肯定是需要登陆的,这里就简单的写一个登陆页面. 简单的登陆页面login.php <h1>登录页面</h1> <form action="login ...

  9. 学习到目前,自己封装的db类和pdo类

    DB封装类 <?php class DBDA { public $host = "localhost"; public $uid = "root"; pu ...

  10. active port

    2510099 - SSL Port XXXXX Not Active - message on NWA even though SSL works Resolution Open the defau ...