http://acm.hdu.edu.cn/showproblem.php?pid=5015
根据题意 构造一个 (n+2)*1的原矩阵 【a1 a2 a3.....233 3】
和一个 n+2 * n+2 的系数矩阵
【1 1 1 1 0 0】
【0 1 1 1 0 0】
【0 0 1 1 0 0】
【1 1 1 1 10 0】
【0 0 0 0 1 1】
使得原矩阵每次乘以系数矩阵都会得到 i+1列的数据
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;
struct Matrix
{
long long mat[15][15];
} ;
int n,m;
const long long mod=10000007;
Matrix unit_matrix, c,xi;
Matrix mul(Matrix a, Matrix b) //矩阵相乘
{
Matrix res;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
{
res.mat[i][j] = 0;
for(int t = 0; t < n; t++)
{
res.mat[i][j] += a.mat[i][t] * b.mat[t][j];
res.mat[i][j] %= mod;
}
// printf("%d ",res.mat[0][0]);
}
return res;
}
Matrix pow_matrix(Matrix a, int m) //矩阵快速幂
{
Matrix res = unit_matrix;
while(m != 0)
{
if(m & 1)
res = mul(res, a);
a = mul(a, a);
m >>= 1;
}
return res;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for (int i=0; i<n; i++)
scanf("%lld",&c.mat[0][i]);
memset(xi.mat,0,sizeof xi.mat);
c.mat[0][n]=233;
c.mat[0][n+1]=3;
for(int i=0; i<n; i++)
{
for (int j=i; j<n+1; j++)
{
xi.mat[i][j]=1;
}
}
for (int i=0; i<n; i++)
xi.mat[n][i]=1;
for (int i=0; i<n; i++)
xi.mat[n+1][i]=0;
for (int i=0; i<n; i++)
xi.mat[i][n]=0;
xi.mat[n][n]=10;
xi.mat[n+1][n]=1;
for (int i=0; i<n+1; i++)
xi.mat[i][n+1]=0;
xi.mat[n+1][n+1]=1;
n+=2;
//初始化单位矩阵
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
unit_matrix.mat[i][j] = 0;
for(int i = 0; i < n; i++)
unit_matrix.mat[i][i] = 1;
Matrix ans = pow_matrix(xi,m);
ans=mul(c,ans);
printf("%lld\n",ans.mat[0][n-2-1]);
}
}