BZOJ 5296: [Cqoi2018]破解D-H协议(BSGS)
解题思路
\(BSGS\)裸题??要求的是\(g^a =A (mod\) \(p)\),设\(m\)为\(\sqrt p\),那么可以设\(a=i*m-j\),式子变成
$$ g^{i*m-j}=A\mod p$$
然后把\(j\)移过去,
$$g{i*m}=A*gj\mod p$$
然后可以预处理枚举\(j\)的值用哈希存下来,每次直接\(O(m)\)询问,总的时间复杂度为\(O(T\sqrt p \log)\)
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#define int long long
using namespace std;
typedef long long LL;
int g,MOD,T,A,B,a,b,m;
map<int,int> mp;
inline int fast_pow(int x,int y){
int ret=1;
for(;y;y>>=1){
if(y&1) ret=1ll*ret*x%MOD;
x=1ll*x*x%MOD;
}
return ret;
}
inline void prework(){
m=ceil(sqrt(MOD));
int base=fast_pow(g,m),now=1;
mp[now]=0;
for(int i=1;i<=m;i++){
now=1ll*now*base%MOD;
mp[now]=i;
}
}
inline int BSGS(int x){
int now=x;
if(mp.count(now)) return mp[now]*m;
for(int i=1;i<=m;i++){
now=1ll*now*g%MOD;
if(mp.count(now)) return mp[now]*m-i;
}
}
signed main(){
scanf("%lld%lld%lld",&g,&MOD,&T);
prework();
while(T--){
scanf("%lld%lld",&A,&B);
a=BSGS(A); b=BSGS(B);
a=(a%(MOD-1)+MOD-1)%(MOD-1);
b=(b%(MOD-1)+MOD-1)%(MOD-1);
printf("%lld\n",fast_pow(g,1ll*a*b%(MOD-1)));
}
return 0;
}
BZOJ 5296: [Cqoi2018]破解D-H协议(BSGS)的更多相关文章
- BZOJ_5296_[Cqoi2018]破解D-H协议_BSGS
BZOJ_5296_[Cqoi2018]破解D-H协议_BSGS Description Diffie-Hellman密钥交换协议是一种简单有效的密钥交换方法.它可以让通讯双方在没有事先约定密钥(密码 ...
- BZOJ5296 CQOI2018 破解D-H协议 【BSGS】
BZOJ5296 CQOI2018Day1T1 破解D-H协议 Description Diffie-Hellman密钥交换协议是一种简单有效的密钥交换方法.它可以让通讯双方在没有事先约定密钥(密码) ...
- loj#2531. 「CQOI2018」破解 D-H 协议(BSGS)
题意 题目链接 Sol 搞个BSGS板子出题人也是很棒棒哦 #include<bits/stdc++.h> #define Pair pair<int, int> #defin ...
- BZOJ5296 [CQOI2018] 破解D-H协议 【数学】【BSGS】
题目分析: 裸题. 代码: #include<bits/stdc++.h> using namespace std; typedef long long ll; ; #define mp ...
- 2018.12.18 bzoj5296: [Cqoi2018]破解D-H协议(bsgs)
传送门 bsgsbsgsbsgs基础题. 考虑到给的是原根,因此没无解的情况. 于是只需要每次把a,ba,ba,b解出来. 然后可以通过预处理节省一部分时间. 代码: #include<bits ...
- [CQOI2018]破解D-H协议
嘟嘟嘟 这不就是个bsgs板儿嘛. 顺便就复习了一下bsgs和哈希表. 头一次觉得我的博客这么好用,一下就懂了:数论学习笔记之高次不定方程 这里再补充几点: 1.关于这一段代码: int S = sq ...
- LG4454 【[CQOI2018]破解D-H协议】
先谈一下BSGS算法(传送门) 但是上面这位的程序实现比较繁琐,看下面这位的. clover_hxy这样说 bsgs算法,又称大小步算法(某大神称拔山盖世算法). 主要用来解决 A^x=B(mod C ...
- P4454 [CQOI2018]破解D-H协议
链接 这题并不难只是需要把题读懂 - By ShadderLeave 一句话题意 给定两个数 \(p\)和\(g\),有\(t\)组询问,每组询问给出\(A\)和\(B\) 其中 A = \(g^a ...
- bzoj 5301: [Cqoi2018]异或序列 (莫队算法)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=5301 题面; 5301: [Cqoi2018]异或序列 Time Limit: 10 Sec ...
随机推荐
- 使用eclipse制作war包方法 web项目打包到tomcat
打开eclipse在左侧右击项目名选择“Export” 在导出画面点击 “Web”->“WAR file”点击“Next” 点击“Browse…”选择文件的导出位置,Target run ...
- How to resolve error “Failed to resolve: org.jetbrains.kotlin:kotlin-stdlib-jre7…” when building in Android Studio Ask Question
//implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" implementation & ...
- Web安全测试——常见的威胁攻防
SQL注入 部分程序员在编写代码的时候,没有对用户输入数据的合法性进行判断,使应用程序存在安全隐患.用户可以提交一段数据库查询代码,根据程序返回的结果,获得某些他想得知的数据,这就是所谓的SQL In ...
- Normal Equation Algorithm求解多元线性回归的Octave仿真
Normal Equation算法及其简洁,仅需一步即可计算出theta的取值,实现如下: function [theta] = normalEqn(X, y) theta = zeros(size( ...
- Win7崩溃程序目录
很烦,占用系统空间,毫无用处 C:\Users\你的用户名\AppData\Local\CrashDumps C:\Users\你的用户名\AppData\Local\Microsoft\Window ...
- spring-第六篇之创建bean的3种方式
1.创建bean的方式有3种: 1>使用构造器创建bean,即设值注入.构造注入本质都是使用bean的构造器创建bean的. 2>使用静态工厂方法创建bean. 3>调用实例工厂方法 ...
- [Bzoj3223][Tyvj1729] 文艺平衡树(splay/无旋Treap)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3223 平衡树处理区间问题的入门题目,普通平衡树那道题在维护平衡树上是以每个数的值作为维护 ...
- Emacs中的前进后退jump-tree
Emacs中的前进后退jump-tree */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #8394 ...
- .ef core 多对对关系的关联方法
最近在用.net core 重构博客,在使用ef core连表查询时,遇到了一些问题.记录一下. 关系:一个博客可以有多个标签,一个标签可以属于多个博客,博客和标签之间存在多对多的关系 下面是实体代码 ...
- javascript跨浏览器操作xml
//跨浏览器获取xmlDom function getXMLDOM(xmlStr) { var xmlDom = null; if (typeof window.DOMParser != 'undef ...