Codeforces Round #113 (Div. 2) E. Tetrahedron dp

E. Tetrahedron
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a tetrahedron. Let's mark its vertices with letters ABC 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 vertexD 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 vertexD 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).

Sample test(s)
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
題意:螞蟻初始在d點,找走k步後仍在d點的方法數。

思路:dp[i][j]表示走i步在j點的方法數。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
using namespace std;
const int mod=1e9+7;
const int N=100005;
int dp[2][4];
int main(){
    int n;
    scanf("%d",&n);
    dp[0][0]=1;
    for(int i=1;i<=n;i++){
        memset(dp[i&1],0,sizeof(dp[i&1]));
        for(int j=0;j<4;j++){
            for(int k=0;k<4;k++){
                if(j==k)continue;
                dp[i&1][j]+=dp[1-i&1][k];
                dp[i&1][j]%=mod;
            }
        }
    }
    printf("%d\n",dp[n&1][0]);
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章