xiaoyu's blog

Coding for a better life!

0%

2019 ICPC国际大学生程序设计竞赛中国邀请赛(南昌) H. Coloring Game

David has a white board with 2 \times N2×N grids.He decides to paint some grids black with his brush.He always starts at the top left corner and ends at the bottom right corner, where grids should be black ultimately.

Each time he can move his brush up(↑), down(↓), left(←), right(→), left up(↖), left down(↙), right up(↗), right down (↘) to the next grid.

For a grid visited before,the color is still black. Otherwise it changes from white to black.

David wants you to compute the number of different color schemes for a given board. Two color schemes are considered different if and only if the color of at least one corresponding position is different.

Input

One line including an integer n(0<n \le 10^9)n(0<n≤109)

Output

One line including an integer, which represent the answer \bmod 1000000007mod1000000007

样例输入1

1
2

样例输出1

1
4

样例解释1

image

样例输入2

1
3

样例输出2

1
12

样例解释2
image

题目意思是说有一个2*N的网格,从左上角涂色,涂到右下角为止,可以往旁边8个方向扩散,问一共有多少种不同的涂色方案,结果对1e9+7取模

拿到这道题第一反应就是打表,试试看能不能发现某些规律

所以可以先来个暴力

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>
#include<vector>
#include<set>
#include<cstring>
using namespace std;
const int maxn = 100;
set<vector<int> > sset;
int n;
int dir[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
int maze[2][maxn];
int valid(int x, int y) {
return x >= 0 && x < 2 && y >= 0 && y < n;
}
void dfs(int x, int y) {
vector<int> v;
if (x == 1 && y == n - 1) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < n; j++) {
v.push_back(maze[i][j]);
}
}
sset.insert(v);
return;
}
for (int i = 0; i < 8; i++) {
int nx = x + dir[i][0], ny = y + dir[i][1];
if (valid(nx, ny) && !maze[nx][ny]) {
maze[nx][ny] = 1;
dfs(nx, ny);
maze[nx][ny] = 0;
}
}
}
int main() {
for (n = 1; n <= 50; n++) {
sset.clear();
memset(maze, 0, sizeof(maze));
maze[0][0] = 1;
dfs(0, 0);
cout << sset.size() << endl;
}
return 0;
}

每产生一种方案就往vector容器里存,再通过set去重,发现结果是:

1
2
3
4
5
6
7
8
9
1
4
12
36
108
324
972
2916
8738

所以可以大胆推测,从n = 2开始,结果构成一个首项为4,公比为3的等比数列,所以终于可以用快速幂取模解决了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
using namespace std;
const int MOD = 1e9 + 7;
long long quick_pow(int n, int k) {
long long res = 1, base = n;
while (k) {
if (k & 1) res = res * base % MOD;
base = base * base % MOD;
k >>= 1;
}
return res;
}
int main() {
int n;
cin >> n;
if (n == 1) cout << "1";
else cout << 4 * quick_pow(3, n - 2) % MOD;
return 0;
}