https://www.cnblogs.com/sdzwyq/p/9900650.html

模板:

unordered_map<int, int> mp;
LL q_pow(LL n, LL k, LL p) {
LL ans = 1;
if(k == -1) return 0;
while(k) {
if(k&1) ans = (ans*n) % p;
n = (n*n) % p;
k >>= 1;
}
return ans;
}
int BSGS(int a, int b, int p){
int m = sqrt(p)+1, s = b;
mp.clear();
for(int i = 0; i < m; ++i){
mp[s]=i;
s= (s*1LL*a)%p;
}
int t = q_pow(a, m, p);
s = 1;
for(int i = 1; i <= m; ++i){
s = (s*1LL*t)%p;
if(mp.find(s) != mp.end()) return i*m-mp[s];
}
return -1;
}
int exBSGS(int a, int b, int p) {
int d = __gcd(a, p), k = 1, t = 0;
while(d^1){
if(b%d) return -1;
++t;
b /= d;
p /= d;
k = (k*1LL*(a/d)) % p;
if(b == k) return t;
d = __gcd(a, p);
}
int s = b;
mp.clear();
int m = sqrt(p) + 1;
for(int i = 0;i < m; ++i){
mp[s] = i;
s = (s*1LL*a) % p;
}
s = k;
k = q_pow(a, m, p);
for(int i = 1; i <= m; ++i){
s = (s*1LL*k) % p;
if(mp.find(s) != mp.end()) return i*m-mp[s]+t;
}
return -1;
}

例题:P4195 【模板】exBSGS/Spoj3105 Mod

代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define y1 y11
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define pdd pair<double, double>
#define mem(a, b) memset(a, b, sizeof(a))
#define debug(x) cerr << #x << " = " << x << "\n";
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//head unordered_map<int, int> mp;
LL q_pow(LL n, LL k, LL p) {
LL ans = 1;
if(k == -1) return 0;
while(k) {
if(k&1) ans = (ans*n) % p;
n = (n*n) % p;
k >>= 1;
}
return ans;
}
int exBSGS(int a, int b, int p) {
int d = __gcd(a, p), k = 1, t = 0;
while(d^1){
if(b%d) return -1;
++t;
b /= d;
p /= d;
k = (k*1LL*(a/d)) % p;
if(b == k) return t;
d = __gcd(a, p);
}
int s = b;
mp.clear();
int m = sqrt(p) + 1;
for(int i = 0;i < m; ++i){
mp[s] = i;
s = (s*1LL*a) % p;
}
s = k;
k = q_pow(a, m, p);
for(int i = 1; i <= m; ++i){
s = (s*1LL*k) % p;
if(mp.find(s) != mp.end()) return i*m-mp[s]+t;
}
return -1;
}
int a, b, p;
int main() {
while(~scanf("%d %d %d", &a, &p, &b)) {
if(!a && !b && !p) break;
if(b == 1) {
if(p == 1) printf("No Solution\n");
else printf("0\n");
}
else {
int x = exBSGS(a, b, p);
if(~x) printf("%d\n", x);
else printf("No Solution\n");
}
}
return 0;
}

