題目: LINK
此題可以用矩陣快速冪做
用行向量[a1, b2, a3, a4, .... 233, 3] ,n+2個元素
再構造n+2 的方陣
1 1 1 1 ..... 0 0
0 1 1 1 ..... 0 0
0 0 1 1 ..... 0 0
0 0 0 1 ..... 0 0
................
1 1 1 1 ..... 10 0
0 0 0 0 ..... 1 1
左上角的n大小的方陣是上三角行的,多加的兩列是爲了控制233....33的,每次都可以算出來,
求方陣的m次冪,用初始行向量乘以結果方陣的第n列,就是結果了.
模擬一下就知道爲什麼這麼做了.
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std;
#define INF 1000000000
typedef __int64 LL;
#define N 15
LL n, m, num[N];
#define mod 10000007
struct node {
LL mat[N][N];
};
node mul(node x, node y)
{
node ret;
memset(ret.mat, 0, sizeof(ret.mat));
for(int i=1; i<=n+2; i++) {
for(int j=1; j<=n+2; j++) {
for(int k = 1; k<=n+2; k++) {
ret.mat[i][j] += x.mat[i][k]*y.mat[k][j];
ret.mat[i][j] %= mod;
}
}
}
return ret ;
}
node pow_(node x, LL y)
{
node ret;
for(int i = 0; i <= n+2; i++) {
for(int j = 0; j <= n+2; j++) {
if(i == j) ret.mat[i][j] = 1;
else ret.mat[i][j] = 0;
}
}
while(y) {
if(y&1) {
ret = mul(ret, x);
}
y>>=1;
x = mul(x, x);
}
return ret;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE
while(scanf("%I64d%I64d", &n, &m) != EOF) {
for(int i = 1; i <= n; i++) scanf("%I64d", &num[i]);
node S;
memset(S.mat, 0, sizeof(S.mat));
for(int i = 1; i <= n; i++) {
for(int j = n; j >= i; j--) S.mat[i][j] = 1;
}
for(int i = 1; i <= n; i++) S.mat[n+1][i] = 1;
S.mat[n+1][n+1] = 10;
S.mat[n+2][n+1] = S.mat[n+2][n+2] = 1;
S = pow_(S, m);
LL ans = 0;
for(int i = 1; i <= n; i++) ans += num[i] * S.mat[i][n] % mod, ans %= mod;
ans += (LL)233 * S.mat[n+1][n]; ans %= mod;
ans += (LL) 3 * S.mat[n+2][n]; ans %= mod;
printf("%I64d\n", ans);
}
return 0;
}