K - 233 Matrix HDU - 5015
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?
InputThere 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).OutputFor 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
题解
矩阵快速幂,主要是找出重复计算的矩阵
#include<bits\stdc++.h>
using namespace std;
#define ll long long
const int mod = 1e7+7;
ll n, m, ant[15];
typedef struct node {
ll e[15][15];
void Init(){
memset(e, 0, sizeof(e));
for(int i = 0; i < n+2; i++) e[i][i] = 1;
}
}Mar;
Mar ans, mp;
Mar Mut(Mar a, Mar b) {
Mar c;
memset(c.e, 0, sizeof(c.e));
for(int i = 0; i < n+2; i++) {
for(int j = 0; j < n+2; j++) {
for(int k = 0; k < n+2; k++) {
c.e[i][j] += (a.e[i][k] * b.e[k][j])%mod;
}
c.e[i][j] %= mod; // 一定要再mod一次
}
}
return c;
}
Mar quick_mod(Mar a, int power) {
Mar re;
re.Init();
while(power) {
if(power&1) re = Mut(re, a);
a = Mut(a, a);
power >>= 1;
}
return re;
}
int main() {
freopen("test.in", "r", stdin);
while(cin >> n >> m) {
ll wsy = 0;
ant[0] = 23;
for(int i = 1; i < n+1; i++) cin >> ant[i];
ant[n+1] = 3;
cout << "*" << endl;
memset(mp.e, 0, sizeof(mp.e));
for(int i = 0; i < n+1; i++) mp.e[i][0] = 10;
for(int i = 0; i < n+2; i++) mp.e[i][n+1] = 1;
for(int i = 0; i < n+1; i++)
for(int j = i; j >= 1; j--)
mp.e[i][j] = 1;
ans = quick_mod(mp, m);
for(int i = 0; i < n+2; i++)
wsy += (ans.e[n][i] * ant[i])%mod;
wsy %= mod;
cout << wsy << endl;
}
return 0;
}