跳过正文
  1. Posts/

BSGS(Baby steps giant steps)算法学习笔记

·2 分钟

离散对数(Discrete Logarithm)问题是这样一个问题,它是对于模方程

a^x=b(mod prime),求满足条件的X,或者得出不存在这样的X

最暴力的思路,那么就是枚举x? 根据费马小定理,只需要枚举[0,p-1)

但是还是很大…我们不禁想到把x写成x=A*m+B的形式,m=ceil(sqrt(p))

因此有

 a^{A\lceil \sqrt{p} \rceil + B} \equiv b \pmod p
,变形得到
 a^{A\lceil \sqrt{p} \rceil} \equiv b\cdot a^{-B} \pmod p

然后预处理一边存到map中,从小到大枚举另一边看是否存在…

我们可以设 

x = A \lceil \sqrt{p} \rceil - B
,其中 
0 \leq B < \lceil \sqrt{p} \rceil
,  
0 < A \leq \lceil \sqrt{p} \rceil + 1
,这样的话化简后的方程就是

 a^{A\lceil \sqrt{p} \rceil} \equiv b\cdot a^B \pmod p

就可以不用求出逆元,要注意只是不用求出逆元,而不是没有用到逆元的存在

就可以不用求出逆元,要注意只是不用求出逆元,而不是没有用到逆元的存在

就可以不用求出逆元,要注意只是不用求出逆元,而不是没有用到逆元的存在

其实在m=sqrt(p)的时候你可能就有预感了…

BSGS算法的本质,就是个分块啊,而分块的本质就是暴力乱搞…所以BSGS看起来很高大上的算法不过是暴力乱搞2333

