• 模板


    struct Matrix {
        static const int MAXN = 100;
        static const int MOD = 1e9 + 7;
        int n, a[MAXN + 5][MAXN + 5];
    
        Matrix(int _n = 0) {n = _n;}
    
        int add(const int &a, const int &b)const {
            int c = a + b;
            if(c >= MOD)
                c -= MOD;
            return c;
        }
    
        int sub(const int &a, const int &b)const {
            int c = a - b;
            if(c < 0)
                c += MOD;
            return c;
        }
    
        int mul(const int &a, const int &b)const {
            ll c = 1ll * a * b;
            if(c >= MOD)
                c %= MOD;
            return (int)c;
        }
    
        void Init(int _n, const int _a[MAXN + 5][MAXN + 5]) {
            n = _n;
            for(int i = 1; i <= n; ++i) {
                for(int j = 1; j <= n; ++j)
                    a[i][j] = _a[i][j];
            }
        }
    
        void ZERO(int _n) {
            n = _n;
            for(int i = 1; i <= n; ++i) {
                for(int j = 1; j <= n; ++j)
                    a[i][j] = 0;
            }
        }
    
    
        void ONE(int _n) {
            n = _n;
            for(int i = 1; i <= n; ++i) {
                for(int j = 1; j <= n; ++j)
                    a[i][j] = (i == j);
            }
        }
    
        Matrix operator+(const Matrix& m)const {
            Matrix res(n);
            for(int i = 1; i <= n; ++i) {
                for(int j = 1; j <= n; ++j)
                    res.a[i][j] = add(a[i][j], m.a[i][j]);
            }
            return res;
        }
    
        Matrix operator-(const Matrix& m)const {
            Matrix res(n);
            for(int i = 1; i <= n; ++i) {
                for(int j = 1; j <= n; ++j)
                    res.a[i][j] = sub(a[i][j], m.a[i][j]);
            }
            return res;
        }
    
        Matrix operator*(const Matrix& m)const {
            Matrix res;
            res.ZERO(n);
            for(int k = 1; k <= n; ++k) {
                for(int i = 1; i <= n; ++i) {
                    for(int j = 1; j <= n; ++j)
                        res.a[i][j] = add(res.a[i][j], mul(a[i][k], m.a[k][j]));
                }
            }
            return res;
        }
    
        Matrix pow(ll k)const {
            Matrix x, res;
            x.Init(n, a);
            res.ONE(n);
            while(k) {
                if(k & 1)
                    res = res * x;
                x = x * x;
                k >>= 1;
            }
            return res;
        }
    
        void Show(string name = "") {
            //cout << "Matrix " << name << " = 
    ";
            for(int i = 1; i <= n; ++i) {
                for(int j = 1; j <= n; ++j)
                    printf("%d%c", a[i][j], " 
    "[j == n]);
            }
        }
    };
    
  • 相关阅读:
    搭建 Linux 下 GitLab 服务器(转)
    sql语法:inner join on, left join on, right join on具体用法
    Android Studio之同一应用创建多个Activity(一)
    java环境变量配置
    老鸟的Python新手教程
    域名注冊以及域名解析设置
    Android在WebView上构建Web应用程序
    利用JasperReport+iReport进行Web报表开发
    android App Widgets
    多数据库下activiti的流程定义缓存问题
  • 原文地址:https://www.cnblogs.com/KisekiPurin2019/p/11898508.html
Copyright © 2020-2023  润新知