http://www.lydsy.com/JudgeOnline/problem.php?id=3122

等比数列求和公式+BSGS

#include<map>
#include<cmath>
#include<cstdio>
#include<iostream> using namespace std; int p,a,b,x1,t; map<int,int>mp; void read(int &x)
{
x=; char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
} int Pow(int x,int y)
{
int r=;
for(;y;x=1LL*x*x%p,y>>=)
if(y&) r=1LL*r*x%p;
return r;
} void force()
{
int ans=-;
int last=x1,now;
for(int i=;i<=p;++i)
{
now=(1LL*a*last+b)%p;
if(now==t)
{
ans=i;
break;
}
last=now;
}
printf("%d\n",ans);
} void A1()
{
if(!b) printf("-1\n");
else printf("%d\n",int((1LL*(t-x1+p)%p*Pow(b,p-)%p+)%p));
} void BSGS()
{
if(!a)
{
if(t==x1) printf("1\n");
else if(t==b) printf("2\n");
else printf("-1\n");
return;
}
int ans=-;
mp.clear();
int B=1LL*t*Pow(x1,p-)%p;
int m=ceil(1.0*sqrt(1.0*p));
int mul=B;
mp[B]=;
for(int i=;i<m;++i)
{
mul=1LL*mul*a%p;
mp[mul]=i;
}
mul=;
int am=Pow(a,m);
for(int i=;i<=m;++i)
{
mul=1LL*mul*am%p;
if(mp.find(mul)!=mp.end())
{
ans=i*m-mp[mul];
break;
}
}
if(ans!=-) ans++;
printf("%d\n",ans);
} void bsgs()
{
if(!a)
{
if(t==x1) printf("1\n");
else if(t==b) printf("2\n");
else printf("-1\n");
return;
}
int ans=-;
mp.clear();
int tmp=1LL*b*Pow(a-,p-)%p;
int B=1LL*(t+tmp)*Pow(x1+tmp,p-)%p;
int m=ceil(1.0*sqrt(1.0*p));
int mul=B;
mp[B]=;
for(int i=;i<m;++i)
{
mul=1LL*mul*a%p;
mp[mul]=i;
}
mul=;
int am=Pow(a,m);
for(int i=;i<=m;++i)
{
mul=1LL*mul*am%p;
if(mp.find(mul)!=mp.end())
{
ans=i*m-mp[mul];
break;
}
}
if(ans!=-) ans++;
printf("%d\n",ans);
} int main()
{
//freopen("random.in","r",stdin);
//freopen("random.out","w",stdout);
int T;
read(T);
while(T--)
{
read(p); read(a); read(b); read(x1); read(t);
if(x1==t)
{
printf("1\n");
continue;
}
if(p<=) force();
else if(a==) A1();
else if(!b) BSGS();
else bsgs();
}
}

