Problem:
Score : 400 points

Problem Statement

You are given an integer N. Find the number of strings of length N that satisfy the following conditions, modulo 10^9+7:

The string does not contain characters other than A, C, G and T.

The string does not contain AGC as a substring.

The condition above cannot be violated by swapping two adjacent characters once.

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC.

Constraints

3 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of strings of length N that satisfy the following conditions, modulo 10^9+7.

Sample Input 1

3

Sample Output 1

61

There are 4^3 = 64 strings of length 3 that do not contain characters other than A, C, G and T. Among them, only AGC, ACG and GAC violate the condition, so the answer is 64 - 3 = 61.

Sample Input 2

4

Sample Output 2

230

Sample Input 3

100

Sample Output 3

388130742

Be sure to print the number of strings modulo 10^9+7.

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)


#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstddef>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

// clang-format off
template <typename T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); }
template <typename T> T lcm(T a, T b) { return a * b / gcd(a, b); }
template <typename T> T clamp(T val, T min, T max) { return val < min ? min : (max < val ? max : val); }
template <typename T> size_t max_index(T begin, T end) { return distance(begin, max_element(begin, end)); }
template <typename T> size_t min_index(T begin, T end) { return distance(begin, min_element(begin, end)); }
// clang-format on

// #define int long long

using namespace std;

int dfs(string s, int l, int n) {

  // cout << "dfs: " + s << endl;
  int tmp = 0;

  // if (l == n) return 1;
  if (l == n) {
    cout << s << endl;
    return 1;
  }

  // if (l < 2) {
  //   tmp += dfs(s + 'A', l + 1, n) + dfs(s + 'C', l + 1, n) + dfs(s + 'G', l + 1, n) + dfs(s + 'T', l + 1, n);
  // } else if (l == 3) {
  //   if ((s[l - 2] == 'A' && s[l - 1] == 'G') || (s[l - 2] == 'G' && s[l - 1] == 'A')) {
  //   tmp += dfs(s + 'A', l + 1, n) + dfs(s + 'G', l + 1, n) + dfs(s + 'T', l + 1, n);
  // } else if (s[l - 2] == 'A' && s[l - 1] == 'C') {
  //   tmp += dfs(s + 'A', l + 1, n) + dfs(s + 'C', l + 1, n) + dfs(s + 'T', l + 1, n);
  // } else {
  //   tmp += dfs(s + 'A', l + 1, n) + dfs(s + 'C', l + 1, n) + dfs(s + 'G', l + 1, n) + dfs(s + 'T', l + 1, n);
  //   }
  // } else {
    
  // }

  if (l > 2 && s[l - 3] == 'A' && (s[l - 1] == 'A' || s[l - 2] == 'A')) {
    tmp += dfs(s + 'A', l + 1, n) + dfs(s + 'G', l + 1, n) + dfs(s + 'T', l + 1, n);
  } else if ((s[l - 2] == 'A' && s[l - 1] == 'G') || (s[l - 2] == 'G' && s[l - 1] == 'A')) {
    tmp += dfs(s + 'A', l + 1, n) + dfs(s + 'G', l + 1, n) + dfs(s + 'T', l + 1, n);
  } else if (s[l - 2] == 'A' && s[l - 1] == 'C') {
    tmp += dfs(s + 'A', l + 1, n) + dfs(s + 'C', l + 1, n) + dfs(s + 'T', l + 1, n);
  } else {
    tmp += dfs(s + 'A', l + 1, n) + dfs(s + 'C', l + 1, n) + dfs(s + 'G', l + 1, n) + dfs(s + 'T', l + 1, n);
  }

  return tmp;

}

signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int num = 0;

  int n;
  cin >> n;

  num = dfs("", 0, n);

  cout << num << endl;

  return 0;
}