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 ...
随机推荐
- 2017ecjtu-summer training #11 POJ 1018
Communication System Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29218 Accepted: ...
- 6.移植uboot-支持yaffs烧写,打补丁
在上一章,裁剪uboot以及分区后,本章主要使uboot支持yaffs以及制作补丁 1. 修改uboot支持yaffs 首先,每个命令都会对应一个文件,比如nand命令对应的common/cmd_na ...
- YUI 和路径相关的参数与module加载之间的关系
相关参数默认值 使用YUI, 我们可以配置一些和路径相关参数,如base.root.comboBase.cdn, combine.path.fullpath等属性的配置均会影响到YUI的module加 ...
- css3中的关键帧技术分析应用
最近在研究网页加载进度效果的时候发现可以使用css3实现这个效果. 使用css3实现完全不需要图片,相比使用loading.gif的实现来说可能更快. 使用css3实现动态加载的效果,主要会涉及到几个 ...
- 如何判断NSDictionary是否包含某个键
方法一: if([[dictionary allKeys] containsObject:key){ // contains key} 方法二: if([dictionary objectFo ...
- Codeforces 900C Remove Extra One 模拟
题目链接:900C Remove Extra One 题意: 首先record是指这个数比数列前面的所有数都大,给了n个数(1-n),删掉一个数,让整个数列的record值达到最大. 题解: 刚开始我 ...
- shell实例练习+详解
想着将Shell与Python和Java等脚本比较比较,当一有这个念头我就放弃了.这太侮辱Shell了.(哭笑脸!) 作为一个程序员,Linux那是最基本要求.而shell脚本有时候也会显示它在Lin ...
- 信号处理引发的cpu高
背景知识: 1.tty 终端是一种字符型设备,它有多种类型,通常使用tty来简称各种类型的终端设备. tty指的是七个alt+crtl+F1~F7.tty1-tty6表示文字界面,可以用Ctrl+Al ...
- 使用 cURL 度量 Web 站点的响应时间
curl -o /dev/null -s -w %{time_connect}:%{time_starttransfer}:%{time_total} http://www.canada.com 0. ...
- Spring注解装配
Spring 自动装配的主机有 @Autowired.@Intect.@Resource @Autowired是byType的, @Resource是byName的.我们一般用@Atutowired. ...