1. 程式人生 > >POJ 3734 Blocks (矩陣快速冪)

POJ 3734 Blocks (矩陣快速冪)

不同 sans rst tab pac function you space cat

題目鏈接:http://poj.org/problem?id=3734

《挑戰程序設計競賽》202頁。與單純的用遞推式與矩陣快速冪求第N項不同,設染到第i個方塊為止,紅綠都是偶數的方案數目為a,紅綠恰有一個是偶數方案數目為b,紅綠都是奇數方案數目為c, 則:

a[i+1] = 2 * a[i] + b[i]

b[i+1] = 2 * a[i]+2 * b[i]+2 * c[i]

c[i+1] = b[i] + 2 * c[i]

之後構建3*3矩陣求解

代碼:

 1 typedef vector<int> vec;
 2 typedef vector<vec> mat;
 3 int
mod = 1e4+7; 4 5 mat mul(mat &A, mat &B){ 6 mat C(A.size(), vec(B[0].size())); 7 for(int i = 0; i< A.size(); i++){ 8 for(int k = 0; k < B.size(); k++){ 9 for(int j = 0; j < B[0].size(); j++){ 10 C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % mod;
11 } 12 } 13 } 14 return C; 15 } 16 17 mat fpow(mat A, ll k){ 18 mat ans(A.size(), vec(A.size())); 19 for(int i = 0; i < A.size(); i++){ 20 ans[i][i] = 1; 21 } 22 while(k > 0){ 23 if(k & 1) ans = mul(A, ans); 24 A = mul(A, A);
25 k >>= 1; 26 } 27 return ans; 28 } 29 30 int main(){ 31 int t; 32 scanf("%d", &t); 33 while(t--){ 34 int n; 35 scanf("%d", &n); 36 mat A(3, vec(3)); 37 A[0][0] = 2; A[0][1] = 1; A[0][2] = 0; 38 A[1][0] = 2; A[1][1] = 2; A[1][2] = 2; 39 A[2][0] = 0; A[2][1] = 1; A[2][2] = 2; 40 A = fpow(A, n - 1); 41 ll ans = (2 * A[0][0] + 2 * A[0][1]) % mod; 42 printf("%lld\n", ans); 43 } 44 }

題目:

Blocks
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6776 Accepted: 3300

Description

Panda has received an assignment of painting a line of blocks. Since Panda is such an intelligent boy, he starts to think of a math problem of painting. Suppose there are N blocks in a line and each block can be paint red, blue, green or yellow. For some myterious reasons, Panda want both the number of red blocks and green blocks to be even numbers. Under such conditions, Panda wants to know the number of different ways to paint these blocks.

Input

The first line of the input contains an integer T(1≤T≤100), the number of test cases. Each of the next T lines contains an integer N(1≤N≤10^9) indicating the number of blocks.

Output

For each test cases, output the number of ways to paint the blocks in a single line. Since the answer may be quite large, you have to module it by 10007.

Sample Input

2
1
2

Sample Output

2
6
typedef vector<int> vec;
typedef vector<vec> mat;
int mod = 1e4+7;

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;
			}
		}
	}
	return C;
}

mat fpow(mat A, ll k){
	mat ans(A.size(), vec(A.size()));
	for(int i = 0; i < A.size(); i++){
		ans[i][i] = 1;
	}
	while(k > 0){
		if(k & 1) ans = mul(A, ans);
		A = mul(A, A);
		k >>= 1;
	}
	return ans;
}

int main(){
	int t;
	scanf("%d", &t);
	while(t--){
		int n;
		scanf("%d", &n);
		mat A(3, vec(3));
		A[0][0] = 2; A[0][1] = 1; A[0][2] = 0;
		A[1][0] = 2; A[1][1] = 2; A[1][2] = 2;
		A[2][0] = 0; A[2][1] = 1; A[2][2] = 2;
		A = fpow(A, n - 1);
		ll ans = (2 * A[0][0] + 2 * A[0][1]) % mod;
		printf("%lld\n", ans);
	}
}

POJ 3734 Blocks (矩陣快速冪)