【BZOJ】【2242】【SDOI2011】计算器
快速幂/扩展欧几里得/BSGS
经典好例题!!
三个问题三种算法……
算法:白书(算法竞赛入门经典——训练指南)全有……
/**************************************************************
Problem: 2242
User: Tunix
Language: C++
Result: Accepted
Time:1824 ms
Memory:2476 kb
****************************************************************/ //BZOJ 2242
#include<map>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
using namespace std; int getint(){
int v=,sign=; char ch=getchar();
while(ch<''||ch>'') {if (ch=='-') sign=-; ch=getchar();}
while(ch>=''&&ch<='') {v=v*+ch-''; ch=getchar();}
return v*=sign;
}
/*******************tamplate********************/
typedef long long LL;
LL x,y,z,P;
LL pow(LL a,LL b,LL P){
LL r=,base=a;
while(b){
if (b&) r=r*base%P;
base=base*base%P;
b>>=;
}
return r;
}
void exgcd(LL a,LL b,LL &d,LL &x,LL &y){
if (!b) {d=a; x=; y=; return;}
else{exgcd(b,a%b,d,y,x); y-=x*(a/b);}
}
LL log_mod(LL a,LL b,LL P){
LL m,v,e=,i;
m=ceil(sqrt(P+0.5));//这里需要用ceil……?
v=pow(a,P-m-,P);//inv
map<LL,LL> x;
x[]=;
for(int i=;i<m;++i){
e=e*a%P;
if (!x.count(e)) x[e]=i;
}
rep(i,m){
if (x.count(b)) return (LL)i*m+x[b];
b=b*v%P;
}
return -;
}
int main(){
int T=getint(),K=getint();
while(T--){
y=getint(); z=getint(); P=getint();
if (K==) printf("%lld\n",pow(y,z,P));
if (K==){
LL d=,X=,Y=;
exgcd(y,P,d,X,Y);
if(z%d){ printf("Orz, I cannot find x!\n"); continue; }
X*=z/d;
LL T=X*d/P;
X-=T*P/d;
if (X<) X+=P/d;
printf("%lld\n",X);
}
if (K==){
y%=P; z%=P;
if(!y && !z) {printf("1\n"); continue;}
if(!y) { printf("Orz, I cannot find x!\n"); continue; }
LL ans=log_mod(y,z,P);
if (ans==-){ printf("Orz, I cannot find x!\n"); continue; }
printf("%lld\n",ans);
}
}
return ;
}
【BZOJ】【2242】【SDOI2011】计算器的更多相关文章
- bzoj 2242: [SDOI2011]计算器 BSGS+快速幂+扩展欧几里德
2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 你被 ...
- 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]计算器(扩展欧几里得 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! 咳咳,我到底改了些什么?…… 感觉和自己的第一版写的差不多……可能是 ...
- BZOJ 2242 [SDOI2011]计算器 BSGS+高速幂+EXGCD
题意:id=2242">链接 方法: BSGS+高速幂+EXGCD 解析: BSGS- 题解同上.. 代码: #include <cmath> #include <c ...
- bzoj 2242: [SDOI2011]计算器
#include<cstdio> #include<iostream> #include<map> #include<cmath> #define ll ...
- [原博客] BZOJ 2242 [SDOI2011] 计算器
题目链接 noip级数论模版题了吧.让求三个东西: 给定y,z,p,计算`Y^Z Mod P` 的值. 给定y,z,p,计算满足`xy≡ Z ( mod P )`的最小非负整数. 给定y,z,p,计算 ...
随机推荐
- linux下文件转码
一.工具介绍 enca是一个很好用的文件转码工具,使用命令 sudo apt-get install enca 即可安装 二.基本用法 1.查看文件编码 $ enca filename 2.文件转码 ...
- python 并集union, 交集intersection, 差集difference, 对称差集symmetric_difference
python的集合set和其他语言类似,是一个无序不重复元素集, 可用于消除重复元素. 支持union(联合), intersection(交), difference(差)和sysmmetric d ...
- python sys.argv[]的用法简明解释
sys模块中文参考文档:http://xukaizijian.blog.163.com/blog/static/170433119201111625428624/ sys.argv[]: 「argv」 ...
- linux 设备文件和设备之间联系的建立
<设备驱动模型> 注:几乎所有的设备结构体都包含"strcut kobject kobj"和"srtuct list_head list"该结构体 ...
- centOS7.4服务器 yum安装 搭建lamp环境
// 红色加粗是linux命令 安装gcc和gcc-c++ yum -y install gcc gcc-c++ yum list httpd* 安装apche yum -y install http ...
- ThinkPHP3.2设置异常页面404跳转页面
在ThinkPHP3.2版本中当我们访问不存在的页面时会出现非常不友好错误提示页面,类如下图: 0 «上一篇:div非弹出框半透明遮罩实现全屏幕遮盖css实现»下一篇:利于反向代理绑定任意的域名 po ...
- CF961E Tufurama【主席树】
CF961E Tufurama 题意翻译 题目描述 有一天Polycarp决定重看他最喜爱的电视剧<Tufurama>.当他搜索“在线全高清免费观看Tufurama第3季第7集”却只得到第 ...
- python及其模块下载集合
1)python平台 https://www.python.org/downloads/ 2)打包工具 cx-freeze(python3以上版本打包工具) http://cx-freeze.sour ...
- BZOJ 3751: [NOIP2014]解方程 数学
3751: [NOIP2014]解方程 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3751 Description 已知多项式方程: ...
- iOS学习之WebView的使用 (主要是下面的全屏半透明实现)
1.使用UIWebView加载网页 运行XCode 4.3,新建一个Single View Application,命名为WebViewDemo. 2.加载WebView 在ViewControlle ...