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. UFLDL教程练习(exercise)答案(2)

    主成分分析与白化,这部分很简单,当然,其实是用Matlab比较简单,要是自己写SVD分解算法,足够研究好几个月的了.下面是我自己实现的练习答案,不保证完全正确,不过结果和网站上面给出的基本一致. 1. ...

  2. SharePoint PowerShell使用Export-SPWeb和Import-SPWeb指令来导出和导入网站

    导出网站,例如: Export-SPWeb -Identity http://win2012sp2013:1000/Hopewell_Portal/ -Path "C:\KenmuTemp\ ...

  3. 在Unity3d中调用外部程序及批处理文件

    如果调用外部普通应用程序, 比如notepad.exe 这样调用 static public bool ExecuteProgram(string exeFilename, string workDi ...

  4. False 'Sharing Violation' Xcopy error message

    今天想要将QC的新工具自动拷贝到p4 用户机器上使用,为了避免每次通知大家升级啊!!! 于是,我在程序里调用了bat文件,执行拷贝操作,想在默默的情况下替换更新新版本工具,结果我测试发现没能成功更新版 ...

  5. QT QGraphicsProxyWidget对象可选择或移动的一些tricks

    我在QT图形视图框架中使用QGraphicsProxyWidget嵌入widget,但是无法使其和其它的QGraphicsItem一样可以选择或移动,使用如下语句无效:  C++ Code  1234 ...

  6. 【笔试面试】神马搜索C++程序猿电话面试

    面试时间:2015.07.15 预约时间:2015.07.14.电话面试前一天,会电话咨询你方面电话面试的时间. 面试环节: 无自我介绍(这是我面试这么多家公司碰到的第一次),直接面试内容. 问题1: ...

  7. Android Studio 视图解析

    AS一共同拥有三种视图.我们来分别分析每一种视图的作用. 一.Project视图.(白色字体的文件夹/文件可不关注) 图片中的链接 Gralde介绍:http://stormzhang.com/dev ...

  8. Go之对象拷贝

    这里interface{}就相当于c#,java中的object, boy := util.Boy{util.Person{"Eric", 19, "boy"} ...

  9. 【代码审计】LaySNS_v2.2.0 前台XSS跨站脚本漏洞

      0x00 环境准备 LaySNS官网:http://www.laysns.com/ 网站源码版本:LaySNS_v2.2.0 程序源码下载:https://pan.lanzou.com/i0l38 ...

  10. CentOS7--Firewalld防火墙

    Firewalld服务是红帽RHEL7系统中默认的防火墙管理工具,特点是拥有运行时配置与永久配置选项且能够支持动态更新以及"zone"的区域功能概念,使用图形化工具firewall ...