233 Matrix
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1215 Accepted Submission(s): 714
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 a
0,1 = 233,a
0,2 = 2333,a
0,3 = 23333...) Besides, in 233 matrix, we got a
i,j = a
i-1,j +a
i,j-1( i,j ≠ 0). Now you have known a
1,0,a
2,0,...,a
n,0, could you tell me a
n,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 ≤ 10 9). The second line contains n integers, a 1,0,a 2,0,...,a n,0(0 ≤ a i,0 < 2 31).
For each case, the first line contains two postive integers n,m(n ≤ 10,m ≤ 10 9). The second line contains n integers, a 1,0,a 2,0,...,a n,0(0 ≤ a i,0 < 2 31).
Output
For each case, output a
n,m mod 10000007.
Sample Input
1 1 1 2 2 0 0 3 7 23 47 16
Sample Output
234 2799 72937Hint
Source
注第二个代码的构造方法:
矩阵为(233 a0 a1 a2 1),转移矩阵为
|10 1 11 0|
|0 1 11 0|
|0 01 1 0|
|0 00 1 0|
|3 0 0 01|
/*
另一种构造方法。加油!!!
Time:2015-4-10 18:36
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const LL mod=10000007;
struct Matrix{
LL 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++){
for(int k=0;k<n;k++){
ret.mat[i][j]+=mat[i][k]*b.mat[k][j];
ret.mat[i][j]%=mod;
//if(ret.mat[i][j]>=mod)ret.mat[i][j]-=mod;//不能用-mod,有可能是mod的好几倍
}
}
}
return ret;
}
};
Matrix pow_mat(Matrix a,int k){
Matrix ret; ret.Init(a.n);
for(int i=0;i<ret.n;i++)ret.mat[i][i]=1;
while(k>0){
if(k&1)ret=ret*a;
a=a*a;
k>>=1;
}
return ret;
}
int main(){
int n,m;
Matrix trans,ans;
while(scanf("%d%d",&n,&m)!=EOF){
trans.Init(n+2);ans.Init(n+2);
for(int i=0;i<n;i++){
scanf("%lld",&ans.mat[0][i]);
}ans.mat[0][n]=233;ans.mat[0][n+1]=3;
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
trans.mat[i][j]=1;
}
trans.mat[n][i]=1;
}
trans.mat[n][n]=10;trans.mat[n+1][n]=1;
trans.mat[n+1][n+1]=1;
trans=pow_mat(trans,m);
ans=ans*trans;
/*
for(int i=0;i<n+2;i++){
printf("%d ",ans.mat[0][i]);
for(int j=0;j<n+2;j++){
// printf("%d ",trans.mat[i][j]);
}puts("");
}*/
printf("%lld\n",ans.mat[0][n-1]);
}
return 0;
}
/*
每一列相当于前一列和该列的最上边的和,所以构造转移矩阵
矩阵构造,,快速幂
加油!!!
Time:2015-3-30 9:48
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
typedef long long LL;
const int mod=10000007;
struct Matrix{
int n;
int mat[15][15];
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]+=(LL)mat[i][k]*b.mat[k][j]%mod;
if(ret.mat[i][j]>=mod)ret.mat[i][j]-=mod;
}
}
}
return ret;
}
};
Matrix quick_power(Matrix a,LL b){
Matrix ret;
ret.Init(a.n);
for(int i=0;i<ret.n;i++){ret.mat[i][i]=1;}
while(b>0){
if(b&1) ret=ret*a;
a=a*a;
b>>=1;
}
return ret;
}
int main(){
int n,m;
Matrix a,b;
while(scanf("%d%d",&n,&m)!=EOF){
a.Init(n+2); b.Init(n+2);
a.mat[0][0]=233;a.mat[0][n+1]=1;
for(int i=1;i<n+1;i++){
scanf("%d",&a.mat[0][i]);
}
b.mat[0][0]=10;b.mat[n+1][0]=3;
for(int j=1;j<n+1;j++){
for(int i=0;i<=j;i++){
b.mat[i][j]=1;
}
}
b.mat[n+1][n+1]=1;
b=quick_power(b,m);
a=a*b;
/*
for(int i=0;i<n+2;i++){
for(int j=0;j<n+2;j++){
printf("%d ",b.mat[i][j]);
}puts("");
}
for(int i=0;i<n+2;i++){
printf("%d ",a.mat[0][i]);
}puts("");
*/
printf("%d\n",a.mat[0][n]);
}
return 0;
}