BZOJ 2242 / Luogu P2485 [SDOI2011]计算器 (BSGS)
type 1type\ 1type 1 就直接快速幂
type 2type\ 2type 2 特判+求逆元就行了.
type 3type\ 3type 3 BSGS板
CODE
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
inline LL qpow(LL a, LL b, LL c) {
LL re = 1;
while(b) {
if(b & 1) re = re * a % c;
a = a * a % c; b >>= 1;
}
return re;
}
int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
map<int, int>myhash;
inline int Baby_Step_Giant_Step(int a, int b, int p) {
if(p == 1) return 0;
a %= p, b %= p;
if(b == 1) return 0;
int cnt = 0; LL tmp = 1;
for(int g = gcd(a, p); g != 1; g = gcd(a, p)) {
if(b % g) return -1;
b /= g, p /= g, tmp = tmp * (a/g) % p;
++cnt;
if(b == tmp) return cnt;
}
myhash.clear();
int m = int(sqrt(p) + 1);
LL base = b;
for(int i = 0; i < m; ++i) {
myhash[base] = i;
base = base * a % p;
}
base = qpow(a, m, p);
for(int i = 1; i <= m+1; ++i) {
tmp = tmp * base % p;
if(myhash.count(tmp))
return i*m - myhash[tmp] + cnt;
}
return -1;
}
inline void solve1(int a, int b, int p) {
printf("%d\n", qpow(a, b, p));
}
inline void solve2(int a, int b, int p) {
a %= p, b %= p;
if(!a) {
if(!b) puts("0");
else puts("Orz, I cannot find x!");
}
else printf("%d\n", int(1ll * b * qpow(a, p-2, p) % p));
}
inline void solve3(int a, int b, int p) {
int ans = Baby_Step_Giant_Step(a, b, p);
if(~ans) printf("%d\n", ans);
else puts("Orz, I cannot find x!");
}
int main() {
int a, b, p, T, type;
scanf("%d%d", &T, &type);
while(T--) {
scanf("%d%d%d", &a, &b, &p);
if(type == 1) solve1(a, b, p);
else if(type == 2) solve2(a, b, p);
else solve3(a, b, p);
}
}
BZOJ 2242 / Luogu P2485 [SDOI2011]计算器 (BSGS)的更多相关文章
- luogu P2485 [SDOI2011]计算器
题目描述 你被要求设计一个计算器完成以下三项任务: 1.给定y.z.p,计算y^z mod p 的值: 2.给定y.z.p,计算满足xy ≡z(mod p)的最小非负整数x: 3.给定y.z.p,计算 ...
- 洛谷 P2485 [SDOI2011]计算器 解题报告
P2485 [SDOI2011]计算器 题目描述 你被要求设计一个计算器完成以下三项任务: 1.给定y.z.p,计算y^z mod p 的值: 2.给定y.z.p,计算满足xy ≡z(mod p)的最 ...
- P2485 [SDOI2011]计算器
P2485 [SDOI2011]计算器 题目描述 你被要求设计一个计算器完成以下三项任务: 1.给定y.z.p,计算y^z mod p 的值: 2.给定y.z.p,计算满足xy ≡z(mod p)的最 ...
- bzoj 2242: [SDOI2011]计算器 BSGS+快速幂+扩展欧几里德
2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 你被 ...
- 【BZOJ2242】[SDOI2011]计算器 BSGS
[BZOJ2242][SDOI2011]计算器 Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ ...
- 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]计算器 | BSGS
insert的时候忘了取模了-- #include <cstdio> #include <cmath> #include <cstring> #include &l ...
- bzoj 2242: [SDOI2011]计算器 & BSGS算法笔记
这题的主要难点在于第三问该如何解决 于是就要知道BSGS是怎样的一种方法了 首先BSGS是meet in the middle的一种(戳下面看) http://m.blog.csdn.net/blog ...
随机推荐
- 菜单中Clean和batch build的作用
清除Build生成的中间文件,删除编译的文件 编译的不一样,有重新编译和普通编译,重新编译会删除以前生成的文件
- ffmpeg AVFrame结构体及其相关函数
0. 简介 AVFrame中存储的是原始数据(例如视频的YUV, RGB, 音频的PCM), 此外还包含了一些相关的信息, 例如: 解码的时候存储了宏块类型表, QP表, 运动矢量等数据. 编码的时候 ...
- linux kprobe rootkit学习
介绍 <linux二进制分析>中提到了使用kprobe来写内核rootkit,还给出了一个简单的源码实现,这里看一下他的源码 kprobe kprobe的介绍可以看下面这几篇文章 介绍:h ...
- 小米soar
SOAR 简介 SOAR,即 SQL Optimizer And Rewriter,是一款 SQL 智能优化与改写工具,由小米运维 DBA 团队出品 SOAR 体系架构 SOAR主要由语法解析器,集成 ...
- Scala学习十一——操作符
一.本章要点 标识符由字母,数字或运算符构成 一元和二元操作符其实是方法调用 操作符优先级取决于第一个字符,而结合性取决于最后一个字符 apply和update方法在对expr(args)表达式求值时 ...
- dev chart使用
public class LineChartHelp { #region 折线图 /// <summary> /// 创建折线图 /// </summary> public v ...
- C#进阶之泛型(Generic)
1.泛型 泛型是framwork2.0推出的新语法,具有延迟声明的特点:把参数类型的声明推迟到调用的时候.泛型不是一个语法糖,是框架升级提供的功能.需要编辑器和JIT(just-in-time com ...
- EF入门-CRUD操作
一.EF数据查询假设我们已经定义好了context:private AccountContext db = new AccountContext(); 1.[基本查询] 查询所有var users = ...
- mockjs介绍
官网 https://github.com/nuysoft/Mock/wiki/Getting-Started 一.为什么使用mockjs 在做开发时,当后端的接口还未完成,前端为了不影响工作效率,手 ...
- linux(centos7) 查看磁盘空间大小
命令: df -hl 1 显示: 文件系统 容量 已用 可用 已用% 挂载点 Filesystem Size Used Avail Use% Mounted on /dev/hda2 45G 19G ...