BZOJ 2242: [SDOI2011]计算器 [快速幂 BSGS]
2242: [SDOI2011]计算器
题意:求\(a^b \mod p,\ ax \equiv b \mod p,\ a^x \equiv b \mod p\),p是质数
这种裸题我竟然WA了好多次
第三个注意判断a和b整除p的情况
#pragma GCC optimize ("O2")
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
using namespace std;
typedef long long ll;
#define fir first
#define sec second
inline int read() {
char c=getchar(); int x=0, f=1;
while(c<'0' || c>'9') {if(c=='-')f=-1; c=getchar();}
while(c>='0' && c<='9') {x=x*10+c-'0'; c=getchar();}
return x*f;
}
int a, b, p;
ll Pow(ll a, int b, int p) {
a%=p; ll ans=1;
for(; b; b>>=1, a=a*a%p)
if(b&1) ans=ans*a%p;
return ans;
}
ll inv(int a, int p) {
if(a%p==0) return -1;
return Pow(a, p-2, p);
}
map<int, int> ma;
ll ind(int a, int b, int p) {
a%=p; b%=p;
ma.clear();
ll e=1; int m=sqrt(p)+0.5;
for(int i=0; i<m; i++) {
if(!ma.count(e)) ma[e]=i;
e=e*a%p;
}
e=Pow(e, p-2, p);
for(int i=0; i<m; i++) {
if(ma.count(b)) return i*m + ma[b];
b=b*e%p;
}
return -1;
}
int main() {
//freopen("in","r",stdin);
freopen("calc.in","r",stdin);
freopen("calc.out","w",stdout);
int T=read(), type=read();
while(T--) {
a=read(); b=read(); p=read();
ll ans;
if(type==1) ans=Pow(a, b, p);
else if(type==2) {ans=inv(a, p);if(ans!=-1) ans=ans*b%p;}
else ans=ind(a, b, p);
if(ans==-1) puts("Orz, I cannot find x!");
else printf("%lld\n", ans);
}
}
BZOJ 2242: [SDOI2011]计算器 [快速幂 BSGS]的更多相关文章
- BZOJ 2242: [SDOI2011]计算器( 快速幂 + 扩展欧几里德 + BSGS )
没什么好说的... --------------------------------------------------------------------- #include<cstdio&g ...
- bzoj 2242 [SDOI2011]计算器 快速幂+扩展欧几里得+BSGS
1:快速幂 2:exgcd 3:exbsgs,题里说是素数,但我打的普通bsgs就wa,exbsgs就A了...... (map就是慢)..... #include<cstdio> # ...
- bzoj 2242: [SDOI2011]计算器 BSGS+快速幂+扩展欧几里德
2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 你被 ...
- 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]计算器 ——EXGCD/快速幂/BSGS
三合一的题目. exgcd不解释,快速幂不解释. BSGS采用了一种不用写EXGCD的方法,写起来感觉好了很多. 比较坑,没给BSGS的样例(LAJI) #include <map> #i ...
- bzoj 2242: [SDOI2011]计算器【扩展欧几里得+快速幂+BSGS】
第一问快速幂板子 第二问把式子转化为\( xy\equiv Z(mod P)\rightarrow xy+bP=z \),然后扩展欧几里得 第三问BSGS板子 #include<iostream ...
- 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]计算器(数论知识)
Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给 ...
- bzoj 2242: [SDOI2011]计算器
#include<cstdio> #include<iostream> #include<map> #include<cmath> #define ll ...
随机推荐
- hdu_1029_hash/map
http://acm.hdu.edu.cn/showproblem.php?pid=1029 太水了,一次过,直接上代码吧,只想说最愚蠢的hash都要比map快! #include<cstdio ...
- Prime Ring Problem(dfs水)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- c++(排序二叉树)
前面我们讲过双向链表的数据结构.每一个循环节点有两个指针,一个指向前面一个节点,一个指向后继节点,这样所有的节点像一颗颗珍珠一样被一根线穿在了一起.然而今天我们讨论的数据结构却有一点不同,它有三个节点 ...
- 移动App,AJAX异步请求,实现简单的增、删、改、查
用ajax发异步请求时,要注意url."AppServer"为后台项目名,"LoginServlet.action"为web.xml中的<url-patt ...
- 关于vue的使用计算属性VS使用计算方法的问题
在vue中需要做一些计算时使用计算属性和调用methods方法都可以达到相同的效果,那么这两种使用方式的区别在哪里: <div id="example"> <p& ...
- ZooKeeper 分布式共享锁的实现
原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/8352919.html ------------------------------------ ...
- RocketMQ-事务消费
理论部分在https://www.jianshu.com/p/453c6e7ff81c中的 "三.事务消息".下面从代码层面看一下rockemq的事务消息 一.事务消费端. 从代码 ...
- 捕获arm非托管磁盘虚拟机,并进行还原
背景:非托管磁盘虚拟机"hlmcen69n1",附加了一块100GB的数据磁盘.由于arm非托管磁盘机器无法通过Portal界面直接"Capture",故只能通 ...
- linux_系统调优
linux如何调优? 1. 关闭SELLinux功能,美国国家安全局对于强制访问控制实现,生产场景也是关闭 cat /etc/selinux/config | grep '^SELINUX=' # 查 ...
- duilib消息类型
//定义所有消息类型 ////////////////////////////////////////////////////////////////////////// #define DUI_MS ...