HDU5015
这题属于很水的矩形题了。 矩阵也很容易构造!
233 Matrix
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 453 Accepted Submission(s): 298
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
Source
2014 ACM/ICPC Asia Regional Xi’an Online
需要构造一个 n+2 维的矩形。 就是要增加一维去维护2333这样的序列。 可以发现 2333 = 233*10 + 3 所以增加了一维就 是1, 然后就可以全部转移了。 反正状态就是 一列 ,再增加一个233不停增加的,然后一个1,所以是 n+2维。 很水了。
#include <stdio.h>
#include
#include
#include
#include
#include <string.h>
#include
#include
#include
#include <math.h>
using namespace std;
const int MOD = 1e7+7;
struct Matrix{
int mat[15][15];
int n;
void init(int _n){
n = _n;
memset(mat,0,sizeof(mat));
}
Matrix operator (const Matrix &b)const{
Matrix ret;
ret.init(n);
for(int i = 0;i < n;i++)
for(int j = 0;j < n;j++){
ret.mat[i][j] = 0;
for(int k = 0;k < n;k++){
ret.mat[i][j] += (long long)mat[i][k]b.mat[k][j]%MOD;
if(ret.mat[i][j] >= MOD)
ret.mat[i][j] -= MOD;
}
}
return ret;
}
};
Matrix pow_M(Matrix a,long long n){
Matrix ret;
Matrix tmp = a;
ret.init(a.n);
for(int i = 0;i < a.n;i++)ret.mat[i][i] = 1;
while(n){
if(n&1)ret = rettmp;
tmp = tmptmp;
n >>= 1;
}
return ret;
}
int a[20];
Matrix A;
int main()
{
//freopen(“in.txt”,”r”,stdin);
//freopen(“out.txt”,”w”,stdout);
int n,m;
while(scanf("%d%d",&n,&m) == 2){
for(int i = 0;i < n;i++){scanf("%d",&a\[i\]);a\[i\]%=MOD;}
A.init(n+2);
for(int j = 0;j < n;j++){
for(int i = 0;i <= j;i++)
A.mat\[i\]\[j\] = 1;
A.mat\[n\]\[j\] = 1;
}
A.mat\[n\]\[n\] = 10;
A.mat\[n+1\]\[n\] = 3;
A.mat\[n+1\]\[n+1\] = 1;
A = pow_M(A,m);
long long ans = 0;
for(int i = 0;i < n;i++){
ans += (long long)a\[i\]*A.mat\[i\]\[n-1\]%MOD;
ans %= MOD;
}
ans += (long long)233*A.mat\[n\]\[n-1\];
ans%=MOD;
ans += (long long)A.mat\[n+1\]\[n-1\];
ans%=MOD;
printf("%d\\n",(int)ans);
}
return 0;
}