zoukankan      html  css  js  c++  java
  • HDU 1575 Tr A 矩阵快速幂

    跟着cxlove的矩阵专题(http://blog.csdn.net/ACM_cxlove/article/details/7815594)刷的,一道一道来。

    最裸的题目,直接快速幂算就好了。

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <climits>
    #include <iostream>
    #include <string>
    
    using namespace std;
     
    #define MP make_pair
    #define PB push_back
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef vector<int> VI;
    typedef pair<int, int> PII;
    typedef pair<double, double> PDD;
    const int INF = INT_MAX / 3;
    const double eps = 1e-8;
    const LL LINF = 1e17;
    const double DINF = 1e60;
    const int maxn = 15;
    const int mod = 9973;
    
    struct Matrix {
        int n, m, data[maxn][maxn];
        Matrix(int n = 0, int m = 0): n(n), m(m) {
            memset(data, 0, sizeof(data));
        }
        void print() {
            for(int i = 1; i <= n; i++) {
                for(int j = 1; j <= m; j++) {
                    printf("%d ", data[i][j]);
                }
                puts("");
            }
        }
    };
    
    Matrix operator * (Matrix a, Matrix b) {
        int n = a.n, m = b.m;
        Matrix ret(n, m);
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= m; j++) {
                for(int k = 1; k <= a.m; k++) {
                    ret.data[i][j] += a.data[i][k] * b.data[k][j];
                    ret.data[i][j] %= mod;
                }
            }
        }
        return ret;
    }
    
    Matrix pow(Matrix mat, int k) {
        if(k == 1) return mat;
        Matrix ret = pow(mat * mat, k / 2);
        if(k & 1) ret = ret * mat;
        return ret;
    }
    
    Matrix mat;
    int n, k;
    
    int main() {
        int T; scanf("%d", &T);
        while(T--) {
            scanf("%d%d", &n, &k);
            mat.n = mat.m = n;
            for(int i = 1; i <= n; i++) {
                for(int j = 1; j <= n; j++) {
                    scanf("%d", &mat.data[i][j]);
                }
            }
            mat = pow(mat, k);
            int ans = 0;
            for(int i = 1; i <= n; i++) {
                ans = (ans + mat.data[i][i]) % mod;
            }
            printf("%d
    ", ans);
        }
        return 0;
    }
    

      

  • 相关阅读:
    面试题11:旋转数组的最小数字(C++)
    2019.9.20学习内容及随堂笔记
    2019.9.19学习内容及小结
    2019.9.18(day39)学习内容及小结
    2019.9.17学习内容及随堂笔记
    2019.9.16学习内容及随堂笔记
    2019.9.12(day36)学习内容及笔记
    2019.9.11学习内容及随堂笔记
    2019.9.10学习内容及随堂笔记
    2019.9.9学习内容及随堂笔记
  • 原文地址:https://www.cnblogs.com/rolight/p/4048508.html
Copyright © 2011-2022 走看看