嘟嘟嘟




这不就是个bsgs板儿嘛。

顺便就复习了一下bsgs和哈希表。

头一次觉得我的博客这么好用,一下就懂了:数论学习笔记之高次不定方程




这里再补充几点:

1.关于这一段代码:

int S = sqrt(c), p = 1;
for(int i = 0; i < S; ++i)
{
if(p == b) return i + cnt;
insert(1LL * p * b % c, i);
p = 1LL * p * a % c;
}
for(int i = S, q = p; i - S + 1 < c; i += S)
{
int t = query(q);
if(~t) return i - t + cnt;
q = 1LL * q * p % c;
}

这一段是bsgs里最核心的部分。

首先是为什么\(q\)每一次乘以\(p\):第一层循环后,\(p\)就成为了\(a ^ m\),这样就相当于\(a ^ {i * m}\)。

然后是第二层循环的终止条件为什么是这个:大家都知道,这一段代码是枚举\(a ^ {tm}\)的,只不过用\(i\)代替了\(tm\),所以我们考虑\(i\)的最大值:\(tm - y < c \Rightarrow i - (m - 1) < c \Rightarrow i - m + 1 < c\) 。这就出来了。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int base = 99979;
const int maxe = 1e6 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
} int g, P, n; In ll quickpow(ll a, ll b, ll mod)
{
ll ret = 1;
for(; b; b >>= 1, a = a * a % mod)
if(b & 1) ret = ret * a % mod;
return ret;
}
In ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
In void exgcd(ll a, ll b, ll& x, ll& y)
{
if(!b) x = 1, y = 0;
else exgcd(b, a % b, y, x), y -= a / b * x;
} In ll inv(ll a, ll mod)
{
ll x, y;
exgcd(a, mod, x, y);
return x;
} struct Hash
{
int nxt, to, w;
}e[maxe];
int head[base], ecnt = 0;
int st[base], top = 0;
In void init()
{
ecnt = 0;
while(top) head[st[top--]] = 0;
}
In void insert(int x, int y)
{
int h = x % base;
if(!head[h]) st[++top] = h;
e[++ecnt] = (Hash){head[h], x, y};
head[h] = ecnt;
}
In int query(int x)
{
int h = x % base;
for(int i = head[h]; i; i = e[i].nxt)
if(e[i].to == x) return e[i].w;
return -1;
} In int bsgs(int a, int b, int c)
{
int Gcd, d = 1, cnt = 0;
while((Gcd = gcd(a, c)) ^ 1)
{
if(b % Gcd) return -1;
++cnt; b /= Gcd, c /= Gcd;
d = 1LL * d * (a / Gcd) % c;
}
b = 1LL * b * inv(d, c) % c;
init();
int S = sqrt(c), p = 1;
for(int i = 0; i < S; ++i)
{
if(p == b) return i + cnt;
insert(1LL * p * b % c, i);
p = 1LL * p * a % c;
}
for(int i = S, q = p; i - S + 1 < c; i += S)
{
int t = query(q);
if(~t) return i - t + cnt;
q = 1LL * q * p % c;
}
return -1;
} int main()
{
g = read(), P = read(), n = read();
for(int i = 1; i <= n; ++i)
{
int A = read(), B = read();
write(quickpow(g, 1LL * bsgs(g, A, P) * bsgs(g, B, P) , P)), enter;
}
return 0;
}

