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 Rust (1.15.1) (judge verdict: Accepted)

use std::io;
use std::cmp;
use std::collections::BinaryHeap;
use std::collections::HashMap;
use std::collections::HashSet;
use std::f64::consts::PI;

fn read_line() -> String {
    let mut buf = String::new();
    io::stdin().read_line(&mut buf).ok();
    buf
}

fn is_agc(x: usize) -> bool {
    let x1 = x / 64;
    let x2 = (x / 16) % 4;
    let x3 = (x /4) % 4;
    let x4 = x % 4;

    (x1 == 0 && x2 == 2 && x3 == 1) ||
     (x1 == 0 && x2 == 1 && x3 == 2) ||
     (x1 == 2 && x2 == 0 && x3 == 1) ||
     (x2 == 0 && x3 == 2 && x4 == 1) ||
     (x2 == 0 && x3 == 1 && x4 == 2) ||
     (x2 == 2 && x3 == 0 && x4 == 1) ||
     (x1 == 0 && x3 == 2 && x4 == 1) ||
     (x1 == 0 && x2 == 2 && x4 == 1)
}


fn main() {
    let n: usize = read_line().trim().parse().unwrap();

    if n == 3 {
        println!("61");
    } else {
        let MOD: u64 = 1_000_000_007;
        let mut memo = vec![vec![0u64; 256]; n + 10];
        for i in 0..256 {
            if is_agc(i) {
                memo[3][i] = 0;
            } else {
                memo[3][i] = 1;
            }
        }
        for i in 4..n {
            for j in 0..256 {
                if is_agc(j) {
                    memo[i][j] = 0;
                } else {
                    memo[i][j] = (memo[i-1][j/4] + memo[i-1][j/4 + 64] + memo[i-1][j/4 + 128] + memo[i-1][j/4 + 192]) % MOD;
                }
            }
        }
        let ans = memo[n-1].iter().fold(0u64, |acc, val| (acc + val) % MOD);
        println!("{}", ans);
    }
}