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 ...
随机推荐
- Qt基础学习---滑动条之QSlider
Qt滑动条基本用法: //mydialog.h #ifndef MYDIALOG_H #define MYDIALOG_H #include <QDialog> class QLineEd ...
- ActiveMQ组件使用方法
由于组件使用了spring,故需要相关的spring包及配置 首先,需要加载对应的jar包 然后,编写调用类 package com.demo.testSpring; import com.jfina ...
- DOMContentLoaded方法
document.addEventListener('DOMContentLoaded',function(){ alert("SSDD") },false);
- C# 创建对象的方法
1.实例化方法,也就是new(): //where T:new () 表示T必须有构造函数 public static T Create<T> where T:new () { retur ...
- Pyhon中运算符的使用
1. a & b python中的&延续了C/C++的含义,表示位运算. 例如 3 & 4:3&5:6&7 3 & 4 = (011)2 & ( ...
- AngularJS +Kendo UI Grid template
var dataSource = new kendo.data.DataSource({ transport: { dataType: "json", read: inputUri ...
- 2015-10-07 jQuery2
jQuery (2) 四. 过滤选择器 1. $("input[type='button']").val(“中国”) //所有设置type=button的input,其val ...
- OSSIM安装使用教程(OSSIM-5.6.5)
一.说明 1.1 相关概念说明 SEM,security event management,安全事件管理,指对事件进行实时监控,收集信息差展生通知和告警的行为. SIM,security inform ...
- python-模块2
from collections import namedtuple # # 类 # p = namedtuple("Point", ["x", "y ...
- Android : 跟我学Binder --- (4) 驱动情景分析
目录: Android : 跟我学Binder --- (1) 什么是Binder IPC?为何要使用Binder机制? Android : 跟我学Binder --- (2) AIDL分析及手动实现 ...