Matrix_code

math - GaussMod

Jul 13th, 2017 (edited)
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. const int MOD = 2;
  2. // Gaussian Modular Solution. 0 based Indexing, Complexity: eq^2 * var
  3. struct GaussMod{
  4.     typedef long long  T;
  5.     void show(vector< vector<T> > A){
  6.         for(int i = 0 ; i < A.size(); i ++ ) {
  7.             for(int j = 0; j < A[i].size(); j ++ ) printf("%4lld",A[i][j]);
  8.             printf("\n");
  9.         }
  10.     }
  11.     long long extended_euclid(long long a, long long b, long long &x, long long &y) {
  12.         long long xx = y = 0;
  13.         long long yy = x = 1;
  14.         while (b) {
  15.             long long q = a / b;
  16.             long long t = b; b = a%b; a = t;
  17.             t = xx; xx = x - q*xx; x = t;
  18.             t = yy; yy = y - q*yy; y = t;
  19.         }
  20.         return a;
  21.     }
  22.     long long mod(long long a, long long b) {
  23.         return ((a%b) + b) % b;
  24.     }
  25.     // computes b such that ab = 1 (mod n), returns -1 on failure
  26.     long long inv(long long a,long long n)
  27.     {
  28.         long long x, y;
  29.         long long g = extended_euclid(a, n, x, y);
  30.         if (g > 1) return -1;
  31.         return mod(x, n);
  32.  
  33.     }
  34.  
  35.  
  36.     int gauss(vector< vector<T> >& A) {
  37.         int m = A.size(), n = A[0].size() - 1;
  38.         int i = 0, j = 0;
  39.         while (i < m && j < n) {
  40.             int r = i;
  41.             for (int k = i; k < m; k++) {
  42.                 if (A[k][j]) {
  43.                     r = k;
  44.                     break;
  45.                 }
  46.             }
  47.             if (r != i) for (int k = 0; k <= n; k++) swap(A[i][k], A[r][k]);
  48.             if (A[i][j] == 0) {
  49.                 j++;
  50.                 continue;
  51.             }
  52.  
  53.             for (int k = 0; k < m; k++) {
  54.                 if (A[k][j] == 0 || i == k) continue;
  55.                 long long f = A[k][j] * inv(A[i][j], MOD ) % MOD ;
  56.  
  57.                 for (int t = j; t <= n; t++) A[k][t] = ((A[k][t] - f * A[i][t]) % MOD  + MOD ) % MOD ;
  58.             }
  59.  
  60.  
  61.             i++;
  62.         }
  63.         for (int k = i; k < m; k++) if (A[k][n]) return 0; // no solution
  64.         if (i < n) return 2; // many solution
  65.         return 1; // unique solution
  66.     }
  67.  
  68.     vector< pair<int,T > > FindSolution(vector< vector<T> > A) // returns solution (x,v) form, x=0,Multiple,1-unique. v=root
  69.     {
  70.  
  71.         vector<pair<int,T> > res(A[0].size() - 1, mp(0,-1));
  72.         for(int i = 0; i < A.size(); i ++ ) {
  73.             for(int j = 0; j < A[i].size() - 1; j ++ ) {
  74.                 if(A[i][j]) {
  75.                     res[j] = make_pair(1, A[i].back());
  76.                     break;
  77.                 }
  78.             }
  79.         }
  80.         return res;
  81.     }
  82. };
Add Comment
Please, Sign In to add comment