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::io::BufRead;
use std::ops::Add;
use std::ops::AddAssign;

fn main() {
    let mut sc = Scanner::new(std::io::stdin());
    let n = sc.next::<usize>();
    // 1 2 3
    let mut dp = vec![Mint(0); 4 * 4 * 4];
    dp[0] = Mint(1);
    for _ in 0..n {
        let mut ndp = vec![Mint(0); 4 * 4 * 4];
        for j in 0..dp.len() {
            let a = j >> 4 & 3;
            let b = j >> 2 & 3;
            let c = j & 3;
            for k in 0..4 {
                if a == 1 && b == 2 && c == 3
                    || a == 1 && b == 3 && c == 2
                    || a == 2 && b == 1 && c == 3
                    || a == 1 && b == 2 && k == 3
                    || a == 1 && c == 2 && k == 3
                    || b == 1 && c == 2 && k == 3
                    || b == 1 && c == 3 && k == 2
                    || b == 2 && c == 1 && k == 3
                {
                    // Do nothing
                } else {
                    let s = b << 4 | c << 2 | k;
                    ndp[s] += dp[j];
                }
            }
        }
        dp = ndp;
    }
    let mut res = Mint(0);
    for i in 0..dp.len() {
        res += dp[i];
    }
    println!("{}", res);
}

////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////// TEMPLATE ///////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

/// ```
/// use contest::vec;
/// let mut is = vec![2,3,1];
/// assert!(vec::next_permutation(&mut is));
/// assert_eq!(vec![3,1,2], is);
/// assert!(vec::next_permutation(&mut is));
/// assert_eq!(vec![3,2,1], is);
/// assert!(!vec::next_permutation(&mut is));
/// assert_eq!(vec![1,2,3], is);
///
/// let mut is = (0..3).collect();
/// let mut count = 0;
/// loop {
///     count+=1;
///     // Do something using is
///     if !vec::next_permutation(&mut is) {
///         break;
///     }
/// }
/// assert_eq!(6, count);
///
/// let mut is = vec![1,2,1];
/// assert!(vec::next_permutation(&mut is));
/// assert_eq!(vec![2,1,1], is);
/// assert!(!vec::next_permutation(&mut is));
/// assert_eq!(vec![1,1,2], is);
/// ```
pub fn next_permutation(is: &mut Vec<i32>) -> bool {
    let n = is.len();
    for i in (1..n).rev() {
        let i = i as usize;
        if is[i - 1] < is[i] {
            let mut j = n - 1;
            while is[i - 1] >= is[j] {
                j -= 1;
            }
            is.swap(i - 1, j);
            reverse(is, i, n);
            return true;
        }
    }
    is.reverse();
    false
}

fn reverse(v: &mut Vec<i32>, from_inclusive: usize, to_exclusive: usize) {
    let mut i = from_inclusive;
    let mut j = to_exclusive - 1;
    while i < j {
        v.swap(i, j);
        i += 1;
        j -= 1;
    }
}
/// Scanner
///
/// # Example
/// ```
/// use contest::io::Scanner;
/// let mut sc = Scanner::new("1 2 \n\n \r\t \n 3.5".as_bytes());
/// assert_eq!("1".to_string(), sc.next::<String>());
/// assert_eq!(2, sc.next());
/// assert_eq!(3.5, sc.next());
///
/// // To create a scanner from stdin:
/// Scanner::new(std::io::stdin());
/// ```

pub struct Scanner<R: io::Read> {
    br: io::BufReader<R>,
    // Read tokens are stored in reversed order per line.
    buf: Vec<String>,
}

pub fn new<R: io::Read>(r: R) -> Scanner<R> {
    Scanner::new(r)
}

impl<R: io::Read> Scanner<R> {
    #[inline]
    pub fn new(r: R) -> Scanner<R> {
        Scanner {
            br: io::BufReader::new(r),
            buf: vec![],
        }
    }
    #[inline]
    pub fn next<T>(&mut self) -> T
    where
        T: std::str::FromStr,
        T::Err: std::fmt::Debug,
    {
        self.next_string()
            .map(|s| s.parse::<T>().expect("Parse failed: "))
            .expect("Unexpected EOF")
    }
    fn next_string(&mut self) -> Option<String> {
        self.buf.pop().or_else(|| match self.update() {
            true => self.next_string(),
            false => None,
        })
    }
    #[inline]
    fn update(&mut self) -> bool {
        let mut s = String::new();
        let res = self.br.read_line(&mut s);
        match res.expect("I/O error.") {
            0 => false,
            _ => {
                self.buf = s.split_whitespace().map(|x| x.to_string()).rev().collect();
                true
            }
        }
    }
}

#[test]
fn next() {
    let mut sc = new("hoge".as_bytes());
    let s: String = sc.next();
    assert_eq!("hoge", s);
}

static mut MODULO: i64 = 1_000_000_007;

#[inline]
fn modulo() -> i64 {
    unsafe { MODULO }
}

#[derive(Copy, Clone)]
pub struct Mint(pub i64);

impl Mint {
    pub fn set_mod(modulo: i32) {
        unsafe {
            MODULO = modulo as i64;
        }
    }
    pub fn value(self) -> i64 {
        self.0
    }
}

impl ::std::fmt::Display for Mint {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// ```
/// use contest::math::Mint;
/// Mint::set_mod(5);
/// let a = Mint(2);
/// let b = Mint(3);
/// assert_eq!(0, (a + b).0);
/// assert_eq!(1, (b + b).0);
/// assert_eq!(4, (a + a).0);
/// ```
impl Add for Mint {
    type Output = Mint;
    fn add(self, o: Mint) -> Mint {
        let mut x = self.0 + o.0;
        if x >= modulo() {
            x -= modulo();
        }
        Mint(x)
    }
}

/// ```
/// use contest::math::Mint;
/// Mint::set_mod(5);
/// let mut a = Mint(2);
/// a += Mint(4);
/// assert_eq!(1, a.0);
/// ```
impl AddAssign for Mint {
    fn add_assign(&mut self, o: Mint) {
        let mut x = self.0 + o.0;
        if x >= modulo() {
            x -= modulo();
        }
        self.0 = x;
    }
}

/// ```
/// use contest::math;
/// assert_eq!(3, math::inv(2,5));
/// assert_eq!(1, math::inv(1,2));
/// assert_eq!(1, math::inv(3,2));
/// ```
pub fn inv(value: i32, modulo_prime: i32) -> i32 {
    return pow_mod(value, modulo_prime - 2, modulo_prime);
}

/// ```
/// use contest::math;
/// assert_eq!(8, math::pow_mod(2,3,10));
/// assert_eq!(1, math::pow_mod(3,4,10));
/// assert_eq!(1, math::pow_mod(0,0,3));
/// assert_eq!(0, math::pow_mod(0,0,1));
/// ```
pub fn pow_mod(value: i32, exponent: i32, modulo: i32) -> i32 {
    if exponent < 0 {
        panic!("Exponent should >= 0.");
    }
    if modulo == 1 {
        return 0;
    }
    let modulo = modulo as i64;
    let mut value = value as i64;
    if value >= modulo {
        value %= modulo;
    }
    if value < 0 {
        value = value % modulo + value;
    }
    let mut res = 1;
    let mut exponent = exponent;
    while exponent > 0 {
        if (exponent & 1) == 1 {
            res = res * value % modulo;
        }
        value = value * value % modulo;
        exponent >>= 1;
    }
    res as i32
}