HDU - 5015 矩阵快速幂(构造矩阵)

【题目链接】


已知 a [ 0 ] [ 1 ] = 233 , a [ 0 ] [ 2 ] = 2333 , a [ 0 ] [ 2 ] = 23333 ⋯ ( a [ 0 ] [ i ] = a [ 0 ] [ i − 1 ] ∗ 10 + 3 ) a[0][1]=233,a[0][2]=2333,a[0][2]=23333\cdots(a[0][i]=a[0][i-1]*10+3) a[0][1]=233,a[0][2]=2333,a[0][2]=23333(a[0][i]=a[0][i1]10+3)
a [ i ] [ j ] = a [ i − 1 ] [ j ] + a [ i ] [ j − 1 ] a[i][j]=a[i-1][j]+a[i][j-1] a[i][j]=a[i1][j]+a[i][j1],现给你n个数, a [ 0 ] [ 1 ] ∼ a [ 0 ] [ n ] a[0][1] \sim a[0][n] a[0][1]a[0][n],最后求 a [ n ] [ m ] % 10000007 a[n][m]\%10000007 a[n][m]%10000007


【数据范围】
n ≤ 10 , m ≤ 1000000000 , 0 ≤ a i , 0 ≤ 1000000000 n\leq 10,m\leq 1000000000,0\leq a_{i,0}\leq 1000000000 n10,m1000000000,0ai,01000000000


【分析】
[ a 0 , 0 a 0 , 1 ⋯ a 0 , m a 1 , 0 a 1 , 1 ⋯ a 1 , m ⋮ ⋮ ⋱ ⋮ a n , 0 a n , 1 ⋯ a n , m ] \begin{bmatrix} a_{0,0}&a_{0,1}&\cdots &a_{0,m} \\ a_{1,0}&a_{1,1}&\cdots &a_{1,m} \\ \vdots & \vdots & \ddots & \vdots \\ a_{n,0}& a_{n,1}& \cdots & a_{n,m} \\ \end{bmatrix} a0,0a1,0an,0a0,1a1,1an,1a0,ma1,man,m
对于上述 n × m n\times m n×m矩阵,我们已知第一行第一列,由于 a i , j = a i − 1 , j + a i , j − 1 a_{i,j}=a_{i-1,j}+a_{i,j-1} ai,j=ai1,j+ai,j1,即可递推到 a n , m = a 1 , m − 1 + a 2 , m − 1 + ⋯ a n , m − 1 + a 0 , m a_{n,m}=a_{1,m-1}+a_{2,m-1}+\cdots a_{n,m-1}+a_{0,m} an,m=a1,m1+a2,m1+an,m1+a0,m
n ≤ 10 n\leq 10 n10,即可构造 12 × 12 12\times 12 12×12的矩阵(当n不为10时,由于计算结果时只计算前n行,所以并不影响结果):
[ 10 0 0 0 ⋯ 0 10 1 0 0 ⋯ 1 10 1 1 0 ⋯ 1 ⋮ ⋮ ⋮ ⋮ ⋱ ⋮ 10 1 1 1 ⋯ 1 0 0 0 0 ⋯ 1 ] \begin{bmatrix} 10 & 0 & 0 & 0 & \cdots &0 \\ 10 & 1& 0 & 0 & \cdots &1 \\ 10 & 1 & 1 & 0 & \cdots & 1 \\ \vdots & \vdots & \vdots & \vdots & \ddots & \vdots \\ 10& 1& 1 & 1 & \cdots & 1 \\ 0 & 0 & 0 & 0 & \cdots & 1 \end{bmatrix} 10101010001110001100001001111
矩阵乘法如下:
[ a 0 , m a 1 , m a 2 , m ⋮ a n , m 3 ] = [ 10 0 0 0 ⋯ 0 10 1 0 0 ⋯ 1 10 1 1 0 ⋯ 1 ⋮ ⋮ ⋮ ⋮ ⋱ ⋮ 10 1 1 1 ⋯ 1 0 0 0 0 ⋯ 1 ] × [ a 0 , m − 1 a 1 , m − 1 a 2 , m − 1 ⋮ a n , m − 1 3 ] \begin{bmatrix} a_{0,m}\\ a_{1,m}\\ a_{2,m}\\ \vdots \\ a_{n,m}\\ 3 \end{bmatrix}= \begin{bmatrix} 10 & 0 & 0 & 0 & \cdots &0 \\ 10 & 1& 0 & 0 & \cdots &1 \\ 10 & 1 & 1 & 0 & \cdots & 1 \\ \vdots & \vdots & \vdots & \vdots & \ddots & \vdots \\ 10& 1& 1 & 1 & \cdots & 1 \\ 0 & 0 & 0 & 0 & \cdots & 1 \end{bmatrix} \times \begin{bmatrix} a_{0,m-1} \\ a_{1,m-1} \\ a_{2,m-1} \\ \vdots \\ a_{n, m-1}\\ 3 \end{bmatrix} a0,ma1,ma2,man,m3=10101010001110001100001001111×a0,m1a1,m1a2,m1an,m13
即为:
[ a 0 , m a 1 , m a 2 , m ⋮ a n , m 3 ] = [ 10 0 0 0 ⋯ 0 10 1 0 0 ⋯ 1 10 1 1 0 ⋯ 1 ⋮ ⋮ ⋮ ⋮ ⋱ ⋮ 10 1 1 1 ⋯ 1 0 0 0 0 ⋯ 1 ] m × [ a 0 , 0 a 1 , 0 a 2 , 0 ⋮ a n , 0 3 ] \begin{bmatrix} a_{0,m}\\ a_{1,m}\\ a_{2,m}\\ \vdots \\ a_{n,m}\\ 3 \end{bmatrix}= \begin{bmatrix} 10 & 0 & 0 & 0 & \cdots &0 \\ 10 & 1& 0 & 0 & \cdots &1 \\ 10 & 1 & 1 & 0 & \cdots & 1 \\ \vdots & \vdots & \vdots & \vdots & \ddots & \vdots \\ 10& 1& 1 & 1 & \cdots & 1 \\ 0 & 0 & 0 & 0 & \cdots & 1 \end{bmatrix} ^m \times \begin{bmatrix} a_{0,0} \\ a_{1,0} \\ a_{2,0} \\ \vdots \\ a_{n, 0}\\ 3 \end{bmatrix} a0,ma1,ma2,man,m3=10101010001110001100001001111m×a0,0a1,0a2,0an,03
这里只需要做一遍矩阵快速幂求出构造矩阵的m次方,最后对第一列做一遍矩阵乘法即可求得答案。


