233 Matrix HDU - 5015
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

题意:
给出矩阵的第0行(233,2333,23333,…)和第0列a1,a2,…an(n<=10,m<=10^9),给出式子: A[i][j] = A[i-1][j] + A[i][j-1],要求A[n][m]。
分析:
这个题主要是转移矩阵不好写
第一列元素为:
⎛⎝⎜⎜⎜⎜⎜⎜0a1a2a3a4⎞⎠⎟⎟⎟⎟⎟⎟(0a1a2a3a4)
转化为:
⎛⎝⎜⎜⎜⎜⎜⎜⎜⎜23a1a2a3a43⎞⎠⎟⎟⎟⎟⎟⎟⎟⎟(23a1a2a3a43)
则第二列为:
⎛⎝⎜⎜⎜⎜⎜⎜⎜⎜23×10+323×10+3+a123×10+3+a1+a223×10+3+a1+a2+a323×10+3+a1+a2+a3+a43⎞⎠⎟⎟⎟⎟⎟⎟⎟⎟(23×10+323×10+3+a123×10+3+a1+a223×10+3+a1+a2+a323×10+3+a1+a2+a3+a43)
根据前后两列的递推关系有等式可得关系转移矩阵A:
⎛⎝⎜⎜⎜⎜⎜⎜⎜⎜⎜⎜⎜⎜⎜⎜⎜⎜a0,ma1,ma2,ma3,m...an,m3⎞⎠⎟⎟⎟⎟⎟⎟⎟⎟⎟⎟⎟⎟⎟⎟⎟⎟=⎛⎝⎜⎜⎜⎜⎜⎜⎜⎜⎜⎜⎜⎜⎜⎜⎜⎜10 0 0 ⋯ 0 110 1 0 ⋯ 0 110 1 1 ⋯ 0 1. . . ⋯ . .. . . ⋯ . .. . . ⋯ . .. . . ⋯ . .10 1 1 ⋯ 1 1 0 0 0 ⋯ 0 1⎞⎠⎟⎟⎟⎟⎟⎟⎟⎟⎟⎟⎟⎟⎟⎟⎟⎟⎛⎝⎜⎜⎜⎜⎜⎜⎜⎜⎜⎜⎜⎜⎜⎜⎜⎜a0,m−1a1,m−1a2,m−1a3,m−1...an,m−13⎞⎠⎟⎟⎟⎟⎟⎟⎟⎟⎟⎟⎟⎟⎟⎟⎟⎟(112)(112)(a0,ma1,ma2,ma3,m...an,m3)=(10 0 0 ⋯ 0 110 1 0 ⋯ 0 110 1 1 ⋯ 0 1. . . ⋯ . .. . . ⋯ . .. . . ⋯ . .. . . ⋯ . .10 1 1 ⋯ 1 1 0 0 0 ⋯ 0 1)(a0,m−1a1,m−1a2,m−1a3,m−1...an,m−13)
code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 15;
const int mod = 1e7+7;
int b[N];
int n;
struct Matrix{
ll mat[N][N];
Matrix operator * (const Matrix &b)const{
Matrix ans;
for(int i = 0; i <= n+1; i++){
for(int j = 0; j <= n+1; j++){
ans.mat[i][j] = 0;
for(int k = 0; k <= n+1; k++){
if(mat[i][k] && b.mat[k][j]){
ans.mat[i][j] += mat[i][k] * b.mat[k][j];
ans.mat[i][j] %= mod;
}
}
}
}
return ans;
}
}a;
Matrix q_pow(Matrix a,int b){
Matrix ans;
memset(ans.mat,0,sizeof(ans));
for(int i = 0; i <= n+1; i++){
ans.mat[i][i] = 1;
}
while(b){
if(b & 1)
ans = ans * a;
b >>= 1;
a = a * a;
}
return ans;
}
void init(){
b[0] = 23;
b[n+1] = 3;
for(int i = 1; i <= n; i++){
scanf("%d",&b[i]);
}
memset(a.mat,0,sizeof(a.mat));
for(int i = 0; i <= n; i++){
a.mat[i][0] = 10;
a.mat[i][n+1] = 1;
}
a.mat[n+1][n+1] = 1;
for(int i = 1; i < n+1; i++){
for(int j = 1; j <= i; j++){
a.mat[i][j] = 1;
}
}
}
int main(){
int m;
while(~scanf("%d%d",&n,&m)){
init();
Matrix ans = q_pow(a,m);
ll s = 0;
for(int i = 0; i <= n+1; i++){
s = (s + ans.mat[n][i] * b[i] % mod) % mod;
}
printf("%lld\n",s);
}
return 0;
}
本文详细解析了HDU-5015题目233Matrix的算法实现,通过构建特殊的矩阵并运用递推关系,解决了一个涉及矩阵递归运算的问题。文章提供了完整的代码实现,展示了如何利用矩阵乘法和快速幂优化递归过程。
750

被折叠的 条评论
为什么被折叠?



