233 Matrix
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 707 Accepted Submission(s): 430
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 72937唉,比赛时候没能写出来。其实是一道构造矩阵的水题。可以构造12*12的矩阵(第一二行起辅助作用)c=[1 0 0 0 0 0 0 0 0 0 0 01 10 0 0 0 0 0 0 0 0 0 01 10 1 0 0 0 0 0 0 0 0 01 10 1 1 0 0 0 0 0 0 0 01 10 1 1 1 0 0 0 0 0 0 01 10 1 1 1 1 0 0 0 0 0 01 10 1 1 1 1 1 0 0 0 0 01 10 1 1 1 1 1 1 0 0 0 01 10 1 1 1 1 1 1 1 0 0 01 10 1 1 1 1 1 1 1 1 0 01 10 1 1 1 1 1 1 1 1 1 01 10 1 1 1 1 1 1 1 1 1 1 ]构造第0列的矩阵(其中第一,二行起辅助作用,第三行开始为实际数据)为a=[323a1a2a3......an]由以上矩阵,c^1*a可以构造出第1列,c^2*a可以构造出第二列,c^m*a可以构造出第m列。其中第m列中的第n行就是我们想要的最后答案.那么如何求c^m*a%M(其中M=10000007)呢?c^m*a%M=((c^m%M)*(a%M))%M;c^m%M可以用矩阵快速幂很容易求得。过程中为了避免数据溢出,要用long long 型哦!具体AC的代码如下:#include<iostream> #include<algorithm> #define LL long long #define L 12 #define M 10000007 using namespace std; struct Mtraix{ LL m[L][L];}; Mtraix mul(Mtraix a,Mtraix b) { Mtraix c; fill(&c.m[0][0],&c.m[L][0],0); for(int i=0;i<L;i++) for(int j=0;j<L;j++) for(int k=0;k<L;k++) c.m[i][j]=(c.m[i][j]+(a.m[i][k]*b.m[k][j])%M)%M; return c; } Mtraix quick_mod(Mtraix a,int b) { Mtraix c; fill(&c.m[0][0],&c.m[L][0],0); for(int i=0;i<L;i++) for(int j=0;j<L;j++) if(i==j) c.m[i][j]=1; while(b) { if(b&1) c=mul(a,c); b>>=1; a=mul(a,a); } return c; } int main() { LL n,m; while(cin>>n>>m) { Mtraix c,t,a; fill(&c.m[0][0],&c.m[L][0],0); for(int i=0;i<L;i++) for(int j=0;j<=i;j++) { if(i==0) {c.m[i][j]=1;break;} if(j==1) c.m[i][j]=10; else c.m[i][j]=1; } fill(&a.m[0][0],&a.m[L][0],0); a.m[0][0]=3;a.m[1][0]=23; for(int i=2;i<=n+1;i++) cin>>a.m[i][0]; t=quick_mod(c,m); t=mul(t,a); cout<<t.m[n+1][0]<<endl; } return 0; }