Portal: http://www.lydsy.com/JudgeOnline/problem.php?id=4128
A flood problem together
Using the STRIDE step algorithm, the operation of the number into the matrix is good.
Matrix inversion? So the basis of the line-generation algorithm I do not want to say, or self-baidu it
It is important to note that matrices do not have a commutative law, so do not confuse the sequence when calculating $b\cdot a^{-m}$.
Code:
1#include <cstring>2#include <cstdio>3#include <algorithm>4#include <map>5#include <cmath>6 7 using namespacestd;8 Const intMAXN = -;9 Ten intn, p; One A structMatrix { - intA[MAXN][MAXN]; - int*operator[](intidx) { the returnA[idx]; - } - Const int*operator[](intIDX)Const { - returnA[idx]; + } - BOOL operator< (ConstMatrix &RHS)Const{//for map + for(inti =0; I < n; ++i) A for(intj =0; J < N; ++j) at if(A[i][j] < rhs[i][j])return true; - Else if(A[i][j] > Rhs[i][j])return false; - return false; - } - voidUnit () {//fill in the N-Order unit matrix -Memset (A,0,sizeof(a)); in for(inti =0; I < n; ++i) A[i][i] =1; - } to voidClear () {//Complete 0 +Memset (A,0,sizeof(a)); - } the }; *Matrix A, B;//given A/b matrix in the title $ Panax Notoginseng intPow_mod (intAintb) {//integer Fast Power - intAns =1; the while(b) { + if(b&1) ans = ans * a%p; AA = a * a%p; theb >>=1; + } - returnans; $ } $ intInvintA) {//Inverse integer negation - returnPow_mod (a,p-2); - } the voidInput_matrix (Matrix &A) { - for(inti =0; I < n; ++i)Wuyi for(intj =0; J < N; ++j) { thescanf"%d", &a[i][j]); -A[I][J]%=p; Wu } - } About voidMulConstMatrix &a,ConstMatrix &b, Matrix &c) {//matrix multiplication, results deposited in C $ Matrix tmp; Tmp.clear (); - for(inti =0; I < n; ++i) - for(intj =0; J < N; ++j) - for(intK =0; K < n; ++k) ATMP[I][J] = (Tmp[i][j] + a[i][k] * B[k][j]% p)%p; +C =tmp; the } - voidGauss_jordan (inta[maxn][maxn<<1] ) {//The inverse of Gausio when eliminating Yuan $ for(inti =0; I < n; ++i) { the intR; the for(r = i; r < N; + +)R) the if(A[r][i]) Break; the if(r! =i) - for(intj = i; J < (n<<1); ++j) in swap (A[r][j], a[i][j]); the intIV =INV (a[i][i]); the for(intj = i; J < (n<<1); ++j) AboutA[I][J] = a[i][j] * IV%p; the for(intj =0; J < N; ++j) the if(J! = i &&A[j][i]) { the intTMP = (P-a[j][i])%p; + for(intK = i; K < (n<<1); ++k) -A[j][k] = (A[j][k] + a[i][k] * tmp% p)%p; the }Bayi } the } the voidInvConstMatrix &a, Matrix &b) {//matrix inversion, results deposited b - inttmp[maxn][maxn<<1] = {0}; - for(inti =0; I < n; ++i) the for(intj =0; J < N; ++j) theTMP[I][J] =A[i][j]; the for(inti =0; I < n; ++i) Tmp[i][i+n] =1; the Gauss_jordan (TMP); - for(inti =0; I < n; ++i) the for(intj =0; J < N; ++j) theB[I][J] = tmp[i][j+n]; the }94 theMap<matrix,int>MP; the intBSGS (Matrix A, matrix B) {//Step -by-step algorithm the intm = sqrt (p) +1;98 Matrix E; E.unit (); About for(inti =0; I < m; ++i) { - if(!mp.count (E)) mp[e] =i;101 Mul (e,a,e);102 }103 INV (e,e);104 for(inti =0; I < P; i + =m) { the if(Mp.count (B))returni +Mp[b];106Mul (B,E,B);//Note Matrix multiplication order107 }108 return-1;109 } the 111 intMain () { thescanf"%d%d", &n, &p);113 Input_matrix (A), Input_matrix (B); theprintf"%d\n", Bsgs (A, b)); the return 0; the}
"Solving" matrix Bzoj 4128 matrices inverse discrete logarithm step-by-step algorithm