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的更多相关文章

  1. 学了两天 react,乱讲一下学习思路,顺便弄了一个脚手架

    之前一直用 vue 做一些小项目,最近接触了一个项目是用 react 做前端,虽然本身是做后端开发的,但是前端还是要了解一点的. 现在的项目基本上都是前后端分离的,后端就先不提了.前端的框架也是层出不 ...

  2. javascript洗牌算法 乱序算法 面试题

    1.2种方案代码 <!DOCTYPE html> <html lang="zh"> <head> <meta charset=" ...

  3. 多项式&生成函数(~~乱讲~~)

    多项式 多项式乘法 FFT,NTT,MTT不是前置知识吗?随便学一下就好了(虽然我到现在还是不会MTT,exlucas也不会用) FTT总结 NTT总结 泰勒展开 如果一个多项式\(f(x)\)在\( ...

  4. KMP算法细讲(豁然开朗)

    一.KMP算法是如何针对传统算法修改的 用模式串P去匹配字符串S,在i=6,j=4时发生失配: ---------------------------------------------------- ...

  5. 【模板】【数论】二次剩余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 ...

  6. 「算法笔记」BSGS 与 exBSGS

    一.离散对数 给定 \(a,b,m\),存在一个 \(x\),使得 \(\displaystyle a^x\equiv b\pmod m\) 则称 \(x\) 为 \(b\) 在模 \(m\) 意义下 ...

  7. BSGS算法学习笔记

    从这里开始 离散对数和BSGS算法 扩展BSGS算法 离散对数和BSGS算法 设$x$是最小的非负整数使得$a^{x}\equiv b\ \ \ \pmod{m}$,则$x$是$b$以$a$为底的离散 ...

  8. 大数据时代:基于微软案例数据库数据挖掘知识点总结(Microsoft 聚类分析算法)

    原文:(原创)大数据时代:基于微软案例数据库数据挖掘知识点总结(Microsoft 聚类分析算法) 本篇文章主要是继续上一篇Microsoft决策树分析算法后,采用另外一种分析算法对目标顾客群体的挖掘 ...

  9. ELFhash - 优秀的字符串哈希算法

    ELFhash - 优秀的字符串哈希算法 2016年10月29日 22:12:37 阅读数:6440更多 个人分类: 算法杂论算法精讲数据结构 所属专栏: 算法与数据结构   版权声明:本文为博主原创 ...

随机推荐

  1. 408. Add Binary【LintCode java】

    Description Given two binary strings, return their sum (also a binary string). Example a = 11 b = 1 ...

  2. CMDBuild2.4.3安装配置

    参考文档: 官网:http://www.cmdbuild.org/en 参考:http://blog.csdn.net/shawn210/article/details/70230248 本文涉及CM ...

  3. Spring Bean注册解析(一)

           Spring是通过IoC容器对Bean进行管理的,而Bean的初始化主要分为两个过程:Bean的注册和Bean实例化.Bean的注册主要是指Spring通过读取配置文件获取各个bean的 ...

  4. python3【基础】-文件操作

    1. python对文件操作流程: 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件操作 关闭文件 现有如下文件: 昨夜寒蛩不住鸣. 惊回千里梦,已三更. 起来独自绕阶行. 人悄悄,帘外月胧明. ...

  5. ios程序后台继续运行

    1.图标右上角显示消息个数 if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) { UIUserNotificati ...

  6. spark作用流程

    原文:https://www.cnblogs.com/asura7969/p/8441471.html https://blog.csdn.net/xu__cg/article/details/700 ...

  7. Java JVM多线程

  8. WebService(一)

    1.简介 Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用 ...

  9. 【beta】Scrum站立会议第6次....11.8

    小组名称:nice! 组长:李权 成员:于淼  刘芳芳韩媛媛 宫丽君 项目内容:约跑app(约吧) 时间:2016.11.8    12:00——12:30 地点:传媒西楼220室 本次对beta阶段 ...

  10. Centos 7 环境下,如何使用 Apache 实现 SSL 虚拟主机 双向认证 的详细教程:

    1. testing ! ... 1 1 原文参考链接: http://showerlee.blog.51cto.com/2047005/1266712 很久没有更新LAMP的相关文档了,刚好最近单位 ...