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::prelude::*;

fn input<T>() -> T
    where T: std::str::FromStr {
    let stdin = std::io::stdin();
    let token: String = stdin
        .lock()
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().unwrap()
}


static MOD: usize = 1_000_000_000 + 7;
fn main() {
    let n = input::<usize>();
    let mut dp = vec![vec![vec![vec![0; 4]; 4]; 4]; 101];

    dp[0][3][3][3] = 1;
    for len in 0..n {
        // 最後から1文字目
        for c1 in 0..4 {
            // 最後から2文字目
            for c2 in 0..4 {
                // 最後から3文字目
                for c3 in 0..4 {
                    if dp[len][c1][c2][c3] == 0 {
                        continue;
                    }
                    // 新しく追加する文字列
                    for a in 0..4 {
                        if
                            a == 2 && c1 == 1 && c2 == 0 ||
                            a == 2 && c1 == 0 && c2 == 1 ||
                            a == 1 && c1 == 2 && c2 == 0 ||
                            a == 2 && c1 == 1 && c3 == 0 ||
                            a == 2 && c2 == 1 && c3 == 0 {
                           continue;
                        }

                        dp[len+1][a][c1][c2] += dp[len][c1][c2][c3];
                        dp[len+1][a][c1][c2] %= MOD;
                    }
                }
            }
        }
    }
    let mut ans = 0;
    for c1 in 0..4 {
        // 最後から2文字目
        for c2 in 0..4 {
            // 最後から3文字目
            for c3 in 0..4 {
                ans += dp[n][c1][c2][c3];
                ans %= MOD;
            }
        }
    }
    println!("{}", ans);
}

#[allow(unused_doc_comments)]
#[cfg(test)]
mod tests {
    #[test]
    fn tester() {
        let inputs = [
("
3
", "
61
"),

("
 4
", "
 230
"),

("
 100
","
388130742
"),
        ];

        for (input, expected) in inputs.iter() {
            call(input, expected.to_string());
        }
    }

    fn call<T: AsRef<str>>(input: T, expected: String) {
        use std::process::{Command, Stdio};
        use std::io::{Read, Write};

        let mut cmd_ls = Command::new("echo")
            .arg(input.as_ref())
            .stdout(Stdio::piped())
            .spawn()
            .unwrap();
        let mut cmd_grep = Command::new("cargo")
            .arg("run")
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .spawn()
            .unwrap();

        if let Some(ref mut stdout) = cmd_ls.stdout {
            if let Some(ref mut stdin) = cmd_grep.stdin {
                let mut buf: Vec<u8> = Vec::new();
                stdout.read_to_end(&mut buf).unwrap();
                stdin.write_all(&buf).unwrap();
            }
        }

        let res = cmd_grep.wait_with_output().unwrap().stdout;

        let res_string: String = String::from_utf8(res.to_vec()).unwrap();

        // convert '\n' into space
        let actual = res_string
            .replace("\n", " ")
            .trim_end_matches(" ")
            .trim_start_matches(" ")
            .to_owned();
        let expected = expected
            .replace("\n", " ")
            .trim_end_matches(" ")
            .trim_start_matches(" ")
            .to_owned();

        // split into a vector
        let actual = actual
            .split(" ")
            .map(|s| s.parse::<f64>().unwrap())
            .collect::<Vec<_>>();
        let expected = expected
            .split(" ")
            .map(|s| s.parse::<f64>().unwrap())
            .collect::<Vec<_>>();

        assert_eq!(actual, expected);
    }
}