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复制

2

样例输出1

4

样例解释1

样例输入2

3

样例输出2

12

样例解释2

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

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

所以可以先来个暴力

#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去重,然后可以发现,结果是这样的

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

#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;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值