计算器(bzoj 2242)
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
/*
Type1:快速幂
Type2:扩展欧几里得
Type3:BSGS
*/
#include<cstdio>
#include<iostream>
#include<map>
#include<cmath>
#define lon long long
using namespace std;
int Q,op;lon A,B,C;
map<lon,int> hash;
lon poww(lon a,lon b){
lon base=a,r=;
while(b){
if(b&) r*=base;r%=C;
base*=base;base%=C;
b>>=;
}
return r;
}
lon exgcd(lon a,lon b,lon &x,lon &y){
if(!b) {
x=;y=;return a;
}
lon r=exgcd(b,a%b,x,y);
lon t=x;x=y;y=t-a/b*y;
return r;
}
void work3(){
hash.clear();
if(A%C==){
printf("Orz, I cannot find x!\n");
return;
}
lon m=ceil(sqrt(C)),ans;
for(int i=;i<=m;i++){
if(!i){
ans=B%C;hash[ans]=i;
continue;
}
ans=(ans*A)%C;hash[ans]=i;
}
ans=;lon t=poww(A,m);bool fl=false;
for(int i=;i<=m;i++){
ans=(ans*t)%C;
if(hash[ans]){
ans=i*m-hash[ans];
ans=(ans+C+C)%C;
printf("%lld\n",ans);
fl=true;
break;
}
}
if(!fl) printf("Orz, I cannot find x!\n");
}
void work2(){
lon x,y,vgcd;
vgcd=exgcd(A,C,x,y);
if(B%vgcd) {
printf("Orz, I cannot find x!\n");
return;
}
x=x*B/vgcd;
lon mod=C/vgcd;
x=((x%mod)+mod)%mod;
printf("%lld\n",x);
}
void work1(){
printf("%lld\n",poww(A,B));
}
int main(){
scanf("%d%d",&Q,&op);
while(Q--){
scanf("%lld%lld%lld",&A,&B,&C);
if(op==) work1();
if(op==) work2();
if(op==) work3();
}
return ;
}
计算器(bzoj 2242)的更多相关文章
- bzoj 2242: [SDOI2011]计算器 BSGS+快速幂+扩展欧几里德
2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 你被 ...
- BZOJ 2242 计算器
Description 你被要求设计一个计算器完成以下三项任务: \(1.\)给定\(y,z,p\),计算\(y^{z}\;mod\;P\)的值: \(2.\)给定\(y,z,p\),计算满足\(xy ...
- BZOJ 2242: [SDOI2011]计算器( 快速幂 + 扩展欧几里德 + BSGS )
没什么好说的... --------------------------------------------------------------------- #include<cstdio&g ...
- BZOJ 2242: [SDOI2011]计算器 [快速幂 BSGS]
2242: [SDOI2011]计算器 题意:求\(a^b \mod p,\ ax \equiv b \mod p,\ a^x \equiv b \mod p\),p是质数 这种裸题我竟然WA了好多次 ...
- 【BZOJ 2242】[SDOI2011]计算器
Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给 ...
- bzoj 2242 [SDOI2011]计算器(数论知识)
Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给 ...
- [BZOJ 2242] [SDOI 2011] 计算器
Description 你被要求设计一个计算器完成以下三项任务: 给定 \(y,z,p\),计算 \(y^z \bmod p\) 的值: 给定 \(y,z,p\),计算满足 \(xy≡ z \pmod ...
- BZOJ.2242.[SDOI2011]计算器(扩展欧几里得 BSGS)
同余方程都不会写了..还一直爆int /* 2.关于同余方程ax ≡b(mod p),可以用Exgcd做,但注意到p为质数,y一定有逆元 首先a%p=0时 仅当b=0时有解:然后有x ≡b*a^-1( ...
- BZOJ 2242 [SDOI2011]计算器(快速幂+Exgcd+BSGS)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2242 [题目大意] 给出T和K 对于K=1,计算 Y^Z Mod P 的值 对于K=2 ...
- bzoj 2242 [SDOI2011]计算器——BSGS模板
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2242 第一道BSGS! 咳咳,我到底改了些什么?…… 感觉和自己的第一版写的差不多……可能是 ...
随机推荐
- /^/m|/$/m|\b|\B|$&|$`|$'|变量捕获|()?|(?:pattern)|(?<LABEL>PATTERN)|$+{LABEL}|(|)|\g{LABEL}
#!/usr/bin/perl use strict; use warnings; $_=' $$ oinn &&& ninq kdownc aninp kkkk'; if ( ...
- 基于matlab的蓝色车牌定位与识别---分割
接着上面的工作,接下去就该是进行字符分割了.考虑到为了后面的字符识别,因此在这部分需要实现的目标是需要把车牌的边框全部切除,对重新定位的车牌进行垂直方向水平方向调整,保证字符是正的.最后才是字符的分割 ...
- Ubuntu16.04安装后开发工作的配置
由于多次安装Ubuntu16.04用于学习,其中出了多次问题.每次找参考文件太麻烦,于是写了这篇总结,方便之后备用. 一.精简系统,删除不常用软件 参考资料来自:https://blog.csdn.n ...
- wamp开发环境配置之配置Apache虚拟主机
网站建设人员在本地测试时,常常不是一个网站,那怎么能像输入域名一样在地址栏上面输入“域名”就能本地访问该网站呢?这里就要配置Apache虚拟主机了! 1.找到apache\conf\httpd.con ...
- Linux下open函数、read函数、write函数记录
open() #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> int open( cons ...
- isinstance() 函数
Python3 isinstance() 函数 描述 isinstance() 函数来判断一个对象是否是一个已知的类型,类似 type(). isinstance() 与 type() 区别: t ...
- flask_关注者
表的模型实现 class Follow(db.Model): __tablename__ = 'follows' follower_id = db.Column(db.Integer,db.Forei ...
- dedecms 搬家流程
进入后台 系统 点击数据备份/还原根据新空间数据库版本格式备份 进入网站根目录备份文件夹data\backupdataimagestempletsuploadsplus 进入新空间 重新安装dede程 ...
- JAVA-基础(五) 更多工具集
1.StringTokenizer(字符串标记) StringTokenizer实现枚举(Enumeration)接口.因此,给定一个输 入字符串,可以使用StringTokenizer对包含于其中的 ...
- AtCoder Regular Contest 080
手贱去开了abc,这么无聊.直接arc啊 C - 4-adjacent Time limit : 2sec / Memory limit : 256MB Score : 400 points Prob ...