BSGS 模板
模板如下:
扩展版本:
求解a^k=b %p 求k,最小的k一定小于p,否则会重复,否则无解
***********************
gcd(a,p)=1时
设k=mi+v m=sqrt(p);
i,v<=m
a^v=b*(a^-m)^i %p
打表map for i=0~m-1 (a^i,i)
for i=0~m-1
check a^v存在
****************************
gcd(a,p)=t时
(A/t)^c*A^(x-c)=B/(t^c) %C/(t^c) c max
A^(x-c)=B/(t^c)*(A/t)^-c %C/(t^c)
const int NN = 99991 ; //sqrt(p)
int Hash[NN][2] ;
void insert(int id , LL vv){
LL v = vv % NN ;
while( Hash[v][0]!=-1 && Hash[v][1]!=vv){
v++ ; if(v == NN) v-=NN ;
}
if(Hash[v][0]==-1 ){
Hash[v][1] = vv ; Hash[v][0] = id ;
}
}
int find(LL vv){
LL v = vv % NN ;
while( Hash[v][0]!=-1 && Hash[v][1]!=vv){
v++ ;if(v == NN) v-=NN ;
}
if( Hash[v][0]==-1 ) return -1;
return Hash[v][0] ;
}
void ex_gcd(LL a , LL b , LL& x , LL& y){
if(b == 0){
x = 1 ; y = 0 ;
return ;
}
ex_gcd(b , a%b , x, y) ;
LL t = x ;
x = y;
y = t - a/b*y ;
}
LL baby_step(LL A, LL B , LL C){ //A^x=B %C 最小x,__gcd g++使用
LL D=1 % C ,d=0;
if(__gcd(A,C)!=1){
LL ans = 1 ;
for(LL i=0;i<=50;i++){
if(ans == B) return i ;
ans = ans * A % C ;
}
LL tmp ;
while( (tmp=__gcd(A,C)) != 1 ){
if(B % tmp) return -1 ;
d++ ;
B/=tmp ;
C/=tmp ;
D = D*A/tmp%C ;
} //D*A^(x-d)=B %C
} //printf("D=%lld A=%lld B=%lld C=%lld d=%lld\n",D,A,B,C,d);
memn(Hash);
LL M = ceil( sqrt(C*1.0) ) ;
LL rr = 1 ;
for(int i=0;i<M;i++){
insert(i, rr) ;
rr = rr * A % C ;
} //rr=A^M
LL jj,x,y;
for(int i=0;i<M;i++){
ex_gcd(D, C , x, y) ;
jj = find( (x * B % C+C)%C ) ; //printf("f %lld\n",r);
if(jj != -1){
return i*M+jj+d;
}
D = D * rr % C ;
}
return -1 ;
}
-1无解 sqrt(p)
=---------------------------------------
普通版本
//POJ 2417
//baby_step giant_step
// a^x = b (mod n) n为素数,a,b < n
// 求解上式 0 <= x < n的解
#include <cmath>
#include <cstdio>
#include <cstring>
#define MOD 76543
using namespace std;
int hs[MOD], head[MOD], next[MOD], id[MOD], top;
void insert(int x, int y)
{
int k = x % MOD;
hs[top] = x;
id[top] = y;
next[top] = head[k];
head[k] = top++;
}
int find(int x)
{
int k = x % MOD;
for (int i = head[k]; i != -1; i = next[i])
if (hs[i] == x)
return id[i];
return -1;
}
int BSGS(int a, int b, int n)
{
memset(head, -1, sizeof(head));
top = 1;
if (b == 1)
return 0;
int m = sqrt(n * 1.0), j;
long long x = 1, p = 1;
for (int i = 0; i < m; i++, p = p * a % n)
insert(p * b % n, i);
for (long long i = m; ; i += m)
{
if ((j = find(x = x * p % n)) != -1)
return i - j;
if (i > n)
break;
}
return -1;
}
int main()
{
int a, b, n;
while (~scanf("%d%d%d", &n, &a, &b))
{
int ans = BSGS(a, b, n);
if (ans == -1)
printf("no solution\n");
else
printf("%d\n", ans);
}
}
BSGS 模板的更多相关文章
- 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的最小正整数解 解法: 注意到如 ...
- BSGS模板(慢速)
//author Eterna #define Hello the_cruel_world! #pragma GCC optimize(2) #include<iostream> #inc ...
- 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 ...
- BZOJ3239Discrete Logging——BSGS
题目大意:给出$P,B,N$,求最小的正整数$L$,使$B^L\equiv N(mod\ P)$. $BSGS$模板题. #include<set> #include<map> ...
随机推荐
- 仿阿里云后台管理界面模板html源码——后台
链接:http://pan.baidu.com/s/1nuH2SPj 密码:ar8o
- perl6 单线程破解phpmyadmin脚本
use HTTP::UserAgent; my $ua = HTTP::UserAgent.new; my $url = 'http://localhost/phpMyAdmin/index.php' ...
- go标识符、变量、常量
标识符 标识符是用来表示Go中的变量名或者函数名,以字母或_开头.后可跟着字母.数字. _ 关键字 关键字是Go语言预先定义好的,有特殊含义的标识符. 变量 1. 语法:var identifier ...
- fastDFS 命令笔记【转】
端口开放 这是命令运行的前提 iptables -I INPUT -p tcp -m state –state NEW -m tcp –dport 22 -j ACCEPT iptables -I I ...
- URAL题解三
URAL题解三 URAL 1045 题目描述:有\(n\)个机场,\(n-1\)条航线,任意两个机场有且只有一种方案联通.现有两个恐怖分子从\(m\)号机场出发,第一个人在机场安装炸弹,乘坐飞机,引爆 ...
- MAC 文件被锁定
从windows拷贝到MAC的文件,有时候会被锁定.右键-简介-已锁定也是灰色的,无法取消: xattr -l 文件名 xattr -d com.apple.FinderInfo 文件名
- What does “=>” mean in import in scala?(转自StackOverflow问答)
As others have mentioned, it's an import rename. There is however one further feature that proves ...
- 设计模式(一)工厂模式Factory(创建型)(转)
原文链接:http://blog.csdn.net/hguisu/article/details/7505909 设计模式一 工厂模式Factory 在面向对象编程中, 最通常的方法是一个new操作符 ...
- leetcode 之Search in Rotated Sorted Array(四)
描述 Follow up for ”Search in Rotated Sorted Array”: What if duplicates are allowed? Would this aff ...
- 洛谷P1094纪念品分组 题解
题目传送门 首先的思路就是贪心.先将所有的纪念品按照价格从低到高进行排序.在分别从左到右.从右到左合并纪念品.如果两端纪念品价格超过了上上限,那么就将较大的那一个纪念品独自放入.否则将两个纪念品一起放 ...