E. Tetrahedron
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.

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 (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

【题意】

给一个四面体,从顶点D沿棱走n步回到D,问方案数。

【分析】

笔者高中的时候记得在数学试卷上做过类似的题目, 不过当时是求概率的,所以笔者一看到这道题目的时候就是往概率上想,而走n步的情况总可能就是3的n次方,所以只要求出走n步到达D点的概率,用它乘以总可能的情况数并对10的9次方加7取余便是最终的答案,这里设P(A, n)为第n次到A点的概率,所以P(A, n) + P(B, n) + P(C, n) + P(D, n) = 1; 且由于对称性可以知道,P(A, n)  = P(B, n) = P(C, n), 即P(A, n) = 1/3 * (1 - P(D, n))(1),而第n次到达D点,说明第n - 1次在A, B, C 中的其中一点,而又由于对称性可得,P(D, n) = 1/3*(P(A, n - 1) + P(B, n - 1) + P(C, n - 1)) = P(A, n - 1); 与(1)式联立得P(D, n + 1) = 1/3 * (1 - P(D, n)); 利用高中数列知识可以求得;则第n次到达D点的方法数等于总数(3 ^ n)乘以概率,设第n次到达D点的方法数为dp[n], 则有
;这个可以直接用暴力写,也可以用矩阵快速幂写,在这就不贴代码了,不过高中学过数学竞赛的同学可能会觉得这个式子结构()有点眼熟, 3和-1的次数都是等次幂,让人联想到线性递推数列的求解 : 对于数列An = p * An-1 +  q *An-2, 其特征方程为x^2 = px + q, 假设其两根分别为x1, x2,则(x1, x2不相等时)或(x1 = x2时),a,b为常数,可由给出条件获得,所以可以把中的3和-1分别看成x1,x2,由二次方程(设为x^2 + px + q = 0)的韦达定理的p = -(x1 + x2) = -2, q = x1 *x2 = 3;即方程为x^2 - 2x - 3 = 0,而此方程为数列An = 2 * An-1 +  3 *An-2
的特征方程,由此我们可以得到一个递推关系式为dp[n] = 2 * dp[n - 1] + 3 *dp[n - 2];

引自__http://www.cnblogs.com/zyf0163/p/4318036.html

【代码】

#include<cstdio>
using namespace std;
const int N=1e7+5;
const long long mod=1e9+7;
int n;long long f[N];
int main(){
f[1]=0;f[2]=3;
scanf("%d",&n);
for(int i=3;i<=n;i++){
f[i]=2*f[i-1]+3*f[i-2];
f[i]%=mod;
}
printf("%I64d\n",f[n]);
return 0;
}

CF 166E Tetrahedron的更多相关文章

  1. CodeForces 166E -Tetrahedron解题报告

    这是本人写的第一次博客,学了半年的基础C语言,初学算法,若有错误还请指正. 题目链接:http://codeforces.com/contest/166/problem/E E. Tetrahedro ...

  2. Codeforces 矩阵题 题单

    Matrix CF 166E Tetrahedron dp方程设为 f[i] 最后在 D点,g[i] 表示最后不在D点.最后 g[] 可以通过矩阵加速数列求得,数据可以强化,复杂度 \(O(logn) ...

  3. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

  4. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  5. cf Round 613

    A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...

  6. ARC下OC对象和CF对象之间的桥接(bridge)

    在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...

  7. [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

  8. CF memsql Start[c]UP 2.0 A

    CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...

  9. CF memsql Start[c]UP 2.0 B

    CF memsql Start[c]UP 2.0 B B. Distributed Join time limit per test 1 second memory limit per test 25 ...

随机推荐

  1. 安装tensorflow出现问题的解法

    在ubuntu14.04用pip安装tensorflow-gpu 安装1.3.0遇到问题 1.安装tensorflow出现Cannot uninstall 'six'.问题的解法 https://bl ...

  2. ubuntu16.04 桌面图标左侧,右侧,底部进行切换

    转载:https://jingyan.baidu.com/article/e52e36154e6af340c60c518c.html 传统的 Unity 桌面环境,其应用程序启动器的容器——Launc ...

  3. ubuntu安装mongo数据库

    安装mongo数据库,在shell下输入 sudo apt-get install mongodb 如果需要在Python中使用mongo数据库,还需要额外安装Python封装库 pip instal ...

  4. Maven Missing artifact jar

    maven error:Multiple annotations found at this line: - Missing artifact log4j:log4j:jar:1.2.15:compi ...

  5. 深入理解Java虚拟机(一)

    一.运行时数据区域 ​ 1.程序计数器: 当前线程执行字节码的行号指示器(通过改变计数器的值来选择下条需要执行的字节码指令) 每个线程有独立的程序计数器(线程私有,为了切换线程时能恢复到挣钱的执行位置 ...

  6. Redis性能测试Redis-benchmark

    Redis-benchmark是官方自带的Redis性能测试工具 测试Redis在你的系统及你的配置下的读写性能 redis-benchmark可以模拟N个机器,同时发送M个请求 redis-benc ...

  7. BarTender中每个标签提示手动输入的设置方法

    我们知道手动输入数据进行标签打印,可以利用BarTender中的数据输入表单来实现.表单的创建有利于提示程序员,输入要打印标签的的数据.如果手动输入数据的标签较多,可以设置表单提示为每个标签提示,减去 ...

  8. Java利用while循环计算1+1/2!+1/3!……+1/20!

    编写程序,用while语句计算1+1/2!+1/3!……+1/20!,并在控制泰山输出计算结果.要求1+1/2!+1/3!……+1/20!,其实就是求1+1*1/2+1*1/2*1/3+……+1*1/ ...

  9. Bypass 360主机卫士SQL注入防御(多姿势)

    0x00 前言 在服务器客户端领域,曾经出现过一款360主机卫士,目前已停止更新和维护,官网都打不开了,但服务器中依然经常可以看到它的身影.从半年前的测试虚拟机里面,翻出了360主机卫士Apache版 ...

  10. 数组名和数组名取地址&

        在C中, 在几乎所有使用数组的表达式中,数组名的值是个指针常量,也就是数组第一个元素的地址. 它的类型取决于数组元素的类型: 如果它们是int类型,那么数组名的类型就是“指向int的常量指针“ ...