﻿ POJ 3734 Blocks (矩阵快速幂)

### POJ 3734 Blocks (矩阵快速幂)

《挑战程序设计竞赛》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]

``` 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
", 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
", ans);
}
}

```