[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 ).当某校获得一个新软件时,无论是直接得到 ...
随机推荐
- Tomcat跨域访问配置
下载cors-filter-1.7.jar,java-property-utils-1.9.jar这两个库文件,放到Tomcat的lib目录下. D:\Program Files\Tomcat-7.0 ...
- [ACM_模拟] UVA 12504 Updating a Dictionary [字符串处理 字典增加、减少、改变问题]
Updating a Dictionary In this problem, a dictionary is collection of key-value pairs, where keys ...
- Spring Boot 2 实践记录之 使用 ConfigurationProperties 注解将配置属性匹配至配置类的属性
在 Spring Boot 2 实践记录之 条件装配 一文中,曾经使用 Condition 类的 ConditionContext 参数获取了配置文件中的配置属性.但那是因为 Spring 提供了将上 ...
- C# 图像自动切换
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- hdu 5500 Reorder the Books
http://acm.hdu.edu.cn/showproblem.php?pid=5500 Reorder the Books Time Limit: 4000/2000 MS (Java/Othe ...
- jQuery基础笔记(5)
day56 参考:https://www.cnblogs.com/liwenzhou/p/8178806.html#autoid-1-9-5 文档处理 添加到指定元素内部的后面 $(A).append ...
- ansible 的第一次亲密接触
如何添加一台机器 编辑 /etc/ansible/hosts 添加本机的 public ssh key 到目标机器的 authorized_keys 添加本机的 私钥 到 ansible 运行 ans ...
- leetcode-162-寻找峰值
题目描述: 峰值元素是指其值大于左右相邻值的元素. 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引. 数组可能包含多个峰值,在这种情况下,返回任何一 ...
- bootstrap-validator
使用bootstrap-validator挺多的,虽然自己写认证并不复杂,我一向喜欢现成的控件,原因是风格一致,不容易出错. 这个是接口文档:http://bv.doc.javake.cn/setti ...
- WPF快速实现XML可视化编辑工具
虽然最近业余时间主要都放在研究AngularJS上了,不过由于正好要帮朋友做一个生成XML的小工具,顺便又温顾了一下WPF.虽然这个时代相对于Web应用和移动App,Windows应用程序是越来越少了 ...