Codeforces 166E Tetrahedron

来源:互联网 发布:信誉源码 编辑:程序博客网 时间:2024/06/10 09:40

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
解题思路:简单DP
#include <cmath>#include <ctime>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <string>#include <vector>#include <queue>#include <algorithm>#include <functional>using namespace std;const int mod = 1000000000 + 7;int dp[2][10];int n;int main() {    int n;    int id = 0;    memset(dp, 0, sizeof(dp));    scanf("%d", &n);    dp[0][4] = 1;    for(int i = 1; i <= n; ++i) {        dp[id^1][1] = dp[id][2];        dp[id^1][1] = (dp[id^1][1] + dp[id][3]) % mod;        dp[id^1][1] = (dp[id^1][1] + dp[id][4]) % mod;        dp[id^1][2] = dp[id][1];        dp[id^1][2] = (dp[id^1][2] + dp[id][3]) % mod;        dp[id^1][2] = (dp[id^1][2] + dp[id][4]) % mod;        dp[id^1][3] = dp[id][1];        dp[id^1][3] = (dp[id^1][3] + dp[id][2]) % mod;        dp[id^1][3] = (dp[id^1][3] + dp[id][4]) % mod;        dp[id^1][4] = dp[id][1];        dp[id^1][4] = (dp[id^1][4] + dp[id][2]) % mod;        dp[id^1][4] = (dp[id^1][4] + dp[id][3]) % mod;        id ^= 1;    }    printf("%d\n", dp[id][4]);    return 0;}


0 0
原创粉丝点击