poj 3243 Clever Y&&hdu 2815 Mod Tree(扩展baby_step)
2014-08-12 10:40
507 查看
http://poj.org/problem?id=3243
求A^x = B(mod C)中的x ,C是任意的数。
框架都在这里了:http://hi.baidu.com/aekdycoin/item/236937318413c680c2cf29d4
理解:http://blog.csdn.net/ivan_zjj/article/details/7597109
求A^x = B(mod C)中的x ,C是任意的数。
框架都在这里了:http://hi.baidu.com/aekdycoin/item/236937318413c680c2cf29d4
理解:http://blog.csdn.net/ivan_zjj/article/details/7597109
#include <stdio.h> #include <iostream> #include <map> #include <set> #include <list> #include <stack> #include <vector> #include <math.h> #include <string.h> #include <queue> #include <string> #include <stdlib.h> #include <algorithm> #define LL long long #define _LL __int64 #define eps 1e-12 #define PI acos(-1.0) using namespace std; const int maxn = 99991; bool hash[maxn+10]; int idx[maxn+10]; LL val[maxn+10]; LL gcd(LL a, LL b) { if(b == 0) return a; return gcd(b,a%b); } void extend_gcd(LL a, LL b, LL &x, LL &y) { if(b == 0) { x = 1; y = 0; return; } extend_gcd(b,a%b,x,y); LL t = x; x = y; y = t-a/b*y; } void insert(int id, LL vv) { LL v = vv%maxn; while(hash[v] && val[v] != vv) { v++; if(v == maxn) v -= maxn; } if(hash[v] == false) { hash[v] = true; idx[v] = id; val[v] = vv; } } int found(LL vv) { LL v = vv%maxn; while(hash[v] && val[v] != vv) { v++; if(v == maxn) v -= maxn; } if(hash[v] == false) return -1; return idx[v]; } LL baby_step(LL A, LL B, LL C) { memset(hash,false,sizeof(hash)); memset(idx,-1,sizeof(idx)); memset(val,-1,sizeof(val)); //首先特判log(C)以内的解。因为下面消因子法解出来的x >= b,但有可能x的解是小于b的。所以先特判。 LL ans = 1; for(int i = 0; i <= 50; i++) { if(ans == B) return i; ans = ans*A%C; } //消因子,直到gcd(A,C) = 1,就可以用普通的baby_step LL D = 1%C,tmp; LL b = 0; while((tmp = gcd(A,C)) != 1) { if(B%tmp) return -1; b++; B = B/tmp; C = C/tmp; D = D*A/tmp%C; } LL M = ceil(sqrt(C*1.0)); LL k = 1; for(int i = 0; i < M; i++) { insert(i,k); k = k*A%C; } LL x,y; for(int i = 0; i < M; i++) { extend_gcd(D,C,x,y); x = x*B; x = (x%C+C)%C; int jj = found(x); if(jj != -1) return (LL)i*M+(LL)jj+b; D = D*k%C; } return -1; } int main() { LL A,B,C; while(~scanf("%lld %lld %lld",&A,&C,&B)) { if(A == 0 && B == 0 && C == 0) break; LL ans = baby_step(A,B,C); if(ans == -1) printf("No Solution\n"); else printf("%lld\n",ans); } return 0; }
相关文章推荐
- Hdu 2815 Mod Tree + Poj 3243 Clever Y 扩展Baby Step Giant Step 解决离散对数问题
- [zz]hdu 2815——Mod Tree【扩展Baby Step Giant Step解决离散对数问题】
- 扩展BABT STEP hdu 2815 poj 3243
- HDU 2815 Mod Tree【扩展Baby Step Giant Step解决离散对数问题】
- HDU 2815 Mod Tree 离散对数 扩展Baby Step Giant Step算法
- baby_step giant_step hdu 2815 mod tree pku 3243 Clever Y
- HDU 2815 Mod Tree (扩展 Baby Step Giant Step )
- 高次同余方程:poj 3243+poj 2417+hdu 2815 (Baby Step Giant Step 算法)
- POJ 3243 Clever Y (求解高次同余方程A^x=B(mod C) Baby Step Giant Step算法)
- poj 3243 Clever Y && 1467: Pku3243 clever Y【扩展BSGS】
- POJ 3243 Clever Y Extended-Baby-Step-Giant-Step
- hdu 2815 Mod Tree (数论题)(baby step giant step 小步大步)
- 【POJ 3243-Clever Y】 与【POJ 2417-Discrete Logging】(解高次同余方程 Baby-Step-Gaint-Step)
- HDU 2815 扩展baby step giant step 算法
- HDU 2815 Mod Tree 离散对数 扩张Baby Step Giant Step算法
- POJ 3243: Clever Y 求A^X = B (mod C) / BabyStep_GiantStep
- hdu 2815 Mod Tree 模方程a^x=b(mod n),n为任意正整数+模板题(扩展Baby Step Giant Step)
- poj 3243 Clever Y(Baby-Step Giant-Step)
- POJ 3243 Clever Y 求A^X = B (mod C) / BabyStep_GiantStep
- Baby Step Giant Step 及扩展 进一步解释补充 和 poj 3243