算法笔记--BSGS && exBSGS 模板的更多相关文章

  1. 算法笔记--sg函数详解及其模板

    算法笔记 参考资料:https://wenku.baidu.com/view/25540742a8956bec0975e3a8.html sg函数大神详解:http://blog.csdn.net/l ...

  2. opencv笔记4:模板运算和常见滤波操作

    time:2015年10月04日 星期日 00时00分27秒 # opencv笔记4:模板运算和常见滤波操作 这一篇主要是学习模板运算,了解各种模板运算的运算过程和分类,理论方面主要参考<图像工 ...

  3. BSGS&EXBSGS 大手拉小手,大步小步走

    大步小步走算法处理这样的问题: A^x = B (mod C) 求满足条件的最小的x(可能无解) 其中,A/B/C都可以是很大的数(long long以内) 先分类考虑一下: 当(A,C)==1 即A ...

  4. 算法笔记--数位dp

    算法笔记 这个博客写的不错:http://blog.csdn.net/wust_zzwh/article/details/52100392 数位dp的精髓是不同情况下sta变量的设置. 模板: ]; ...

  5. 算法笔记--lca倍增算法

    算法笔记 模板: vector<int>g[N]; vector<int>edge[N]; ][N]; int deep[N]; int h[N]; void dfs(int ...

  6. [置顶] 小白学习KM算法详细总结--附上模板题hdu2255

    KM算法是基于匈牙利算法求最大或最小权值的完备匹配 关于KM不知道看了多久,每次都不能完全理解,今天花了很久的时间做个总结,归纳以及结合别人的总结给出自己的理解,希望自己以后来看能一目了然,也希望对刚 ...

  7. 学习Java 以及对几大基本排序算法(对算法笔记书的研究)的一些学习总结(Java对算法的实现持续更新中)

    Java排序一,冒泡排序! 刚刚开始学习Java,但是比较有兴趣研究算法.最近看了一本算法笔记,刚开始只是打算随便看看,但是发现这本书非常不错,尤其是对排序算法,以及哈希函数的一些解释,让我非常的感兴 ...

  8. 算法笔记--STL中的各种遍历及查找(待增)

    算法笔记 map: map<string,int> m; map<string,int>::iterator it;//auto it it = m.begin(); whil ...

  9. 算法笔记--priority_queue

    算法笔记 priority_queue<int>que;//默认大顶堆 或者写作:priority_queue<int,vector<int>,less<int&g ...

随机推荐

  1. 性能测试loadrunner11工具再也不用担心浏览器兼容的问题了(目前试过的各版本浏览器都是成功的)

    工具:Loadrunner 11.0+Fiddler+浏览器(谷歌.火狐.IE等) 步骤一:查看抓包工具Fiddler对应的端口 1.打开Fiddler------工具------选项-----连接, ...

  2. 李宗盛spss罚写2019-12-8

    以上过程即整个假设检验的思想:反证法及小概率原理. 因而假设检验有可能犯两类错误. 第一类错误:原假设正确,而错误地拒绝了它,即“拒真”的错误,其发生的概率为第一类错误的概率. 第二类错误:原假设不正 ...

  3. js 数组去重、去空(收藏)

    function unique (arr) { return Array.from(new Set(arr)) } var arr = [1,1,'true','true',true,true,15, ...

  4. JAVA第09次实验(IO流)

    JAVA第09次实验(IO流) 0.字节流与二进制文件 我的代码 import java.io.DataInputStream; import java.io.DataOutputStream; im ...

  5. activate-power-mode安装与设置

    Window-->activate-power-mode-->去掉combo/shake,其他三个全勾上,现在用起来就很爽了,赶紧体验吧.

  6. [转帖]Linux修改时区的正确方法

    Linux修改时区的正确方法 /etc/localtime 以及timedatectl 两种方式修改时区. CentOS和Ubuntu的时区文件是/etc/localtime,但是在CentOS7以后 ...

  7. 在Ubuntu中安装了MongoDB后无法启动mongod的问题

    今天准备学习MongoDB,没想到下载之后服务器端启动不了,记录一下问题和处理过程 一.安装 在Ubuntu中安装还是很简单,直接:sudo apt install mongodb 二.启动 启动Mo ...

  8. TypeScript 枚举

    我们常常会有这样的场景,比如与后端开发约定订单的状态开始是0,未结账是1,运输中是2,运输完成是3,已收货是4.这样的纯数字会使得代码缺乏可读性.枚举就用于这样的场景.枚举可以让我们定义一些名字有意义 ...

  9. java多线程上篇(三) -- 进程通信和线程死锁简单介绍

    进程通信指的是进程间的信息交换 ,IPC(Inter-Process Communication,进程间通信) 进程通信就相当于一种工作方式.沟通形式,进程通信主要指的就是操作系统提供的进程通信工具( ...

  10. Word 查找替换高级玩法系列之 -- 替换手机号中间几位数字

    1.打开"查找和替换"对话框.切换到"开始"选项卡,在"编辑"组中选择"替换".或者按下快捷键"Ctrl+H& ...