【算法乱讲】BSGS
Description
Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 <= N < P, compute the discrete logarithm of N, base B, modulo P. That is, find an integer L such that BL== N (mod P)
Input
Read several lines of input, each containing P,B,N separated by a space.
Output
For each line print the logarithm on a separate line. If there are several, print the smallest; if there is none, print "no solution".
Sample Input
5 2 1
5 2 2
5 2 3
5 2 4
5 3 1
5 3 2
5 3 3
5 3 4
5 4 1
5 4 2
5 4 3
5 4 4
12345701 2 1111111
1111111121 65537 1111111111
Sample Output
0
1
3
2
0
3
1
2
0
no solution
no solution
1
9584351
462803587 显然,这是一道bsgs的裸题
那么bsgs是什么玩意呢,
我们先玩一玩式子
令 m=ceil(sqrt(p))设a^(m*i+j)=b(mod p) 显然 a^j*a^(m*i)=b(mod p)
a^j=b*a^(-m*i) (mod p) 因此,我们预处理所有可能的a^j丢进哈希表中然后我们枚举i,
看看有没有可能对应的j所以我们的算法时间复杂度为O(n^0.5)
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<map>
#include<vector>
#include<set>
#define il inline
#define re register
using namespace std;
typedef long long ll;
struct hash_set{
ll v[];
int next[],g[],w[],tot;
il void clear(){
memset(g,false,sizeof(g));tot=;
}
il void insert(ll h,int f){
v[++tot]=h;
w[tot]=f;
next[tot]=g[h%];
g[h%]=tot;
}
il int find(ll h){
for(re int i=g[h%];i;i=next[i])
if(h==v[i]) return w[i];
return -;
}
} p;
ll A,B,P,m,t,s;
il ll ksm(re ll base,re ll pow){
if(pow<){
cout<<"-1";exit();
}
ll ans=;
for(;pow;pow>>=){
if(pow&) ans=ans*base%P;
base=base*base%P;
}
return ans;
}
il ll rev(re ll a){
return ksm(a,P-);
}
il void init(){
p.clear();
m=ceil(sqrt(P));t=;
for(int i=;i<m;i++){
if(p.find(t)<) p.insert(t,i);
t=t*A%P;
}
//cout<<endl;
for(int i=,l;i<=P/m;i++){
t=rev(ksm(A,m*i));
// cout<<t<<" "<<m*i<<" ";
s=t*B%P;
// cout<<s<<endl;
l=p.find(s);
if(l>=){
printf("%lld\n",m*i+l);
return;
}
}
printf("no solution\n");
}
int main(){
while(scanf("%lld%lld%lld",&P,&A,&B)!=EOF){
init();
}
return ;
}
【算法乱讲】BSGS的更多相关文章
- 学了两天 react,乱讲一下学习思路,顺便弄了一个脚手架
之前一直用 vue 做一些小项目,最近接触了一个项目是用 react 做前端,虽然本身是做后端开发的,但是前端还是要了解一点的. 现在的项目基本上都是前后端分离的,后端就先不提了.前端的框架也是层出不 ...
- javascript洗牌算法 乱序算法 面试题
1.2种方案代码 <!DOCTYPE html> <html lang="zh"> <head> <meta charset=" ...
- 多项式&生成函数(~~乱讲~~)
多项式 多项式乘法 FFT,NTT,MTT不是前置知识吗?随便学一下就好了(虽然我到现在还是不会MTT,exlucas也不会用) FTT总结 NTT总结 泰勒展开 如果一个多项式\(f(x)\)在\( ...
- KMP算法细讲(豁然开朗)
一.KMP算法是如何针对传统算法修改的 用模式串P去匹配字符串S,在i=6,j=4时发生失配: ---------------------------------------------------- ...
- 【模板】【数论】二次剩余Cipolla算法,离散对数BSGS 算法
Cipolla LL ksm(LL k,LL n) { LL s=1; for(;n;n>>=1,k=k*k%mo) if(n&1) s=s*k%mo; return s; } n ...
- 「算法笔记」BSGS 与 exBSGS
一.离散对数 给定 \(a,b,m\),存在一个 \(x\),使得 \(\displaystyle a^x\equiv b\pmod m\) 则称 \(x\) 为 \(b\) 在模 \(m\) 意义下 ...
- BSGS算法学习笔记
从这里开始 离散对数和BSGS算法 扩展BSGS算法 离散对数和BSGS算法 设$x$是最小的非负整数使得$a^{x}\equiv b\ \ \ \pmod{m}$,则$x$是$b$以$a$为底的离散 ...
- 大数据时代:基于微软案例数据库数据挖掘知识点总结(Microsoft 聚类分析算法)
原文:(原创)大数据时代:基于微软案例数据库数据挖掘知识点总结(Microsoft 聚类分析算法) 本篇文章主要是继续上一篇Microsoft决策树分析算法后,采用另外一种分析算法对目标顾客群体的挖掘 ...
- ELFhash - 优秀的字符串哈希算法
ELFhash - 优秀的字符串哈希算法 2016年10月29日 22:12:37 阅读数:6440更多 个人分类: 算法杂论算法精讲数据结构 所属专栏: 算法与数据结构 版权声明:本文为博主原创 ...
随机推荐
- python2 - 列表
列表 a = [1,2,3,4,5,6,7] a[0:4:1]//正向索引 a[-1:-2:-1]//反向索引 列表添加 a = [1, 2] b = [3, 4] +:a + b//把a和b连接,重 ...
- 180716-Centos时区设置
使用timedatectl命令同步时间并设置时区 I. timedatactl命令 1. 使用帮助 timedatectl -h 2. 命令示例 2.1 显示系统的当前时间和日期 timedatect ...
- netty初认识
Netty是什么? 本质:JBoss做的一个Jar包 目的:快速开发高性能.高可靠性的网络服务器和客户端程序 优点:提供异步的.事件驱动的网络应用程序框架和工具 通俗的说:一个好使的处理Socket的 ...
- 带你玩转JavaScript中的隐式强制类型转换
正题开始前我想先抛出一个问题,==和===有什么区别?可能一般人会想,不就是后者除了比较值相等之外还会比较类型是否相等嘛,有什么好问的,谁不知道?!但是这样说还不够准确,两者的真正区别其实是==在比较 ...
- preg_replace 以及弃用的e
preg_replace (PHP 4, PHP 5) preg_replace — 执行一个正则表达式的搜索和替换 说明¶ mixed preg_replace ( mixed $pattern , ...
- Live Archive 训练题 2019/3/9
7454 Parentheses A bracket is a punctuation mark, which is used in matched pairs, usually used withi ...
- scrum立会报告+燃尽图(第三周第五次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2286 项目地址:https://coding.net/u/wuyy694 ...
- 随机生成30道四则运算-NEW
补充:紧跟上一个随机生成30道四则运算的题目,做了一点补充,可以有真分数之间的运算,于是需要在原来的基础上做一些改进. 首先指出上一个程序中的几个不足:1.每次执行的结果都一样,所以不能每天给孩子出3 ...
- web会员注册页面代码(4)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Swift-setValuesForKeysWithDictionary
重写 setValuesForKeysWithDictionary 那么字典中可以有的字段在类中没有对应属性 class Person : NSObject { var age :Int = // 重 ...