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. Jmeter(二十二)_脚本上传Gitlab

    Docker部署接口自动化持续集成环境第四步,代码上传到远程仓库! 接上文:Ubuntu部署jmeter与ant Gitlab在容器中部署好了之后,本地直接打开.我们可以在里面创建项目,上传脚本. 新 ...

  2. cbuild-一个创建和管理C++项目的工具

    cbuild-一个创建和管理C++项目的工具 介绍: 这是个人开发的一个管理C++项目的工具,用shell脚本编写. 可能会不定期更新,也欢迎大家一起完善. 当前开发版本0.5.各版本功能如下: ve ...

  3. Selenium和TestNG

    本文档由Felipe Knorr Kuhn撰写,并根据其博客上发布的一系列文章进行改编. 建模您的测试用例 在编写测试用例之前,您需要知道如何验证以及将要验证的内容.让我们使用WordPress “创 ...

  4. linux centos 中Tomcat的安装和自启动配置

    Tomcat的安装和自启动配置将tomcat添加为linux系统服务,网上找到了很多方法,其中比较简单的如下:方法一:(亲测有效)1. 首先需要将$Tomcat_HOME/bin目录下的catalin ...

  5. python 游戏(井字棋)

    1. 游戏思路和流程图 实现功能,现实生活中的井字棋玩法 游戏流程图 2. 使用模块和游戏提示 import random def game_info(): print('欢迎来到井字棋游戏') pr ...

  6. sql注入语句整理

    1.判断有无注入点; and 1=1 and 1=2 2.猜表一般的表的名称无非是admin adminuser user pass password 等..and 0<>(select ...

  7. Vigenere加密

    Vigenere加密法原理很简单,实现起来也不难.与普通的单码加密法不同,明文经过加密之后,每个字母出现的频率就不会有高峰和低峰. 密钥中字母代表行和明文中的字母代表行.在vigenere表中找到对应 ...

  8. mysql 访问不是本地数据库,给用户刷新了权限没有作用

    1.grant all privileges on *.* to 'yangxin'@'%' identified by 'yangxin123456' with grant option; flus ...

  9. win7+opencv3.0.0+vs2010 安装及配置

    最近看<学习opencv>,想要跑人脸识别的例子,于是先配环境吧. 1.  opencv下载: 具体下载地址,http://opencv.org/,官网太慢,百度网盘的资源链接:http: ...

  10. Java实现模拟登录新浪微博

    毕设题目要使用到新浪微博数据,所以要爬取新浪微博的数据.一般而言,新浪微博的爬虫有两种模式:新浪官方API和模拟登录新浪微博.两种方法的异同点和适用情况就无须赘述了.前辈的文章已经非常多了.写这篇文章 ...