矩阵快速幂 HDU 2157 HDU 5015 POJ 3233

HDU 2157:

#include 

using namespace std;

const int MOD = 1000;
const int MAXN = 108;
int n, m;

struct Matrix {
  int m[MAXN][MAXN];
  
  friend Matrix operator * (Matrix &a, Matrix &b) {
    Matrix ans;
    for(int i = 0; i < n; ++i) {
      for(int j = 0; j < n; ++j) {
        int tmp = 0;
        for(int k = 0; k < n; ++k) {
          tmp = (tmp + a.m[i][k] * b.m[k][j] % MOD) % MOD;
        }
        ans.m[i][j] = tmp;
      }
    }
    return ans;
  }

};

Matrix quickPow(Matrix a, int p) {
  Matrix ans;
  for(int i = 0; i < n; ++i) {
    for(int j = 0; j < n; ++j) {
      ans.m[i][j] = 0;
    }
    ans.m[i][i] = 1;
  }
  while(p) {
    if(p & 1) {
      ans = ans * a;
    }
    a = a * a;
    p >>= 1;
  }
  return ans;
}

int main() {
    while(scanf("%d %d", &n, &m) != EOF) {
      if(n == m && n == 0) {
        break;
      }
      Matrix M;   
      for(int i = 0; i < n; ++i) {
        for(int j = 0; j < n; ++j) {
          M.m[i][j] = 0;
        }
        M.m[i][i] = 0;   // m[i][i] = 0 in relation matrix in a graph 
      }
      int a, b;
      while(m--) {
        scanf("%d %d", &a, &b);
        M.m[a][b] = 1;  
      }
  
      int T;
      scanf("%d", &T);
      while(T--) {
        int A, B, K;
        scanf("%d %d %d", &A, &B, &K);
        Matrix mm = quickPow(M, K);
        printf("%d\n", mm.m[A][B] % MOD);
      }
    }
  	return 0;
}


HDU 5015:

#include 

using namespace std;
const long long MOD = 10000007;
const int MAXN = 27;

int n, c;
long long a[MAXN];

struct Matrix {
  long long m[MAXN][MAXN];
  friend Matrix operator * (Matrix &a, Matrix &b) {
    Matrix pro;
    for(int i = 0; i <= n + 1; ++i) {
      for(int j = 0; j <= n + 1; ++j) {
        pro.m[i][j] = 0;
        for(int k = 0; k <= n + 1; ++k) {
          if(a.m[i][k] && b.m[k][j])
            pro.m[i][j] = (pro.m[i][j] + a.m[i][k] * b.m[k][j] % MOD) % MOD;
        }
      }
    }
    return pro;
  }
};

Matrix quickPow(Matrix M, int p) {
  Matrix pro;
  for(int i = 0; i <= n + 1; ++i) {
    for(int j = 0; j <= n + 1; ++j) {
      pro.m[i][j] = 0;
    }
    pro.m[i][i] = 1;
  }             
  while(p) {
    if(p & 1) {
      pro = pro * M;
    }
    M = M * M;
    p >>= 1;
  }
  return pro;
}

int main() {
  while(scanf("%d %d", &n, &c) != EOF) {
    Matrix M;
    memset(M.m, 0, sizeof(M.m));
    a[0] = 23;
    a[n+1] = 3;
    for(int i = 1; i <= n; ++i) {
      scanf("%lld", &a[i]);
    }
    for(int i = 0; i <= n; ++i) {
      M.m[i][0] = 10;
      M.m[i][n+1] = 1;
    }
    M.m[n+1][n+1] = 1;
    for(int i = 1; i < n + 1; ++i) {
      for(int j = 1; j <= i; ++j) {
        M.m[i][j] = 1;
      }
    }
    Matrix MM = quickPow(M, c);
    long long ans = 0;
    for(int i = 0; i <= n+1; ++i) {
      ans = (ans + (MM.m[n][i] * a[i]) % MOD) % MOD;
    }
    printf("%lld\n", ans);
  }
  return 0;
}


POJ 3233:

#include 
#include 
#include 

using namespace std;
int MOD;
const int MAXN = 35;
int n, k;

struct Matrix {
  long long m[MAXN][MAXN];
  friend Matrix operator + (Matrix &a, Matrix &b) {
    Matrix sum;
    for(int i = 0; i < n; ++i) {
      for(int j = 0; j < n; ++j) {
        sum.m[i][j] = (a.m[i][j] + b.m[i][j]) % MOD;
      }
    }
    return sum;
  }
  friend Matrix operator * (Matrix &a, Matrix &b) {
    Matrix pro;
    for(int i = 0; i < n; ++i) {
      for(int j = 0; j < n; ++j) {
        pro.m[i][j] = 0;
        for(int k = 0; k < n; ++k) {
          pro.m[i][j] = (pro.m[i][j] + a.m[i][k] * b.m[k][j] % MOD) % MOD;
        }
      }
    }
    return pro;
  }
};

Matrix M, ans;

Matrix quickPow(int p) {
  Matrix pro, mm = M;
  for(int i = 0; i < n; ++i) {
    for(int j = 0; j < n; ++j) {
      pro.m[i][j] = 0;
    }
    pro.m[i][i] = 1;
  }             
  while(p) {
    if(p & 1) {
      pro = pro * mm;
    }
    mm = mm * mm;
    p >>= 1;
  }
  return pro;
}

Matrix solve(long long k) {
  if(k == 1) return M;
  Matrix tmp = solve(k / 2);
  if(k & 1) {
    Matrix b = quickPow(k / 2 + 1);
    Matrix xx = tmp * b;
    tmp = tmp + xx;
    tmp = tmp + b;
  } else {
    Matrix b = quickPow(k / 2);
    Matrix xx = tmp * b;
    tmp = tmp + xx;
  }
  return tmp;
}

int main() {
  memset(ans.m, 0, sizeof(ans.m));
  scanf("%d %d %d", &n, &k, &MOD);
  for(int i = 0; i < n; ++i) {
     for(int j = 0; j < n; ++j) {
       scanf("%d", &M.m[i][j]);
       M.m[i][j] %= MOD;
     }
  }
  ans = solve(k);
  for(int i = 0; i < n; ++i) {
     for(int j = 0; j < n - 1; ++j) {
       printf("%d ", ans.m[i][j]);
     }
     printf("%d\n", ans.m[i][n-1]);
  }
  return 0;
}



你可能感兴趣的:(Mathematics)