[CQOI2018]破解D-H协议的更多相关文章

  1. BZOJ_5296_[Cqoi2018]破解D-H协议_BSGS

    BZOJ_5296_[Cqoi2018]破解D-H协议_BSGS Description Diffie-Hellman密钥交换协议是一种简单有效的密钥交换方法.它可以让通讯双方在没有事先约定密钥(密码 ...

  2. BZOJ5296 CQOI2018 破解D-H协议 【BSGS】

    BZOJ5296 CQOI2018Day1T1 破解D-H协议 Description Diffie-Hellman密钥交换协议是一种简单有效的密钥交换方法.它可以让通讯双方在没有事先约定密钥(密码) ...

  3. BZOJ5296 [CQOI2018] 破解D-H协议 【数学】【BSGS】

    题目分析: 裸题. 代码: #include<bits/stdc++.h> using namespace std; typedef long long ll; ; #define mp ...

  4. 2018.12.18 bzoj5296: [Cqoi2018]破解D-H协议(bsgs)

    传送门 bsgsbsgsbsgs基础题. 考虑到给的是原根,因此没无解的情况. 于是只需要每次把a,ba,ba,b解出来. 然后可以通过预处理节省一部分时间. 代码: #include<bits ...

  5. LG4454 【[CQOI2018]破解D-H协议】

    先谈一下BSGS算法(传送门) 但是上面这位的程序实现比较繁琐,看下面这位的. clover_hxy这样说 bsgs算法,又称大小步算法(某大神称拔山盖世算法). 主要用来解决 A^x=B(mod C ...

  6. BZOJ 5296: [Cqoi2018]破解D-H协议(BSGS)

    传送门 解题思路 \(BSGS\)裸题??要求的是\(g^a =A (mod\) \(p)\),设\(m\)为\(\sqrt p\),那么可以设\(a=i*m-j\),式子变成 \[ g^{i*m-j ...

  7. P4454 [CQOI2018]破解D-H协议

    链接 这题并不难只是需要把题读懂 - By ShadderLeave 一句话题意 给定两个数 \(p\)和\(g\),有\(t\)组询问,每组询问给出\(A\)和\(B\) 其中 A = \(g^a ...

  8. 破解使用SMB协议的Windows用户密码:acccheck

    一.工作原理 Acccheck是一款针对微软的SMB协议的探测工具(字典破解用户名和密码),本身不具有漏洞利用的能力. SMB协议:SMB(Server Message Block)通信协议主要是作为 ...

  9. Linux 利用hosts.deny 防止暴力破解ssh(转)

    一.ssh暴力破解 利用专业的破解程序,配合密码字典.登陆用户名,尝试登陆服务器,来进行破解密码,此方法,虽慢,但却很有效果. 二.暴力破解演示 2.1.基础环境:2台linux主机(centos 7 ...

随机推荐

  1. JAVA JVM常见内存参数配置简析

    JVM常见内存参数配置简析   常见参数 -Xms .-Xmx.-XX:newSize.-XX:MaxnewSize.-Xmn(-XX:newSize.-XX:MaxnewSize) 简析 1.-Xm ...

  2. 怎么打开在.bashrc文件以及设置颜色

    打开/etc/bashrc,加入如下一行:   alias ls="ls --color"   下次启动bash时就可以像在Slackware里那样显示彩色的目录列表了,其中不同颜 ...

  3. 快速排序 and 拉格朗日插值查找

    private static void QuictSort(int[] zu, int left, int right) { if (left < right) { ; ; ]; while ( ...

  4. JVM 垃圾回收机制

    首先JVM的内存结构包括五大区域: 程序计数器.虚拟机栈.本地方法栈.方法区.堆区.其中程序计数器.虚拟机栈和本地方法栈3个区域随线程启动与销毁, 因此这几个区域的内存分配和回收都具有确定性,不需要过 ...

  5. 2.对于所有对象都通用的方法_EJ

    第8条: 覆盖equals时请遵守通用约定 我们在覆盖equals方法时,必须遵守它的通用约定: 1.自反性.对于任何非null的引用值x,x.equals(x)必须返回true: 2.对称性.对于任 ...

  6. css控制文字自动换行

    自动换行问题,正常字符的换行是比较合理的,而连续的数字和英文字符常常将容器撑大,挺让人头疼,下面介绍的是CSS如何实现换 行的方法 对于div,p等块级元素正常文字的换行(亚洲文字和非亚洲文字)元素拥 ...

  7. jQuery效果之雪花飘落

    实现思路 1.在一定的频率下在页面中生成一定数目的雪花从上往下飘落: 2.在指定的时间内飘落后移除页面: 3.可设置雪花的大小,在一定范围内随机雪花大小: 4.什么时间后清除生成雪花,停止函数. js ...

  8. 人工智能改进传统云ERP的10种方法

    http://blog.itpub.net/31542119/viewspace-2168809/ 随着数字化转型的进程加快,企业开始重新评估ERP的作用.传统ERP经过多年僵硬化定制过于追求生产的一 ...

  9. 小程序实践(一):主页tab选项实现

    官方文档 效果图: 实现底部Tab选项,只需要在项目根目录下的app.json下修改 如图: ----------------------------------------------------- ...

  10. 微信小程序 text属性设置 WXSS样式

    >微信小程序的布局css样式 参考自  珺L 文字 width: fit-content;font-size:20px;      /*设置文字字号*/color:red;           ...