题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=5015
思路来自:https://blog.csdn.net/u011721440/article/details/39401515
假设a[0][0]=23,对于第一行的元素,a[i][0]=a[i-1][0]*10+3。
a[i][1]=a[i-1][1]+a[1]
a[i][2]=a[i-1][2]+a[1]+a[2]
a[i][3]=a[i-1][3]+a[1]+a[2]+a[3]
.......
因此能够推出相应矩阵。
代码:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int mod=10000007;
int n, m;
struct matrix {
ll num[12][12];
matrix() { memset(num, 0, sizeof(num)); }
matrix operator*(const matrix &x) {
matrix c;
for (int i = 0; i <= n + 1; ++i)
for (int j = 0; j <= n + 1; ++j)
for (int k = 0; k <= n + 1; ++k)
c.num[i][j] = (c.num[i][j] + num[i][k] * x.num[k][j]) % mod;
return c;
}
matrix &operator=(const matrix &x) {
for (int i = 0; i <= n + 1; ++i)
for (int j = 0; j <= n + 1; ++j)
num[i][j] = x.num[i][j];
return *this;
}
};
matrix pow_mod(matrix& x,int k) {
matrix ans;
for (int i = 0; i <= n + 1; ++i)
ans.num[i][i] = 1;
while (k) {
if (k & 1)ans = ans * x;
x = x * x;
k >>= 1;
}
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
while (cin >> n >> m) {
int a[12] = {0};
a[0] = 23;
a[n + 1] = 3;
for (int i = 1; i <= n; ++i)
cin >> a[i];
matrix x;
for (int i = 0; i <= n; ++i)
x.num[i][0] = 10;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= i; ++j)
x.num[i][j] = 1;
for (int i = 0; i <= n + 1; ++i)
x.num[i][n + 1] = 1;
matrix ans = pow_mod(x, m);
ll tot = 0;
for (int i = 0; i <= n + 1; ++i) {
tot = (tot + a[i] * ans.num[n][i]) % mod;
}
cout << tot << endl;
}
return 0;
}