【代码】

#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
typedef vector<LL> vec;
typedef vector<vec> mat;
const LL MOD = 10000007LL;
void print(mat &A) {
	for(int i = 0; i < A.size(); i++) {
		for(int j = 0; j < A[i].size(); j++) printf("%d ", A[i][j]);
		printf("\n");
	}
}
void Init(mat &A) {//构造矩阵
	for(int i = 0; i <= 10; i++) {
		A[i][0] = 10;
		for(int j = 1; j <= 10; j++) {
			if(j <= i)A[i][j] = 1;
			else A[i][j] = 0;
		}
		A[i][11] = 1;
	}
	for(int j = 0; j <= 11; j++) {
		if(j != 11)A[11][j] = 0;
		else A[11][11] = 1;
	}
}
mat mul(mat &A, mat &B) {//矩阵乘法
	mat C(A.size(), vec(B[0].size()));
	for(int i = 0; i < A.size(); i++)
		for(int k = 0; k < B.size(); k++)
			for(int j = 0; j < B[0].size(); j++)
				C[i][j] = (C[i][j] + A[i][k] * B[k][j] % MOD + MOD) % MOD;
	return C;
}
mat pow(mat A, LL n) {//矩阵快速幂
	mat B(A.size(), vec(A.size()));
	for(int i = 0; i < B.size(); i++)B[i][i] = 1;
	while(n > 0) {
		if(n & 1)B = mul(B, A);
		n >>= 1;
		A = mul(A, A);
	}
	return B;
}
void solve(mat &A, int n, int m, mat &num) {
	A = pow(A, m);//求得A的m次方存在A中
	num = mul(A, num);//做矩阵乘法
	printf("%lld\n", num[n][0]);//最后答案即为num[n][0]
}
int main() {
	int n, m;
	mat A(12, vec(12));//构造矩阵数组
	mat num(12, vec(1));//第一列可看成12*1的矩阵
	while(~scanf("%d%d", &n, &m)) {
		Init(A);
		for(int i = 0; i <= 11; i++)num[i][0] = 0LL;
		for(int i = 1; i <= n; i++)scanf("%d", &num[i][0]);
		num[11][0] = 3LL;
		num[0][0] = 23LL;
		solve(A, n, m, num);
	}
	return 0;
}

你可能感兴趣的:(矩阵)