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 Kotlin (1.0.0) (judge verdict: Accepted)



fun main(arr:Array<String>) {
    val n = readLine()!!.toInt()
    val ans = getAns("", n)
    println(ans)
}

val dic = mutableMapOf<String,MutableMap<Int, Long>>()

fun getAns(log:String,remain:Int):Long {
    val key = if(log.length <= 4) log else log.takeLast(4)
    if(!dic.containsKey(key)) {
        dic[key] = mutableMapOf()
    }
    dic[key]!![remain]?.let { return it }
    if(remain == 0) {
        return if(isValid(key)) 1 else 0
    }
    var ans = 0L
    for(c in "ACGT") {
        if(!isValid((key+c).takeLast(4))) {
            continue
        }
        ans += getAns(key+c, remain-1)
        ans = ans % (1000000000 + 7)
    }
    dic[key]!![remain]= ans
    return ans
}

val validDic = mutableMapOf<String, Boolean>()
fun isValid(target:String):Boolean {
    if(validDic.containsKey(target)) {
        return validDic[target]!!
    }
    if(target.length < 3) {
        validDic[target] = true
        return true
    }
    if(target.contains("AGC")) {
        validDic[target] = false
        return false
    }
    for(i in (1 until target.length)) {
        val tmp = target.toMutableList()
        val c = tmp[i]
        tmp[i] = tmp[i-1]
        tmp[i-1] = c
        val unValid = tmp.joinToString("")
        if(unValid.contains("AGC")) {
            validDic[target] = false
            return false
        }
    }
    validDic[target] = true
    return true
}