1. 程式人生 > >大小步(同餘方程的解)

大小步(同餘方程的解)

大小步BigStepGiantStep演算法求 A^x = B( mod P ) (注:P為質數)中x的解

//來自kuangbin的ACM模板

#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; } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int a,b,n; while(scanf("%d%d%d",&n,&a,&b) == 3) { int ans = BSGS(a,b,n); if(ans == -1)printf("no solution\n"); else printf("%d\n",ans); } return 0; }
擴充套件歐幾里得演算法求 A*x = B(mod P) (注:P為質數和合數都可以)中x的解(逆元)
擴充套件歐幾里得演算法求 x = ai % mi ( i = 1 ... n ) 中x的解(模線性方程組的解)
高斯消元求 a1*x1 + ai*xi ... + an*xn = bi%mod 中xi的解( i = 1...n)