这是一道codeforces的题目;题目如下
You are given a tetrahedron. Let's mark its vertices with letters A, B, C and D correspondingly.

An ant is standing in the vertex D of the tetrahedron. The ant is quite active and he wouldn't stay idle. At each moment of time he makes a step from one vertex to another one along some edge of the tetrahedron. The ant just can't stand on one place.

You do not have to do much to solve the problem: your task is to count the number of ways in which the ant can go from the initial vertex D to itself in exactly n steps. In other words, you are asked to find out the number of different cyclic paths with the length of n from vertex D to itself. As the number can be quite large, you should print it modulo 1000000007 (109 + 7).

Input
The first line contains the only integer n (1 ≤ n ≤ 107) — the required length of the cyclic path.

Output
Print the only integer — the required number of ways modulo 1000000007 (109 + 7).

Example
Input
2
Output
3
Input
4
Output
21
Note
The required paths in the first sample are:


D - A - D
D - B - D
D - C - D

这道题是一道dp的题目,主要是不太好想;
假设dpn代表第n天走到为m的点方案数
则层层递推,可知,假设0--->A; 1--->B ; 2--->C;3--->D;(一一对应关系)
那么只要先把边界初始化 dp[1] [0]=1;dp[1] [0]=1; dp[1] [0]=1;
然后就可以递推 dp[i] [j]=(dp[i] [j]+dp[i-1] [k])%1e9+7;
上面dp递推的思想就是现在到达这个点的方案等于上一步的另外一个点加上这个点的方案,将所有的方案求出来后,就可以得到答案是dp[n] [3]即可;

代码如下:

#include <iostream>
#include <string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<list>
using namespace std;
typedef long long ll;
const int  N = 1e7+5;
const int mod = 1e9+7;
int dp[N][5];
int main()
{
    int n;
    while(scanf("%d", &n)!=EOF)
    {
        memset(dp,0,sizeof(dp));
        dp[1][0]=1;
        dp[1][1]=1;
        dp[1][2]=1;
        int i,j,k;
        for(i=2;i<=n;i++)
        {
            for(j=0;j<4;j++)
            {
                for(k=0;k<4;k++)
                {
                    if(j!=k)
                    {
                        dp[i][j]=(dp[i][j]+dp[i-1][k])%mod;
                    }
                }
            }
        }
        printf("%d\n",dp[n][3]);
    }
    return 0;
}

Maosghoul
2 声望3 粉丝