Tetrahedron

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 (1e9 + 7).

Input

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

Output

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

Examples

[外链图片转存中…(img-v27004521313.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTc4MTEyMQ==,size_16,color_FFFFFF,t_70)

Note

The required paths in the first sample are:

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

解题思路

步数 d a b c
1 0 1 1 1
2 3 2 2 2
3 6 7 7 7
4 21 20 20 20
5 60 61 61 61

倒推
去D点时必须从ABC三点去,而上一级去ABC三点中一点时,也必须从不包括本单身的三点上来,把去此点的方法累加起来便可知从d点来这点的n步有多少种方法
在这里插入图片描述

#include<iostream>
using namespace std;
long long  n, i, j,m=1e9+7, k, f[10000005]= {0};
int main()
{
    cin>>n;
    f[2] =3;
    for (i =3; i <= n; i++)
    {
        if((i-1)%2==1)
            f[i]=3*(f[i-1]+1);
        else
            f[i]=3*(f[i-1]-1);
        f[i]%=m;
    }
    cout<<f[n];
    return 0;
}
发布了3 篇原创文章 · 获赞 4 · 访问量 137

猜你喜欢

转载自blog.csdn.net/weixin_45781121/article/details/104528467