【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,计算 ...
随机推荐
- chrome浏览器视频插件
以前安装chrome浏览器flash插件是将libflashplayer.so拷贝到chrome浏览器的plugins目录下.但最近好像不行了. 于是换了另一插件:pepperflashplugin- ...
- 【Java】 String类型的==使用
public class StringDemo { public static void main(String[] args) { String s1 = "abc"; Stri ...
- matplotlib使用总结
一.简介 Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形.通过 Matplotlib,开发者可以仅需要几行代码,便可以生成绘图 ...
- Mysql远程连接报错:SQL Error (1130): Host '192.168.61.128' is not allowed to connect to this MySQL server
Mysql远程连接报错:SQL Error (1130): Host '192.168.0.18' is not allowed to connect to this MySQL server ...
- CSUOJ 1826 Languages map+stringstream
Description The Enterprise has encountered a planet that at one point had been inhabited. The onlyre ...
- JAVAEE——BOS物流项目02:学习计划、动态添加选项卡、ztree、项目底层代码构建
1 学习计划 1.jQuery easyUI中动态添加选项卡 2.jquery ztree插件使用 n 下载ztree n 基于标准json数据构造ztree n 基于简单json数据构造ztree( ...
- ACM 水果 hdu 1263 一题多解
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1263 文章末有相应的一些测试数据供参考. 传统的数组解题方式 思路一: 三种属性的数据放在一个结构体里面, ...
- mongodb中获取图片文件<标记>
获取图片文件 @RequestMapping(value="/downLoadFileFormMongo.do",method=RequestMethod.GET) @Respon ...
- 1017 Queueing at Bank (25)(25 point(s))
problem Suppose a bank has K windows open for service. There is a yellow line in front of the window ...
- 韩梦飞沙Android应用集合 想法
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 那些收藏的图片 那些收藏的微博 那些收藏的音乐 定时短信 音乐列表汇 每天都是快乐的