BZOJ2242 [SDOI2011]计算器
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!
Description
Input
输入包含多组数据。
Output
Sample Input
3 1
2 1 3
2 2 3
2 3 3
【样例输入2】
3 2
2 1 3
2 2 3
2 3 3
【数据规模和约定】
对于100%的数据,1<=y,z,p<=10^9,为质数,1<=T<=10。
Sample Output
2
1
2
【样例输出2】
2
1
0
//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
using namespace std;
typedef long long LL;
const int MOD = ;
const int MAXM = ;
int k,p,first[MOD+],ecnt,to[MAXM],w[MAXM],next[MAXM],m,ans;
inline int gcd(int x,int y){ if(y==) return x; return gcd(y,x%y); }
inline int fast_pow(LL x,int y){ LL r=; while(y>) { if(y&) r*=x,r%=p; x*=x; x%=p; y>>=; } return (int)r; }
inline void wujie(){ printf("Orz, I cannot find x!"); }
inline int getint(){
int w=,q=; char c=getchar(); while((c<''||c>'') && c!='-') c=getchar();
if(c=='-') q=,c=getchar(); while (c>=''&&c<='') w=w*+c-'',c=getchar(); return q?-w:w;
} inline void exgcd(LL x,LL y,LL &d,LL &a,LL &b){
if(y==) { d=x; a=; b=; return ; }
exgcd(y,x%y,d,b,a);
b-=x/y*a;
} inline void solve(int a,int Z){
int GCD=gcd(a,p); if(Z%GCD!=) { wujie(); return ; }
LL x,y,GG; exgcd((LL)a,(LL)p,GG,x,y);
Z/=GCD; p/=GCD;
ans=Z*x%p; ans+=p; ans%=p;
printf("%d",ans);
} inline void insert(int x,int j){
int cc=x; x%=MOD; for(int i=first[x];i;i=next[i]) if(to[i]==cc) { w[i]=j; return ;}
next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=cc; w[ecnt]=j;
} inline int query(int x){
int cc=x; x%=MOD; for(int i=first[x];i;i=next[i]) if(to[i]==cc) return w[i];
return -;
} inline void BSGS(int a,int b){
if(a%p==) { wujie(); return; }
//if(b==1) { printf("0"); return ; }
ecnt=; memset(first,,sizeof(first));
m=sqrt(p); if(m*m<p) m++; LL cc=b; insert(b,);
for(int i=;i<=m;i++) cc*=a,cc%=p,insert((int)cc,i);
cc=; LL cun=fast_pow(a,m);
for(int i=;i<=m;i++) {
cc*=cun; cc%=p; ans=query(cc);
if(ans==-) continue;
printf("%d",i*m-ans);
return ;
}
wujie();
} inline void work(){
int T=getint(); k=getint(); int x,y;
while(T--) {
x=getint(); y=getint(); p=getint();
if(k==) printf("%d",fast_pow(x,y));
else if(k==) solve(x,y);
else BSGS(x,y);
printf("\n");
}
} int main()
{
work();
return ;
}
BZOJ2242 [SDOI2011]计算器的更多相关文章
- [bzoj2242][Sdoi2011]计算器_exgcd_BSGS
计算器 bzoj-2242 Sdoi-2011 题目大意:裸题,支持快速幂.扩展gcd.拔山盖世 注释:所有数据保证int,10组数据. 想法:裸题,就是注意一下exgcd别敲错... ... 最后, ...
- BZOJ2242 [SDOI2011]计算器 【BSGS】
2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 4741 Solved: 1796 [Submit][Sta ...
- BZOJ2242[SDOI2011]计算器——exgcd+BSGS
题目描述 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给定y,z,p, ...
- bzoj2242: [SDOI2011]计算器 BSGS+exgcd
你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值:(快速幂) 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数:(exgcd) 3.给 ...
- 【数学 BSGS】bzoj2242: [SDOI2011]计算器
数论的板子集合…… Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最 ...
- [bzoj2242][SDOI2011][计算器] (Baby-Step-Giant-Step+快速幂+exgcd)
Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给 ...
- bzoj2242: [SDOI2011]计算器 && BSGS 算法
BSGS算法 给定y.z.p,计算满足yx mod p=z的最小非负整数x.p为质数(没法写数学公式,以下内容用心去感受吧) 设 x = i*m + j. 则 y^(j)≡z∗y^(-i*m)) (m ...
- 2018.12.18 bzoj2242: [SDOI2011]计算器(数论)
传送门 数论基础题. 对于第一种情况用快速幂,第二种用exgcdexgcdexgcd,第三种用bsgsbsgsbsgs 于是自己瞎yyyyyy了一个bsgsbsgsbsgs的板子(不知道是不是数据水了 ...
- bzoj千题计划246:bzoj2242: [SDOI2011]计算器
http://www.lydsy.com/JudgeOnline/problem.php?id=2242 #include<map> #include<cmath> #incl ...
随机推荐
- swift实现饭否应用客户端源码
swift 版 iOS 饭否客户端 源码下载:http://code.662p.com/view/13318.html 饭否是中国大陆地区第一家提供微博服务的网站,被称为中国版Twitter.用户可通 ...
- PL/SQL重新编译包无反应
前几天碰到一个有趣的事情:早上同事执行一个包很久没有反应,就中断了执行,发邮件让我帮忙查看具体情况,我用PL/SQL Developer登录后,找到这个包的过程中发现这个包的图标有红色叉叉,也就是说这 ...
- SQL SERVER 2012 修改数据库默认位置不立即生效
今天修改SQL SERVER 2012的数据库默认位置:即数据文件.日志文件默认位置时遇到一个问题,单击"服务器属性"(Server Properties)--> 数据库设置 ...
- DB2 JDBC
官方文档: http://www-01.ibm.com/support/knowledgecenter/SSEPGG_10.1.0/com.ibm.db2.luw.apdv.java.doc/src/ ...
- weblogic忘记登陆密码
以下内容来自网络: weblogic安装后,很久不用,忘记访问控制台的用户名或者密码,可通过以下步骤来重置用户名密码. 版本:WebLogic Server 11g 说明:%DOMAIN_HOME%: ...
- 查询表结构sql
THEN obj.name ELSE '' END AS 表名, col.colorder AS 序号, col.name AS 列名, ISNULL(ep.value, N'') AS 列说明, t ...
- [有意思]The IT workers of Star Wars -- That's not a bug. It's a feature
Yeah, that Artoo is kinda mouthy... ... now select, "restore to factory settings." That'll ...
- 003.安装nginx(lnmp)
一.下载nginx 下载nginx源码包,解压: [root@huh ~]# cd /usr/local/src/ [root@huh src]# wget http://nginx.org/down ...
- js快速判断IE浏览器(兼容IE10与IE11)
在很多时候,我们一般采用navigator.userAgent和正则表达来判断IE浏览器版本,下面介绍用IE浏览器中不同特性来判断IE浏览器 1 判断IE浏览器与非IE 浏览器 IE浏览器与非IE ...
- Useful commmands in Gentoo
Safe way to upgrade: emerge --sync & eix-sync emerge -avuDN --with-bdeps y --keep-going world et ...