链接:https://codeforces.com/problemset/problem/166/E
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 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
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];
}

探讨了在一个四面体中,一只从顶点D出发的蚂蚁,在恰好n步后回到顶点D的不同循环路径数量的计算问题。通过递推算法,解决了路径数量可能巨大的问题,并考虑了模运算以避免溢出。
730

被折叠的 条评论
为什么被折叠?



