[CQOI 2018]破解D-H协议
Description
给出 \(A,B,P,g\) ,\(g\) 是 \(P\) 的原根,求出 \(A\equiv g^a\pmod{P}\) , \(B\equiv g^b\pmod{P}\) 中的 \(a,b\) ,并输出 \(g^{ab}\) 。
\(2\leq A,B<P<2^{31},2\leq g<20,1\leq n\leq 20\)
Solution
\(BSGS\) 板子啊,由于 \(g\) 是 \(P\) 原根,解是唯一的。
Code
#include <bits/stdc++.h>
using namespace std;
map<int, int>mp;
int g, p, n, a, b;
int quick_pow(int a, int b) {
int ans = 1;
while (b) {
if (b&1) ans = 1ll*ans*a%p;
b >>= 1, a = 1ll*a*a%p;
}
return ans;
}
int BSGS(int a, int b) {
mp.clear();
int lim = ceil(sqrt(p)), cnt = b%p;
for (int i = 0; i <= lim; i++, cnt = 1ll*cnt*a%p)
mp[cnt] = i;
int t = cnt = quick_pow(a, lim);
for (int i = 1; i <= lim; i++, cnt = 1ll*cnt*t%p)
if (mp.count(cnt)) return lim*i-mp[cnt];
}
void work() {
scanf("%d%d%d", &g, &p, &n);
while (n--) {
scanf("%d%d", &a, &b);
a = BSGS(g, a); printf("%d\n", quick_pow(b, a)) ;
}
}
int main() {work(); return 0; }
[CQOI 2018]破解D-H协议的更多相关文章
- intellij idea 2018注册码|intellij idea 2018破解文件下载(附破解教程/汉化包)
intellij idea 2018破解文件http://www.3322.cc/soft/37661.html intellij idea 2018注册码是一款针对“intellij idea 20 ...
- phtoshop cs6 下载安装及破解方法(另附Photoshop CC 2018破解版图文教程)
前言: 前端虽然用PS不多,但有时需要用PS切图:UI给你PSD图,需要取色,查看字体颜色大小:测量元素宽高等 但有时想找一个“麻雀虽小,五脏俱全”又是破解版的PS,也不是那么容易的 注:ps完整版不 ...
- Acrobat pro Dc 2018破解版|Adobe Acrobat pro Dc 2018中文破解版下载(附序列号/免破解)
Acrobat pro Dc 2018破解版是由Adobe公司开发的一款PDF编辑软件,它可以以PDF格式制作和保存用户的文档,以此方便浏览和打印,或使用更高级的功能,且PDF格式的文档可如实地保留原 ...
- IntelliJ IDEA 2018 破解过程[详细步骤](Mac OS & Windows)
注册码获取地址:http://idea.lanyus.com/ 1.软件下载 首先进入https://www.jetbrains.com官网进行下载. 2.下载破解包 可百度搜索JetbrainsCr ...
- PhpStorm 2018 破解方法
破解方法如下: 1.修改phpstrom的验证服务器地址. 在C:\Windows\System32\drivers\etc目录下打开hosts文件,并在文件最后加入 0.0.0.0 account. ...
- Good Bye 2018 (A~F, H)
目录 Codeforces 1091 A.New Year and the Christmas Ornament B.New Year and the Treasure Geolocation C.N ...
- IntelliJ IDEA 2018破解方法
1.下载idea:https://download.jetbrains.8686c.com/idea/ideaIU-2018.2.exe 2.安装idea 3.下载破解补丁:http://idea.l ...
- photoshop cc 2018破解补丁(pscc2018注册机) 附使用方法
1.下载破解程序 破解文件自行到 http://www.ddooo.com/softdown/109954.htm 下载 博主可以到本博客的文件-->pscc2018zcj_109954.ra ...
- 2018.09.27 网络协议(tarjan)
描述 一些学校连接在一个计算机网络上.学校之间存在软件支援协议.每个学校都有它应支援的学校名单(学校 a 支援学校 b ,并不表示学校 b 一定支援学校 a ).当某校获得一个新软件时,无论是直接得到 ...
随机推荐
- LeetCode148:Sort List
题目: Sort a linked list in O(n log n) time using constant space complexity. 解题思路: 根据题目要求,可知只能用归并排序,其他 ...
- SQL SERVER 2014--内存表实现秒杀场景
===================================== 网上针对“秒杀”的解决方案很多,数据拆分化解热点,READPAST解决锁问题,应用程序排队限制并发等等很多方式,各有优缺点, ...
- Windows2012 显示我的电脑删除群集
rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,0 在正常删除Cluster 节点之后,再添加节点时,报“节点已经加入群集”,无法加入,注册表信息删 ...
- docker 下载加速
执行这个命令: curl -SSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud. ...
- class字节码结构(四)(方法集合的结构)
<Java虚拟机原理图解>1.5. class文件中的方法表集合--method方法在class文件中是怎样组织的 一个类有多个方法,所以方法肯定是一个集合. 目标是: 1,了解方法在字节 ...
- JavaWeb 基础面试
1. 启动项目时如何实现不在链接里输入项目名就能启动? 修改Tomcat配置文件 server.xml.找到自己的项目配置 : <Context docBase="oneProjec ...
- POJ 2393
#include <iostream> #include <algorithm> using namespace std; int main() { //freopen(&qu ...
- POJ 2291
#include<iostream> #include<stdio.h> using namespace std; int compare(void const *,void ...
- 一元运算符 +,可用于将变量转换为数字;如果变量不能转换,它仍然会是一个数字,但值为 NaN (不是一个数字)
一元运算符,可用于将变量转换为数字: var y = "5"; var x = + y; console.log(typeof y);//string 类型 console.log ...
- Inno Setup入门(二十三)——Inno Setup类参考(9)
今天就简单说一下ProgressBar. TNewProgressBar = class(TWinControl) property Min: Longint; read write; pro ...