233 Matrix
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 670 Accepted Submission(s): 401
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).
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
Hint
构造矩阵b:
b[0]=233
b[1]=a[1]<??http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPjwvcD4KPHA+PHN0cm9uZz5iWzJdPWFbMl08L3N0cm9uZz48L3A+CjxwPjxzdHJvbmc+YlszXT1hWzNdPC9zdHJvbmc+PC9wPgo8cD48c3Ryb25nPi4uLi4uLjwvc3Ryb25nPjwvcD4KPHA+PHN0cm9uZz5iW24mIzQzOzFdPTM8L3N0cm9uZz48L3A+CjxwPjxzdHJvbmc+wP3I5yDR+cD9Mzwvc3Ryb25nPjwvcD4KPHA+PHN0cm9uZz5iWzBdPTIzMzwvc3Ryb25nPjwvcD4KPHA+PHN0cm9uZz5iWzFdPTIzPC9zdHJvbmc+PC9wPgo8cD48c3Ryb25nPmJbMl09NDc8L3N0cm9uZz48L3A+CjxwPjxzdHJvbmc+YlszXT0xNjwvc3Ryb25nPjwvcD4KPHA+PHN0cm9uZz5iWzRdPTM8L3N0cm9uZz48L3A+CjxwPjxzdHJvbmc+td3Nxr7Y1fNBo6zR+cD9Mzwvc3Ryb25nPjwvcD4KPHA+PHN0cm9uZz4xMCAwIDAgMCAxPC9zdHJvbmc+PC9wPgo8cD48c3Ryb25nPjEgICAxIDAgMCAwPC9zdHJvbmc+PC9wPgo8cD48c3Ryb25nPjEgICAxIDEgMCAwPC9zdHJvbmc+PC9wPgo8cD48c3Ryb25nPjEgICAxIDEgMSAwPC9zdHJvbmc+PC9wPgo8cD48c3Ryb25nPjAgICAwIDAgMCAxPC9zdHJvbmc+PC9wPgo8cD48c3Ryb25nPm4mIzQzOzK917e91fM8L3N0cm9uZz48L3A+CjxwPjxzdHJvbmc+QV5tKmK1xLXabs/uvs3Kx73hufs8L3N0cm9uZz48L3A+CjxwPjxzdHJvbmc+tPrC66O6PC9zdHJvbmc+PC9wPgo8cD48c3Ryb25nPjwvc3Ryb25nPjxwcmUgY2xhc3M9"brush:java;">//1046ms #include
#include
#include
#include
using namespace std; const int mod=10000007; struct matrix { long long ma[13][13]; }a; int n,m; long long b[13]; matrix multi(matrix x,matrix y)//矩阵相乘 { matrix ans; memset(ans.ma,0,sizeof(ans.ma)); for(int i=0;i<=n+1;i++) { for(int j=0;j<=n+1;j++) { for(int k=0;k<=n+1;k++) { ans.ma[i][j]=(ans.ma[i][j]+x.ma[i][k]*y.ma[k][j])%mod; } } } return ans; } int main() { while(~scanf("%d%d",&n,&m)) { memset(a.ma,0,sizeof(a.ma)); b[0]=233; for(int i=1;i<=n;i++) { scanf("%I64d",&b[i]); } b[n+1]=3; a.ma[0][0]=10;//构造a矩阵 a.ma[0][n+1]=1; a.ma[n+1][n+1]=1; for(int i=1;i<=n;i++) { for(int j=0;j<=i;j++) { a.ma[i][j]=1; } } matrix ans; memset(ans.ma,0,sizeof(ans.ma)); for(int i=0;i<=n+1;i++)//单位矩阵 { for(int j=0;j<=n+1;j++) { if(i==j) ans.ma[i][j]=1; } } while(m)//矩阵快速幂 { if(m&1) { ans=multi(ans,a); } a=multi(a,a); m=(m>>1); } matrix mp; memset(mp.ma,0,sizeof(mp.ma)); for(int i=0;i<=n+1;i++)//a的m次方与b矩阵相乘 { for(int k=0;k<=n+1;k++) { mp.ma[i][0]=(mp.ma[i][0]+ans.ma[i][k]*b[k])%mod; } } printf("%I64d\n",mp.ma[n][0]); } return 0; }
|