E. Tetrahedron

链接:https://codeforces.com/problemset/problem/166/E

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 ad 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 Dto 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).

Examples

扫描二维码关注公众号,回复: 9053323 查看本文章

input

Copy

2

output

Copy

3

input

Copy

4

output

Copy

21

Note

The required paths in the first sample are:

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

代码:

#include<bits/stdc++.h>
using namespace std;
long long n,k,mod=1e9+7;
long long m[10000001];
int main()
{
	cin>>n;
	m[1]=0;
	m[2]=3;
	m[3]=6;
	m[4]=21;
	for(int i=5;i<=n;i++)
	{
		m[i]=m[i-1]*2+m[i-2]*3;
		m[i]%=mod;
	}
	cout<<m[n];
}
发布了180 篇原创文章 · 获赞 16 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Luoriliming/article/details/104133610