bzoj千题计划259:bzoj3122: [Sdoi2013]随机数生成器的更多相关文章

  1. bzoj3122 [SDOI2013]随机数生成器

    bzoj3122 [SDOI2013]随机数生成器 给定一个递推式, \(X_i=(aX_{i-1}+b)\mod P\) 求满足 \(X_k=t\) 的最小整数解,无解输出 \(-1\) \(0\l ...

  2. bzoj千题计划300:bzoj4823: [Cqoi2017]老C的方块

    http://www.lydsy.com/JudgeOnline/problem.php?id=4823 讨厌的形状就是四联通图 且左右各连一个方块 那么破坏所有满足条件的四联通就好了 按上图方式染色 ...

  3. bzoj千题计划267:bzoj3129: [Sdoi2013]方程

    http://www.lydsy.com/JudgeOnline/problem.php?id=3129 如果没有Ai的限制,就是隔板法,C(m-1,n-1) >=Ai 的限制:m减去Ai &l ...

  4. bzoj千题计划134:bzoj3124: [Sdoi2013]直径

    http://www.lydsy.com/JudgeOnline/problem.php?id=3124 第一问: dfs1.dfs2 dfs2中记录dis[i]表示点i距离最长链左端点的距离 第二问 ...

  5. bzoj千题计划133:bzoj3130: [Sdoi2013]费用流

    http://www.lydsy.com/JudgeOnline/problem.php?id=3130 第一问就是个最大流 第二问: Bob希望总费用尽量大,那肯定是把所有的花费加到流量最大的那一条 ...

  6. bzoj千题计划268:bzoj3131: [Sdoi2013]淘金

    http://www.lydsy.com/JudgeOnline/problem.php?id=3131 如果已知 s[i]=j 表示有j个<=n数的数码乘积=i 那么就会有 s[a1]*s[a ...

  7. bzoj千题计划258:bzoj3123: [Sdoi2013]森林

    http://www.lydsy.com/JudgeOnline/problem.php?id=3123 启发式合并主席树 #include<cmath> #include<cstd ...

  8. bzoj千题计划196:bzoj4826: [Hnoi2017]影魔

    http://www.lydsy.com/JudgeOnline/problem.php?id=4826 吐槽一下bzoj这道题的排版是真丑... 我还是粘洛谷的题面吧... 提供p1的攻击力:i,j ...

  9. bzoj千题计划280:bzoj4592: [Shoi2015]脑洞治疗仪

    http://www.lydsy.com/JudgeOnline/problem.php?id=4592 注意操作1 先挖再补,就是补的范围可以包含挖的范围 SHOI2015 的题 略水啊(逃) #i ...

随机推荐

  1. python3解析网页经过base64编码后的图片

    有时候我们打开网页看到的图片不是普通的url,例如:www.baidu.com/static/2.jpg,而是经过base64方式加密过的路径:例如:data:img/jpg;base64,/9j/4 ...

  2. Postman安装与入门使用

    Postman官方下载地址:https://www.getpostman.com/apps Postman 是一个很强大的 API调试.Http请求的工具.我们可以用来很方便的模拟get或者post或 ...

  3. Final阶段基于spec评论作品

    组名:杨老师粉丝群 组长:乔静玉 组员:吴奕瑶  刘佳瑞  公冶令鑫  杨磊  刘欣  张宇  卢帝同 一.测试目标:拉格朗日2018——飞词 1.开始界面,只有一个开始游戏按钮,简约且醒目     ...

  4. 【MOOC EXP】Linux内核分析实验三报告

     程涵  原创博客 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 [跟踪分析Linux内核的启动过程] ...

  5. C语言版本:单链表的实现

    slist.h #ifndef __SLIST_H__ #define __SLIST_H__ #include<cstdio> #include<malloc.h> #inc ...

  6. OpenState: Programming Platform-independent Stateful OpenFlow Applications Inside the Switch

    文章名称:OpenState: Programming Platform-independent Stateful OpenFlow Applications Inside the Switch Op ...

  7. C++的OOP特性

    内存模型和名称空间 存储持续性,作用域和链接性 C++有三种方案来存储数据 自动存储持续性:在函数定义中声明的变量,包括函数参数.在函数或代码块开始执行时创建.执行完函数或者代码块,内存自动释放. 静 ...

  8. (第三周)c#程序理解

    阅读下面程序,请回答如下问题: 问题1:这个程序要找的是符合什么条件的数? 问题2:这样的数存在么?符合这一条件的最小的数是什么? 问题3:在电脑上运行这一程序,你估计多长时间才能输出第一个结果?时间 ...

  9. 12th 本周工作量及进度统计

    本周PSP: C(类别) C(内容) S(开始时间) ST(结束时间) I(中断时间) T(实际时间) 活动 1日—3日 用户调查 12月1日21:00 12月3日12:00 25小时 14小时 活动 ...

  10. [转帖]shell 中的>/dev/null 2>&1 是什么鬼?

    shell 中的>/dev/null 2>&1 是什么鬼? http://blog.jobbole.com/109355/ 背景 我们经常能在shell脚本中发现>/dev/ ...