Problem p03088 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define NREP(i, n) FOR(i, 1, n)
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const int dx[4] = {0, -1, 1, 0};
const int dy[4] = {-1, 0, 0, 1};
const int INF = 1e9 + 7;
int gcd(int x, int y) {
  if (x < y)
    swap(x, y);
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}

template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <class T> void Add(T &a, const T &b, const T &mod = 1000000007) {
  int val = ((a % mod) + (b % mod)) % mod;
  if (val < 0) {
    val += mod;
  }
  a = val;
}
////////////////////////////////////////
int main() {
  int N;
  cin >> N;
  ll dp[110][4][4][4];
  // dp[N][i][j][k]でN文字目まで確定していてN-2文字目がi,N-1文字目がj,N文字目がk
  // Aなら0,Cなら1,Gなら2,Tなら3
  for (int i = 0; i < 110; ++i) {
    for (int j = 0; j < 4; ++j) {
      for (int k = 0; k < 4; ++k) {
        for (int l = 0; l < 4; ++l) {
          dp[i][j][k][l] = 0;
        }
      }
    }
  }
  for (int i = 0; i < 4; ++i) {
    for (int j = 0; j < 4; ++j) {
      for (int k = 0; k < 4; ++k) {
        if ((i == 0 && j == 2 && k == 1) || (i == 0 && j == 1 && k == 2) ||
            (i == 2 && j == 0 && k == 1)) {
          continue;
        }
        dp[3][i][j][k] = 1;
      }
    }
  }
  for (int i = 3; i < 110; ++i) {
    for (int j = 0; j < 4; ++j) {
      for (int k = 0; k < 4; ++k) {
        for (int l = 0; l < 4; ++l) {
          for (int m = 0; m < 4; ++m) {
            if (j == 0 && k == 2 && l == 1)
              continue;
            if (j == 2 && k == 0 && l == 1)
              continue;
            if (j == 0 && k == 1 && l == 2)
              continue;
            if (m == 0 && k == 2 && l == 1)
              continue;
            if (m == 0 && j == 2 && l == 1)
              continue;
            dp[i + 1][j][k][l] += dp[i][m][j][k];
            dp[i + 1][j][k][l] %= INF;
          }
        }
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < 4; ++i) {
    for (int j = 0; j < 4; ++j) {
      for (int k = 0; k < 4; ++k) {
        ans += dp[N][i][j][k];
      }
    }
  }
  cout << ans % INF << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define NREP(i, n) FOR(i, 1, n)
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const int dx[4] = {0, -1, 1, 0};
const int dy[4] = {-1, 0, 0, 1};
const int INF = 1e9 + 7;
int gcd(int x, int y) {
  if (x < y)
    swap(x, y);
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}

template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <class T> void Add(T &a, const T &b, const T &mod = 1000000007) {
  int val = ((a % mod) + (b % mod)) % mod;
  if (val < 0) {
    val += mod;
  }
  a = val;
}
////////////////////////////////////////
int main() {
  int N;
  cin >> N;
  ll dp[110][4][4][4];
  // dp[N][i][j][k]でN文字目まで確定していてN-2文字目がi,N-1文字目がj,N文字目がk
  // Aなら0,Cなら1,Gなら2,Tなら3
  for (int i = 0; i < 110; ++i) {
    for (int j = 0; j < 4; ++j) {
      for (int k = 0; k < 4; ++k) {
        for (int l = 0; l < 4; ++l) {
          dp[i][j][k][l] = 0;
        }
      }
    }
  }
  for (int i = 0; i < 4; ++i) {
    for (int j = 0; j < 4; ++j) {
      for (int k = 0; k < 4; ++k) {
        if ((i == 0 && j == 2 && k == 1) || (i == 0 && j == 1 && k == 2) ||
            (i == 2 && j == 0 && k == 1)) {
          continue;
        }
        dp[3][i][j][k] = 1;
      }
    }
  }
  for (int i = 3; i < 105; ++i) {
    for (int j = 0; j < 4; ++j) {
      for (int k = 0; k < 4; ++k) {
        for (int l = 0; l < 4; ++l) {
          for (int m = 0; m < 4; ++m) {
            if (j == 0 && k == 2 && l == 1)
              continue;
            if (j == 2 && k == 0 && l == 1)
              continue;
            if (j == 0 && k == 1 && l == 2)
              continue;
            if (m == 0 && k == 2 && l == 1)
              continue;
            if (m == 0 && j == 2 && l == 1)
              continue;
            dp[i + 1][j][k][l] += dp[i][m][j][k];
            dp[i + 1][j][k][l] %= INF;
          }
        }
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < 4; ++i) {
    for (int j = 0; j < 4; ++j) {
      for (int k = 0; k < 4; ++k) {
        ans += dp[N][i][j][k];
      }
    }
  }
  cout << ans % INF << endl;
  return 0;
}