BSGS模板(慢速)
//author Eterna
#define Hello the_cruel_world!
#pragma GCC optimize(2)
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<utility>
#include<cmath>
#include<climits>
#include<deque>
#include<functional>
#include<complex>
#include<numeric>
#include<unordered_map>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define Pi acos(-1.0)
#define ABS(x) ((x) >= 0 ? (x) : (-(x)))
#define pb(x) push_back(x)
#define lowbit(x) (x & -x)
#define FRIN freopen("C:\\Users\\Administrator.MACHENI-KA32LTP\\Desktop\\in.txt", "r", stdin)
#define FROUT freopen("C:\\Users\\Administrator.MACHENI-KA32LTP\\Desktop\\out.txt", "w", stdout)
#define FAST ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define outd(x) printf("%d\n", x)
#define outld(x) printf("%lld\n", x)
#define il inline
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int maxn = 1e5;
const int INF = 0x7fffffff;
const int mod = 1e9 + 7;
const double eps = 1e-7;
inline int read_int() {
char c;
int ret = 0, sgn = 1;
do { c = getchar(); } while ((c < '0' || c > '9') && c != '-');
if (c == '-') sgn = -1; else ret = c - '0';
while ((c = getchar()) >= '0' && c <= '9') ret = ret * 10 + (c - '0');
return sgn * ret;
}
inline ll read_ll() {
char c;
ll ret = 0, sgn = 1;
do { c = getchar(); } while ((c < '0' || c > '9') && c != '-');
if (c == '-') sgn = -1; else ret = c - '0';
while ((c = getchar()) >= '0' && c <= '9') ret = ret * 10 + (c - '0');
return sgn * ret;
}
inline ll Quick_pow(ll a, ll b, int p) {
ll res = 1;
while (b) {
if (b & 1) (res *= a) %= p;
(a *= a) %= p;
b >>= 1;
}
return res;
}
ll extend_gcd(ll a, ll b, ll& x, ll& y) {
ll d = a;
if (b != 0) {
d = extend_gcd(b, a % b, y, x);
y -= (a / b) * x;
}
else x = 1, y = 0;
return d;
}
map<ll, int> mp;
int BSGS(ll y, ll z, ll p) {
if (y == 0 && z == 0) return 1;
if (y == 0 && z != 0) return -1;
int m = sqrt(p);
mp.clear();
int t = z % p;
mp[t] = 0;
for (int j = 1; j <= m; j++) {
t = t*y%p;
mp[t] = j;
}
t = 1;
ll mi = Quick_pow(y, m, p), x;
for (int i = 1; i*i <= p + 1; i++) {
t = t * mi % p;
if (mp.count(t)) {
x = i*m - mp[t];
return (x % p + p) % p;
}
}
return -1;
};
BSGS模板(慢速)的更多相关文章
- [算法模板]FFT-快速傅里叶变换
[算法模板]FFT-快速傅里叶变换 感谢ZYW聚聚为我们讲解FFT~ 思路 我懒,思路和证明部分直接贴链接: rvalue LSJ-FFT与NTT基础 代码 主要思想是利用了单位根特殊的性质(n次单位 ...
- BSGS 模板
模板如下: 扩展版本: 求解a^k=b %p 求k,最小的k一定小于p,否则会重复,否则无解 *********************** gcd(a,p)=1时 设k=mi+v m=sqrt(p) ...
- Bsgs模板
模板最主要的是自己看得舒服,不会给自己留隐患,调起来比较简单,板子有得是,最主要的是改造出适合你的那一套. ——mzz #include<bits/stdc++ ...
- bzoj2242,洛谷2485----SDOI2011计算器(exgcd,qsm,bsgs模板)
就是一道模板题! 这里再强调一下 BSGS 考虑方程\(a^x = b \pmod p\) 已知a,b,p\((2 \le p\le 10^9)\),其中p为质数,求x的最小正整数解 解法: 注意到如 ...
- bzoj 2242 [SDOI2011]计算器——BSGS模板
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2242 第一道BSGS! 咳咳,我到底改了些什么?…… 感觉和自己的第一版写的差不多……可能是 ...
- 【Luogu】P2485计算器(快速幂,exgcd和Bsgs模板)
题目链接 题目描述非常直接,要求你用快速幂解决第一问,exgcd解决第二问,bsgs解决第三问. emmmm于是现学bsgs 第二问让求最小整数解好烦啊…… 假设我们要求得方程$ax+by=c(mod ...
- 2019牛客多校第五场C generator 2 hash,bsgs模板
generator 2 题意 给出\(x_0,a,b,p\),有方程\(x_i\equiv (a*x_{i-1}+b)(\% p)\),求最小的i,使得\(x_i=v\),不存在输出-1 分析 经过公 ...
- U9249 【模板】BSGS
题目描述 给定a,b,p,求最小的非负整数x 满足a^x≡b(mod p) 若无解 请输出“orz” 输入输出格式 输入格式: 三个整数,分别为a,b,p 输出格式: 满足条件的非负整数x 输入输出样 ...
- 【BSGS】BZOJ3239 Discrete Logging
3239: Discrete Logging Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 729 Solved: 485[Submit][Statu ...
随机推荐
- ssh hibernate修改数据库
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in rea ...
- Perl调用外部命令(其他脚本、系统命令)的方法和区别
1. `command`; 使用反引号调用外部命令能够捕获其标准输出,并按行返回且每行结束处附带一个回车.反引号中的变量在编译时会被内插为其值. 2. open LIST "ls -l| ...
- [codechef]SnackDown 2017 Online Elimination Round Prefix XOR
预处理后主席树维护 首先得出最后的答案为 \(\sum_{i=l}^{r}{min(right[i],r)-i+1}\) \(ri[i]\)表示i最远的上升序列(即代码中的f[i]) step1 那么 ...
- python requests用法总结
requests是一个很实用的Python HTTP客户端库,编写爬虫和测试服务器响应数据时经常会用到.可以说,Requests 完全满足如今网络的需求 本文全部来源于官方文档 http://docs ...
- [python](windows)分布式进程问题:pickle模块不能序列化lambda函数
运行错误:_pickle.PicklingError: Can't pickle <function <lambda> at 0x000002BAAEF12F28>: attr ...
- Asp.net core 学习笔记 ( Smtp and Razor template 电子邮件和 Razor 模板 )
refer : https://dotnetcoretutorials.com/2017/08/20/sending-email-net-core-2-0/ https://ppolyzos.com/ ...
- 安卓MVP框架
一.理解MVP 原文地址 我的Demo 效果图: 项目结构: 实现 一.Model层 首先定义一个实体类User package app.qcu.pmit.cn.mvpdemo.model; /** ...
- 『计算机视觉』Mask-RCNN_训练网络其一:数据集与Dataset类
Github地址:Mask_RCNN 『计算机视觉』Mask-RCNN_论文学习 『计算机视觉』Mask-RCNN_项目文档翻译 『计算机视觉』Mask-RCNN_推断网络其一:总览 『计算机视觉』M ...
- JavaScript基础知识点总结
JavaScript----js 全栈课程-------全栈工程师 前端:信息展示,普通用户浏览---前端工程师(html+css:静态页面 js:页面的交互效果) 后台:信息管理,管理员,数据库-- ...
- CentOS卸载通过yum安装的软件
以erlang为例:rpa -qa|grep erlang 使用:yum -y remove erlang-* 扩展--查看yum安装软件的路径:rpm -ql erlang-cosFileTrans ...