BSGS 扩展大步小步法解决离散对数问题 (BZOJ 3239: Discrete Logging// 2480: Spoj3105 Mod)
贴板子 BZOJ 3239: Discrete Logging//2480: Spoj3105 Mod(两道题输入不同,我这里只贴了3239的代码)
CODE
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int p, a, b;
int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
inline int qpow(LL a, LL b, LL c) {
LL res = 1;
while(b) {
if(b&1) res = res * a % c;
a = a * a % c; b >>= 1;
}
return res;
}
map<int, int> myhash;
inline int BSGS(int a, int b, int p) {
a %= p, b %= p;
if(p == 1 || b == 1) return 0;
int cnt = 0; LL tmp = 1; //tmp存a累计下来的系数
for(int g = gcd(a, p); g != 1; g = gcd(a, p)) {
if(b % g) return -1;
p /= g, b /= 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);
LL now = tmp;
for(int i = 1; i <= m+1; ++i) {
now = now * base % p;
if(myhash.count(now))
return i * m - myhash[now] + cnt;
}
return -1; //仍然无解
}
int main() {
while(~scanf("%d%d%d", &p, &a, &b)) {
int ans = BSGS(a, b, p);
if(~ans) printf("%d\n", ans);
else puts("no solution");
}
}
BSGS 扩展大步小步法解决离散对数问题 (BZOJ 3239: Discrete Logging// 2480: Spoj3105 Mod)的更多相关文章
- bzoj 3239: Discrete Logging && 2480: Spoj3105 Mod【BSGS】
都是BSGS的板子题 此时 \( 0 \leq x \leq p-1 \) 设 \( m=\left \lceil \sqrt{p} \right \rceil ,x=i*m-j \)这里-的作用是避 ...
- BZOJ 3239 Discrete Logging(BSGS)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3239 [题目大意] 计算满足 Y^x ≡ Z ( mod P) 的最小非负整数 [题解 ...
- 大步小步法(BSGS) 学习笔记
\(\\\) BSGS 用于求解关于 \(x\) 的方程: \[ a^x\equiv b\pmod p\ ,\ (p,a)=1 \] 一般求解的是模意义下的指数,也就是最小非负整数解. \(\\\) ...
- POJ 2417 Discrete Logging BSGS
http://poj.org/problem?id=2417 BSGS 大步小步法( baby step giant step ) sqrt( p )的复杂度求出 ( a^x ) % p = b % ...
- 【BSGS】BZOJ3239 Discrete Logging
3239: Discrete Logging Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 729 Solved: 485[Submit][Statu ...
- [Unity3D] 5.0 图集合并扩展工具,用于解决UGUI与AssetBundle打包造成资源包过大的问题
[Unity3D] 5.0 图集合并扩展工具,用于解决UGUI与AssetBundle打包造成资源包过大的问题 2017年07月05日 15:57:44 阅读数:1494 http://www.cpp ...
- [转] 扩展微信小程序框架功能
通过第三方 JavaScript 库,扩展微信小程序框架功能. 扩展微信小程序框架功能(1)——Promise ES6 对 Promise 有了原生的支持,但微信开发者工具更新版本(0.11.1122 ...
- JQuery Mobile 的引用代码,以及在手机浏览器上字体太小的解决办法
JQuery Mobile 的引用代码: <link rel="stylesheet" href="http://code.jquery.com/mobile/1. ...
- win10系统中photoshop cs6中界面字体太小的解决方法
win10系统中photoshop cs6中界面字体太小的解决方法 ps页面系统字体特别小,解决方法如下: 右键ps图标—属性—兼容性—勾选替代高dpi缩放行为,缩放执行选为默认,如果不行三个都试试
随机推荐
- [转帖]k8s.gcr.io/pause的作用
k8s.gcr.io/pause的作用 https://blog.51cto.com/liuzhengwei521/2422120 weilovepan520关注0人评论196人阅读2019-07-2 ...
- 【详细解析】MySQL索引详解( 索引概念、6大索引类型、key 和 index 的区别、其他索引方式)
[详细解析]MySQL索引详解( 索引概念.6大索引类型.key 和 index 的区别.其他索引方式) MySQL索引的概念: 索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部分 ...
- 无法执行vue初始化命令
无法执行vue初始化命令:https://www.jianshu.com/p/9eb3cf854aa8 今天vue 初始化项目时提示错误 执行命令: npm install -g vue-cli 执行 ...
- PAT A1058 A+B in Hogwarts (20)
AC代码 #include <cstdio> struct Money { long long Galleon, Sicklke, Knut; }A, B, Sum; void init( ...
- Python 常用单词
Python常用单词(英文好的人自动忽略) 单词 发音 翻译 作用 print 普润特 打印 显示我们想要查看的内容 input 因普特 输入 获取用户输入的一些内容 int 印特 整型 将有引号的数 ...
- spark教程(19)-sparkSQL 性能优化之谓词下推
在 sql 语言中,where 表示的是过滤,这部分语句被 sql 层解析后,在数据库内部以谓词的形式出现: 在 sparkSQL 中,如果出现 where,它会现在数据库层面进行过滤,一般数据库会有 ...
- 树莓派3b 串口乱码的一个解决思路
首先确认波特率设置正确,为115200 向microSD卡中烧录树莓派官方的镜像后,先不要急着把它插入树莓派中. 在电脑上打开microSD卡,按如下方式修改config.txt和cmdline.tx ...
- Web前端开发JQuery框架
JQuery 是一个兼容多浏览器支持的JavaScript库,其核心理念是write less,do more(写得更少,做得更多),它是轻量级的js库,兼容CSS3还兼容各种浏览器,需要注意的是后续 ...
- 你懂什么是分布式系统吗?Redis分布式锁都不会?
分布式系统涉及到很多的技术.理论与协议,很多人也说,分布式系统是“入门容易,深入难”,有一些人简历上写着熟悉分布式系统,很多人都是管中窥豹只见一斑. 究竟什么是分布式系统? 分布式系统是由一组通过网络 ...
- mpstat
mpstat--multiprocessor statistics,统计多处理器的信息 1.安装mpstat工具 [root@localhost ~]# yum install sysstat 2:展 ...