233 Matrix
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 749 Accepted Submission(s): 453
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![]()
一上来想的时候,将每列后面的元素与233等分开,用两个矩阵分别计算,导致了无法找到正确的矩阵,是一个巨大的错误。本体应当将所有数都放入一个矩阵中去计算,更为直接。
思路:
第一列元素为:
0
a1
a2
a3
a4
转化为:23
a1
a2
a3
a4
3
则第二列为:
23*10+3
23*10+3+a1
23*10+3+a1+a2
23*10+3+a1+a2+a3
23*10+3+a1+a2+a3+a4
3
根据前后两列的递推关系,有等式可得矩阵A的元素为:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 20 + 10;
const ll MOD = 1e7 + 7;
struct Matrix{
ll n,m;
ll mat[maxn][maxn];
void init(){
n = 0;m = 0;
memset(mat,0,sizeof(mat));
}
void input(){
// scanf("%I64d%I64d",&n,&m);
for(ll i = 1;i <= n;i++)
for(ll j = 1;j <= m;j++)
scanf("%I64d",&mat[i][j]);
}
Matrix() {init();}
void output(){
printf("n: %I64d, m: %I64d\n",n,m);
for(int i = 0;i <= n;i++){
for(int j = 0;j <= m;j++){
printf("%I64d\t",mat[i][j]);
}
printf("\n");
}
}
Matrix operator * (const Matrix & x){
Matrix ans;
ans.n = n;ans.m = x.m;
for(ll i = 0;i <= n;i++){
for(ll j = 0;j <= x.m;j++){
for(ll k = 0;k <= m;k++){
ans.mat[i][j] += mat[i][k] * x.mat[k][j];
ans.mat[i][j] %= MOD;
}
ans.mat[i][j] = (ans.mat[i][j] + MOD) % MOD;
}
}
return ans;
}
};
Matrix q_pow(Matrix x,ll k){
if(k == 1) return x;
if(k == 2) return x * x;
Matrix temp = q_pow(x,k / 2);
Matrix ans = temp * temp;
if(k % 2==1) ans = ans * x;
return ans;
}
Matrix getMat(ll n,ll m){
Matrix ans;
ans.n = ans.m = n + 1;
for(int i = 0;i <= n;i++) ans.mat[i][0] = 10;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= i;j++){
ans.mat[i][j] = 1;
}
}
for(int i = 0;i <= n + 1;i++) ans.mat[i][n + 1] = 1;
ans = q_pow(ans,m);
return ans;
}
ll n,m;
int main()
{
while(~scanf("%I64d%I64d",&n,&m)){
Matrix p;
p.n = n + 1;p.m = 0;p.mat[0][0] = 23;
for(int i = 1;i <= n;i++) scanf("%I64d",&p.mat[i][0]);p.mat[n + 1][0] = 3;
Matrix q = getMat(n,m);
Matrix ans;ans = q * p;
printf("%I64d\n",ans.mat[n][0]);
}
return 0;
}