模板如下:

扩展版本:
求解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 模板的更多相关文章

  1. Bsgs模板

    模板最主要的是自己看得舒服,不会给自己留隐患,调起来比较简单,板子有得是,最主要的是改造出适合你的那一套.                  ——mzz #include<bits/stdc++ ...

  2. bzoj2242,洛谷2485----SDOI2011计算器(exgcd,qsm,bsgs模板)

    就是一道模板题! 这里再强调一下 BSGS 考虑方程\(a^x = b \pmod p\) 已知a,b,p\((2 \le p\le 10^9)\),其中p为质数,求x的最小正整数解 解法: 注意到如 ...

  3. BSGS模板(慢速)

    //author Eterna #define Hello the_cruel_world! #pragma GCC optimize(2) #include<iostream> #inc ...

  4. bzoj 2242 [SDOI2011]计算器——BSGS模板

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2242 第一道BSGS! 咳咳,我到底改了些什么?…… 感觉和自己的第一版写的差不多……可能是 ...

  5. 【Luogu】P2485计算器(快速幂,exgcd和Bsgs模板)

    题目链接 题目描述非常直接,要求你用快速幂解决第一问,exgcd解决第二问,bsgs解决第三问. emmmm于是现学bsgs 第二问让求最小整数解好烦啊…… 假设我们要求得方程$ax+by=c(mod ...

  6. 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 分析 经过公 ...

  7. U9249 【模板】BSGS

    题目描述 给定a,b,p,求最小的非负整数x 满足a^x≡b(mod p) 若无解 请输出“orz” 输入输出格式 输入格式: 三个整数,分别为a,b,p 输出格式: 满足条件的非负整数x 输入输出样 ...

  8. 【BSGS】BZOJ3239 Discrete Logging

    3239: Discrete Logging Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 729  Solved: 485[Submit][Statu ...

  9. BZOJ3239Discrete Logging——BSGS

    题目大意:给出$P,B,N$,求最小的正整数$L$,使$B^L\equiv N(mod\ P)$. $BSGS$模板题. #include<set> #include<map> ...

随机推荐

  1. JSON.parse()——json字符串转JS

    JSON 通常用于与服务端交换数据. 在接收服务器数据时一般是字符串. 我们可以使用 JSON.parse() 方法将数据转换为 JavaScript 对象. 语法 JSON.parse(text[, ...

  2. Linux下查看进程占用内存的最好方式

    今天看到stackoverflow上关于linux下如何查看某个进程占用的内存是多少的回答,觉得非常棒,不过是全英文的,很多人可能看不懂,所以我翻译一下 翻译自http://stackoverflow ...

  3. 判断Selenium加载完成

    How do you make Selenium 2.0 wait for the page to load? You can also check pageloaded using followin ...

  4. openjudge-NOI 2.6-2000 最长公共子上升序列

    题目链接:http://noi.openjudge.cn/ch0206/2000/ 题解: 裸题,不解释(题目有毒) #include<cstdio> #include<algori ...

  5. PGSql

    http://www.yiibai.com/postgresql/ http://www.postgresql.org/ http://blog.csdn.net/wulex/article/deta ...

  6. Hibernate根据配置文件,生成建表语句

    import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; public class ...

  7. golang之结构体和方法

    结构体的定义 结构体是将零个或者多个任意类型的命令变量组合在一起的聚合数据类型.每个变量都叫做结构体的成员. 其实简单理解,Go语言的结构体struct和其他语言的类class有相等的地位,但是GO语 ...

  8. Merge k Sorted Lists——分治与堆排序(需要好好看)

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 1 ...

  9. Linux Shell基础篇——变量

    一.Shell中的变量 注:这里所说的Shell是Bash Shell,我姑且统称为Shell. Shell中的变量分为用户自定义变量.环境变量.位置参数变量.预定义变量.在Shell中,变量的默认类 ...

  10. python中的any和all函数

    any和all函数是判断一组数据真假性的综合结果.以下摘选自Stackoverflow. ------------------ 分割线开始 ----------------- any any will ...