而BSGS的名字也很贴切…A的变化是giant step?B的变化是baby step? (纯属yy…但是我感觉这样想很好理解啊?

需要注意的是,这里介绍的是常规的BSGS算法,

前提条件是a和P互质

前提条件是a和P互质

前提条件是a和P互质

放一个板子好了,poj 2417

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2017年07月23日 星期日 11时11分00秒
 4File Name :2417.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define PB push_back
20#define fst first
21#define sec second
22#define lson l,m,rt<<1
23#define rson m+1,r,rt<<1|1
24#define ms(a,x) memset(a,x,sizeof(a))
25typedef long long LL;
26#define pi pair < int ,int >
27#define MP make_pair
28
29using namespace std;
30const double eps = 1E-8;
31const int dx4[4]={1,0,0,-1};
32const int dy4[4]={0,-1,1,0};
33const int inf = 0x3f3f3f3f;
34LL p,b,n;
35map<LL,LL>Hash;
36map<LL,LL>::iterator it;
37inline LL ksm(LL a,LL b,LL MOD)
38{
39    LL res = 1LL;
40    while (b)
41    {
42	if (b&1) res = (res*a)%MOD;
43	b = b >> 1;
44	a = (a*a)%MOD;
45    }
46    return res;
47}
48LL BSGS(LL a,LL b ,LL p) // a^x = b (mod p),求x
49{
50    a%=p;
51    b%=p;
52    if (!a&&!b) return 1;
53    if (!a) return -1;
54    Hash.clear();
55    LL m = ceil(sqrt(double(p)));
56    LL tmp = b;
57    for (LL j = 0 ; j <= m ; j++)
58    {
59	Hash[tmp]=j;
60	tmp = (tmp*a)%p;
61    }
62    tmp = ksm(a,m,p);
63    LL ret = 1;
64
65    for (LL i = 1  ; i <= m+1 ; i++)
66    {
67	ret = ret*tmp%p;
68	if (Hash[ret]) return i*m-Hash[ret]; //注意处理下%....虽然其实不处理也没关系...
69    }
70    return -1;
71
72}
73int main()
74{
75        #ifndef  ONLINE_JUDGE
76       // freopen("./in.txt","r",stdin);
77  #endif
78	while (~scanf("%lld%lld%lld",&p,&b,&n))   // B^L = n(mod p)
79	{
80	    LL ans = BSGS(b,n,p);
81	    if (ans==-1) printf("no solution\n");
82	    else printf("%lld\n",(ans%p+p)%p);
83	}
84
85
86  #ifndef ONLINE_JUDGE
87  fclose(stdin);
88  #endif
89    return 0;
90}

参考资料:

一个很多BSGS算法初学者的误区

扩展大步小步法解决离散对数问题

大步小步算法与扩展大步小步算法

BSGS算法学习小记(大步小步算法)

相关文章

poj 2417 Discrete Logging (BSGS算法)

·2 分钟
题目链接 题意: Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 <= N < P, compute the discrete logarithm of N, base B, modulo P. That is, find an integer L such that BL == N (mod P)

bzoj2002: [Hnoi2010]Bounce 弹飞绵羊 (分块)

·1 分钟
http://www.lydsy.com/JudgeOnline/problem.php?id=2002 题意+思路: 同codeforces 13 E holes. 1/* *********************************************** 2Author :111qqz 3Created Time :2016年02月21日 星期日 02时29分39秒 4File Name :code/bzoj/2002.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <ctime> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33const int N=2E5+7; 34int n,m; 35int siz = 450; //sqrt(2E5) 36int a[N]; 37int pos[N]; 38int cnt[N]; 39int nxt[N]; 40int end[N]; 41 42 43void go( int x) 44{ 45 int ans = 0 ; 46 while (1) 47 { 48 if (x>=n) 49 { 50 printf("%d\n",ans); 51 break; 52 } 53 ans +=cnt[x]; 54 x = nxt[x]; 55// cout<<"nxt[x]:"<<nxt[x]<<endl; 56 } 57} 58void update ( int i,int j) 59{ 60 if (j>=n) 61 { 62 cnt[i]=1; 63 nxt[i]=n; 64 end[i]=i; 65 } 66 else 67 { 68 if (pos[i]==pos[j]) 69 { 70 cnt[i] = cnt[j] + 1; 71 nxt[i] = nxt[j]; 72 end[i]=end[j]; 73 } 74 else 75 { 76 cnt[i] = 1; 77 nxt[i] = j; 78 end[i] = end[j]; 79 } 80 } 81} 82int main() 83{ 84 #ifndef ONLINE_JUDGE 85 freopen("code/in.txt","r",stdin); 86 #endif 87 88 scanf("%d",&n); 89 for ( int i = 0 ; i < n ;i++) 90 { 91 scanf("%d",&a[i]); 92 pos[i] = i/siz; 93 } 94 95 for ( int i = n - 1 ; i >= 0 ; i--) update(i,i+a[i]); 96 97 scanf("%d",&m); 98 while (m--) 99 { 100 int opt; 101 scanf("%d",&opt); 102 if (opt==1) 103 { 104 int x; 105 scanf("%d",&x); 106 go(x); 107 } 108 else 109 { 110 int x,y; 111 scanf("%d %d",&x,&y); 112 a[x]=y; 113 update(x,x+a[x]); 114 int p = pos[x]*siz; 115 for ( int i = x-1 ; i >= p ; i--) update(i,i+a[i]); 116 } 117 } 118 119 #ifndef ONLINE_JUDGE 120 fclose(stdin); 121 #endif 122 return 0; 123}

codeforces 13 E. Holes (分块)

·2 分钟
http://codeforces.com/problemset/problem/13/E 题意:给你n个洞,进入某个洞后会跑到另一个洞,到了另一个洞之后又可能会继续到下一个洞,问你从一个洞进去,钻了几个洞才会出来,在哪个洞出来

hdu 4638 Group

·2 分钟
http://acm.hdu.edu.cn/showproblem.php?pid=4638 题意:给定一个序列,序列由1-N个元素全排列而成,求任意区间连续的段数。例如序列2,3,5,6,9就是三段(2, 3) (5, 6)(9)。 思路:增加一个元素,如果它两边的元素都出现了,那么段数-1(相当于把两段连接起来合并成了一段),如果两边元素都没有出现,那么段数+1.反过来,减少一个元素时,如果两边元素都出现了,俺么段数+1(相当于把完整的一段断开成两段),如果两边元素都没有出现,那么段数-1.操作可以O(1)完成。。。上莫队。 因为id大小最大才100000,所以判断某个元素是否出现开一个100000大小的布尔数组即可(我竟然傻逼得去用set….然后华丽丽得TLE了2333)