poj 2417 baby_step giant_step 高次同余方程
2017-04-15 16:53
411 查看
题解链接 :
点击打开链接
code :
点击打开链接
code :
#include <cstdio> #include <cmath> #include <algorithm> using namespace std; typedef long long ll; const int N = (1 << 16) + 10; struct R{ ll v, id; bool operator < (const R &rhs) const{ return v == rhs.v ? id < rhs.id : v < rhs.v; } }Bj ; ll p, b, n; ll FastPowMod(ll a, ll b){ ll ret = 1; while(b){ if(b & 1) ret = ret * a % p; a = a * a % p; b >>= 1; } return ret; } int find(int r, ll v){ int l = 0, h = r - 1; while(l <= h){ int m = (l + h) >> 1; if(Bj[m].v == v) return Bj[m].id; if(Bj[m].v < v) l = m + 1; else h = m - 1; } return -1; } int main(){ //freopen("in.txt", "r", stdin); while(~scanf("%lld%lld%lld", &p, &b, &n)){ ll m = ceil(sqrt((double)p - 1)); for(int i = 0; i < m; ++i){ Bj[i].id = i; Bj[i].v = i ? Bj[i - 1].v * b % p : 1; } sort(Bj, Bj + m); ll pre = -1; int cnt = 0; for(int i = 0; i < m; ++i) if(Bj[i].v != pre) Bj[cnt++] = Bj[i], pre = Bj[i].v; ll tmp = FastPowMod(b, p - 1 - m); ll R = n; int ans = -1; for(int i = 0; i < m; ++i){ /**if(n == 4){ printf("%lld : ", R); for(int i = 0; i < cnt; ++i) printf("%lld、", Bj[i].v); puts(""); }*/ int pos = find(cnt, R); //if(n == 4) printf("%d\n", pos); if(~pos){ ans = i * m + pos; break; } R = R * tmp % p; } if(~ans) printf("%lld\n", ans); else puts("no solution"); } return 0; }
相关文章推荐
- POJ 2417 Discrete Logging(高次同余方程-Baby-Step,Giant-Step)
- poj 2417 Discrete Logging 数论baby_step,giant_step算法
- POJ 2417:Discrete Logging——Baby Step, Giant Step
- POJ 2417 Discrete Logging ( Baby step giant step )
- POJ - 2417 Discrete Logging(Baby-Step Giant-Step)
- poj 2417 Discrete Logging 求解模方程a^x=b(mod n),n为素数+模板题(baby_step giant_step)
- POJ 2417 Discrete Logging (baby_step,giant_step算法)
- poj_2417 (baby_step,giant_step算法)
- poj 2417 && poj3243(Baby-Step Giant-Step)
- poj 2417 && poj3243(Baby-Step Giant-Step)
- 【POJ 3243-Clever Y】 与【POJ 2417-Discrete Logging】(解高次同余方程 Baby-Step-Gaint-Step)
- POJ 2417 Discrete Logging (Baby-Step Giant-Step)
- POJ 3243 Clever Y (求解高次同余方程A^x=B(mod C) Baby Step Giant Step算法)
- POJ 2417 baby_step giant_step 小步大步算法 a^x == b(mod n) 求解0<=x<n的值
- Poj 2417 Discrete Logging (Baby Step Giant Step 解 a^x = b (mod n) n为素数)
- POJ 2417 Discrete Logging (Baby Step Giant Step算法)
- 高次同余方程:poj 3243+poj 2417+hdu 2815 (Baby Step Giant Step 算法)
- POJ 2417 Discrete Logging (Baby-Step-Gaint-Step)
- Hdu 2815 Mod Tree + Poj 3243 Clever Y 扩展Baby Step Giant Step 解决离散对数问题
- 【poj3243-Clever Y】高次同余方程-拓展BabyStepGiantStep