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缩放行为,缩放执行选为默认,如果不行三个都试试
随机推荐
- 基于模板匹配的目标跟踪(OpenCV)
基于VS2010+ OpenCV2.代码可以读入视频,也可以读摄像头,两者的选择只需要在代码中稍微修改即可.对于视频来说,运行会先显示第一帧,然后我们用鼠标框选要跟踪的目标,然后跟踪器开始跟踪每一帧. ...
- Select 多个表并且相关联转置
已知一个表的结构为: ------------------- 姓名 科目 成绩 张三 语文 20 张三 数学 30 张三 英语 50 李四 语文 70 李四 数学 60 李四 英语 90 怎样通过 ...
- Centos修改时间
1. 普通用户 a. 获取超级用户root权限 命令:su或者su -或者su - root b. passwd 用户名 2. 超级用户 a. 打开system-auth文件 命令:vi /etc/p ...
- IDEA连接 Oracle数据库
package com.zxx.util;import org.apache.commons.dbutils.DbUtils;import java.sql.Connection;import jav ...
- signalfx的中间件监控指标so cool
signalfx的中间件监控指标so cool www.jianshu.com 对于我们做运维的来说,监控是最基本的东西,不过在初创公司很多计划是跟不上项目架构变化的,项目中会不断加入各种服务和组 ...
- 更改:把redis替换成kafka
之前的流程是:filebeat,redis,logstash,elasticsearch 现在的流程是:filebeat,Kafka(zookeeper),logstash,elasticsearch ...
- nofollow标签浪费了多少站长做外链的时间
对于rel=“external nofollow”和rel=“nofollow”,这两句代码,使用起来效果基本是一致的,只是前者较后者更为规范一点,两者翻译过来的意思就是:rel=“external ...
- dev chart使用
public class LineChartHelp { #region 折线图 /// <summary> /// 创建折线图 /// </summary> public v ...
- centos7安装配置zabbix监控
CentOs7安装部署Zabbix3.4 1.关闭selinux和firewall systemctl stop firewalld setenforce 1.1检测selinux是否关闭 [ro ...
- springboot-oracle工程win下正常,centos下不能访问数据库
工程在win下正常运行,部署到centos下出现下述异常: ### Error querying database. Cause: org.springframework.jdbc.CannotGet ...