首页 > 代码库 > HDU - 5015 233 Matrix (矩阵构造)

HDU - 5015 233 Matrix (矩阵构造)

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 a0,1 = 233,a0,2 = 2333,a0,3 = 23333...) Besides, in 233 matrix, we got ai,j = ai-1,j +ai,j-1( i,j ≠ 0). Now you have known a1,0,a2,0,...,an,0, could you tell me an,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 ≤ 109). The second line contains n integers, a1,0,a2,0,...,an,0(0 ≤ ai,0 < 231).
 

Output
For each case, output an,m mod 10000007.
 

Sample Input
1 1 1 2 2 0 0 3 7 23 47 16
 

Sample Output
234 2799 72937
Hint
 

Source
2014 ACM/ICPC Asia Regional Xi‘an Online
题意:求最后一位的值
思路:将第i行之后的矩阵都往后移动i位构造一个矩阵:
                                                                                                  10 0 0 0 0 ... 1
                                                                                                  1 1 0 0 0  ..... 0
                                                                                                  0 1 1 0 ...       0
                                                                                                  . . .. . .. . .. .. .. . .
                                                                                                 0 0 0 0 0 ....    1,
但是还需要推出一个B维度为n+2 * 1的初始矩阵,就是根据移动的推出初始的一列
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef __int64 ll;
using namespace std;
const int maxn = 20;
const int mod = 10000007;

struct Matrix{
	int n;
	ll v[maxn][maxn];	

	Matrix(int _n = maxn) {
		n = _n;
	}

	void init(ll _v = 0) {
		memset(v, 0, sizeof(v));
		if (_v)
			for (int i = 0; i < n; i++)
				v[i][i] = _v;
	}

	void output() {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++)
				printf("%I64d ", v[i][j]);
			puts("");
		}
		puts("");
	}
} a, b, c;

Matrix operator * (Matrix a, Matrix b) {
	Matrix c(a.n);
	for (int i = 0; i < a.n; i++) {
		for (int j = 0; j < a.n; j++) {
			c.v[i][j] = 0;
			for (int k = 0; k < a.n; k++) {
				c.v[i][j] += (a.v[i][k] * b.v[k][j]) % mod;
				c.v[i][j] %= mod;
			}
		}
	}
	return c;
}

Matrix operator ^ (Matrix a, ll k) {
	Matrix c(a.n);
	c.init(1);
	while (k) {
		if (k & 1)
			c = a * c;
		a = a * a;
		k >>= 1;
	}
	return c;
}

Matrix operator + (Matrix a, Matrix b) {
	Matrix c(a.n);
	for (int i = 0; i < a.n; i++)
		for (int j = 0; j < a.n; j++)
		c.v[i][j] = (b.v[i][j] + a.v[i][j]) % mod;
	return c;
}

Matrix operator + (Matrix a, ll b) {
	Matrix c = a;
	for (int i = 0; i < a.n; i++)
		c.v[i][i] = (a.v[i][i] + b) % mod;
	return c;
}


ll n, m, con[maxn];
ll f[maxn], f2[maxn];

int main() {
	while (scanf("%I64d%I64d", &n, &m) != EOF) {
		for (int i = 1; i <= n; i++)
			scanf("%I64d", &con[i]);

		memset(f, 0, sizeof(f));
		f[0] = 233;
		f[1] = con[1];
		for (int i = 2; i <= n; i++) {
			memcpy(f2, f, sizeof(f));
			for (int j = 1; j < 10; j++)
				f[j] = f2[j] + f2[j-1];
			f[i] = con[i];
			f[0] = f[0] * 10 + 3;
		}

		a.init();
		a.n = n + 2;
		a.v[0][0] = 10;
		a.v[0][n+1] = 1;
		for (int i = 1; i <= n; i++)
			a.v[i][i-1] = a.v[i][i] = 1;
		a.v[n+1][n+1] = 1;

		b.init();
		b.n = n+2;
		for (int i = 0; i <= n; i++)
			b.v[i][0] = f[i];
		b.v[n+1][0] = 3;

		c = a ^ m;
		c = c * b;
		printf("%I64d\n", c.v[n][0]);
	}
	return 0;
}



HDU - 5015 233 Matrix (矩阵构造)