Baby Step Giant Step model
2015-03-25 22:15
309 查看
******************************************** */ #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; //baby_step giant_step // a^x = b (mod n) n为素数,a,b < n // 求解上式 0<=x < n的解 #define MOD 76543 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; }
相关文章推荐
- POJ 2417 Discrete Logging ( Baby step giant step )
- poj 3243 Clever Y(Baby-Step Giant-Step)
- 【POJ2417】baby step giant step
- POJ 3243 Clever Y (求解高次同余方程A^x=B(mod C) Baby Step Giant Step算法)
- Baby-step giant-step 解高次同余方程a^x=b(mod n) 模版
- 【扩展Baby Step Giant Step解决离散对数问题】(转自AC神牛。。。。)
- HDU 2815 Mod Tree (扩展 Baby Step Giant Step )
- poj_2417 (baby_step,giant_step算法)
- hdu——2815(数论之Baby Step Giant Step解决离散对数问题)
- 高次同余笔记(二):extended-baby-step-giant-step算法
- 每日小结2 baby-step_giant_step
- Baby step Giant step算法
- 高次同余方程(Baby-Step Giant-Step)
- POJ 2417 Discrete Logging (baby_step,giant_step算法)
- POJ 2417:Discrete Logging——Baby Step, Giant Step
- bzoj 3283: 运算器 扩展Baby Step Giant Step && 快速阶乘
- POJ 2417 Discrete Logging (Baby-Step Giant-Step)
- 高次同余方程:poj 3243+poj 2417+hdu 2815 (Baby Step Giant Step 算法)
- poj 2417 Discrete Logging 数论baby_step,giant_step算法
- poj2417 Baby-StepGiant-StepAlgorithm a^x=b%P