E. Tetrahedron(数学推导)

分类: AC路漫漫2013-08-08 16:07 465人阅读 评论(0) 收藏 举报
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.

E. Tetrahedron(数学推导)

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

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问题,从椎体的顶部D出发,指定走n步,要求最后回到D即可。假设走i步回到起点的走法数为f[i],那么可以得到

f[i]=f[i-1]*2+f[i-2]*3

这个公式的意思是说,在i-1步能走到起点的所有行走路线中,我们调整最后两步,让倒数第2步走到除当前点和起点外的另外两个点,最后一步再走到起点,所以选择是f[i-1]*2. 至于i-2步,依旧是考虑最后两个步骤,倒数第2步没有什么要求,选择有3种。有了这个公式,最后打表即可。

  1. #include<cstdio>
  2. #include<iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. unsigned int n;
  7. int i;
  8. long long f[10000001];
  9. f[1] = 0;
  10. f[2] = 3;
  11. f[3] = 6;
  12. for(i=4; i<10000001;i++)
  13. {
  14. f[i] = f[i-1] * 2 + f[i-2] * 3;
  15. f[i] %= 1000000007;
  16. }
  17. scanf("%d",  &n);
  18. cout<<f[n]<<endl;
  19. return 0;
  20. }
上一篇:linux中利用fstab实现磁盘分区自动挂载


下一篇:什么是 BPMN ?为什么我们要用 BPMN 和工作流 ?