zoukankan      html  css  js  c++  java
  • HDU 5015 233 Matrix 矩阵快速幂

    可以构造如下矩阵

    A:

    1 0 0 0 0 0 0 0 0 0 0 0
    1 10 0 0 0 0 0 0 0 0 0 0
    1 10 1 0 0 0 0 0 0 0 0 0
    1 10 1 1 0 0 0 0 0 0 0 0
    1 10 1 1 1 0 0 0 0 0 0 0
    1 10 1 1 1 1 0 0 0 0 0 0
    1 10 1 1 1 1 1 0 0 0 0 0
    1 10 1 1 1 1 1 1 0 0 0 0
    1 10 1 1 1 1 1 1 1 0 0 0
    1 10 1 1 1 1 1 1 1 1 0 0
    1 10 1 1 1 1 1 1 1 1 1 0
    1 10 1 1 1 1 1 1 1 1 1 1

    A0:

    3

    233

    a0+233

    a0+a1+233

    a0+a1+a2+233

    ....

    这样就有第m列为A^(m-1)*A0

    #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 LL mod = 1e7 + 7;
    
    const int maxn = 15;
    
    struct Matrix {
        int n, m;
        LL 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++) {
                    cout << data[i][j] << " ";
                }
                cout << endl;
            }
        }
    };
    
    Matrix operator * (Matrix a, Matrix b) {
        Matrix ret(a.n, b.m);
        for(int i = 1; i <= a.n; i++) {
            for(int j = 1; j <= b.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, LL p) {
        Matrix ret(mat.n, mat.m);
        if(p == 0) {
            for(int i = 1; i <= mat.n; i++) ret.data[i][i] = 1;
            return ret;
        }
        if(p == 1) return mat;
        ret = pow(mat * mat, p / 2);
        if(p & 1) ret = ret * mat;
        return ret;
    }
    
    int main() {
        Matrix A(12, 12); A.data[1][1] = 1;
        for(int i = 2; i <= 12; i++) {
            A.data[i][1] = 1; A.data[i][2] = 10;
        }
        for(int i = 3; i <= 12; i++) {
            for(int j = 1, k = 3; j < i - 1; j++, k++) {
                A.data[i][k] = 1;
            }
        }
        A.print();
        LL n, m;
        while(cin >> n >> m) {
            LL a[11] = {0};
            for(int i = 1; i <= n; i++) cin >> a[i];
            Matrix A0(12, 1);
            A0.data[1][1] = 3; A0.data[2][1] = 233;
            for(int i = 3, j = 1; i <= 12; i++, j++) {
                A0.data[i][1] = a[j] + A0.data[i - 1][1];
            }
            Matrix ans = pow(A, m - 1) * A0;
            cout << ans.data[n + 2][1] << endl;
        }
        return 0;
    }
    

      

  • 相关阅读:
    PHP生成xml 无法识别或是无法读取或是浏览器不识别等问题
    关于PHP 采集类
    Centos7 下安装Docke
    Git使用之设置SSH Key
    yii2.0中Rbac 怎么添加超加管理员
    Undefined index: HTTP_RAW_POST_DATA的解决办法
    window下phpstudy的nginx配置虚拟主机
    yii2.0中添加二维数组,多条数据。
    预防onion比特币勒索病毒,如何快速关闭135,137,138,139,445端口
    github与git之间怎么建立连接
  • 原文地址:https://www.cnblogs.com/rolight/p/4049225.html
Copyright © 2011-2022 走看看