欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

233 Matrix - HDU 5015 - 矩阵快速幂

程序员文章站 2022-07-03 18:24:46
...

链接:

  http://acm.hdu.edu.cn/showproblem.php?pid=5015


题目:

Problem Description

In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 … in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 2333, 23333… (it means a0,1 = 233,a0,2 = 2333,a0,3 = 23333…) Besides, in 233 matrix, we got ai,j = ai-1,j +ai,j-1( i,j ≠ 0). Now you have known a1,0,a2,0,…,an,0, could you tell me an,m in the 233 matrix?

Input

There are multiple test cases. Please process till EOF.

For each case, the first line contains two postive integers n,m(n ≤ 10,m ≤ 109). The second line contains n integers, a1,0,a2,0,…,an,0(0 ≤ ai,0 < 231).

Output

For each case, output an,m mod 10000007.

Sample Input

1 1
1
2 2
0 0
3 7
23 47 16

Sample Output

234
2799
72937

Hint

233 Matrix - HDU 5015 - 矩阵快速幂


题意:

  有一个矩阵横排第一行初始是0 233 2333 23333...,然后给出计算公式a[i][j] = a[i - 1][j] + a[i][j - 1],然后给出了n m和第一列的各值要求计算a[n][m]的值。


思路:

第一列元素为:

0
a1
a2
a3
a4

转化为:

23
a1
a2
a3
a4
3

则第二列为:

23*10+3              = 233
23*10+3+a1           = 233 + a1
23*10+3+a1+a2        = 233 + a1 + a2
23*10+3+a1+a2+a3     = 233 + a1 + a2 + a3
23*10+3+a1+a2+a3+a4  = 233 + a1 + a2 + a3 +a4
3                    = 3

实现:

#include <iostream>
#include <algorithm>
#include <set>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <iomanip>
#include <functional>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#define il inline
#define ll long long
#define ull unsigned long long

using namespace std;
#define maxn 107
long long n, m, a[maxn], result[maxn], mod = 10000007;

struct mat {
    long long m[maxn][maxn];
    mat(long long i=0) {
        memset(m,0,sizeof m);
        if(i == 1)
            for(long long i=0 ; i<n ; i++) m[i][i] = 1;
    }
    mat operator * (const mat tmp) const {
        mat ret;
        for(long long i=0,x ; i<n ; i++) {
            for(long long j=0 ; x=0,j<n ; j++) {
                for(long long k=0 ; k<n ; k++) {
                    x += (m[i][k] * tmp.m[k][j]) % mod;
                }
                ret.m[i][j] = x % mod;
            }
        }
        return ret;
    }
    mat qpow(long long n) {
        mat ret = 1, tmp = *this;
        while(n!=0) {
            if(bool(n&1)) ret = ret * tmp;
            tmp = tmp * tmp;
            n >>= 1;
        }
        return ret;
    }
    void show() {
        mat tmp = *this;
        for(long long i=0 ; i<n ; i++) {
            for(long long j=0 ; j<n-1 ; j++) {
                cout << tmp.m[i][j] << ' ';
            }
            cout << tmp.m[i][n-1] << '\n';
        }
        cout << '\n';
    }
};
mat calc(const mat &a, const mat &b, long long h, long long l, long long s) {
    mat ret;
    for(long long i=0, x ; i<h ; i++) {
        for(long long j=0 ; x=0, j<l ; j++) {
            for(long long k=0 ; k<s ; k++) {
                x += (a.m[i][k] * b.m[k][j]) % mod;
            }
            ret.m[i][j] = x % mod;
        }
    }
    return ret;
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
    ios_base::sync_with_stdio(false);cin.tie(0);
    while(cin >> n >> m) {
        mat base = 1, tmp, ans = 0;
        ans.m[0][0] = 23;
        for(long long i=1 ; i<=n ; i++) cin >> ans.m[i][0];
        n++; base.m[n][n] = 1, ans.m[n][0] = 3;
        for(long long i=0 ; i<n ; i++) base.m[i][n] = 1, base.m[i][0] = 10;
        for(long long i=0 ; i<n ; i++) for(long long j=1 ; j<=i; j++) base.m[i][j] = 1;
        n++;
        tmp = base.qpow(m);
        ans = calc(tmp,ans,n,1,n);
        cout << ans.m[n-2][0] << '\n';
    }
    return 0;
}