E. Tetrahedron(数学推导)
2 seconds
256 megabytes
standard input
standard output
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 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).
The first line contains the only integer n (1 ≤ n ≤ 107) — the required length of the cyclic path.
Print the only integer — the required number of ways modulo 1000000007 (109 + 7).
2
3
4
21
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种。有了这个公式,最后打表即可。
- #include<cstdio>
- #include<iostream>
- using namespace std;
- int main()
- {
- unsigned int n;
- int i;
- long long f[10000001];
- f[1] = 0;
- f[2] = 3;
- f[3] = 6;
- for(i=4; i<10000001;i++)
- {
- f[i] = f[i-1] * 2 + f[i-2] * 3;
- f[i] %= 1000000007;
- }
- scanf("%d", &n);
- cout<<f[n]<<endl;
- return 0;
- }
E. Tetrahedron(数学推导)的更多相关文章
- 借One-Class-SVM回顾SMO在SVM中的数学推导--记录毕业论文5
上篇记录了一些决策树算法,这篇是借OC-SVM填回SMO在SVM中的数学推导这个坑. 参考文献: http://research.microsoft.com/pubs/69644/tr-98-14.p ...
- 关于不同进制数之间转换的数学推导【Written By KillerLegend】
关于不同进制数之间转换的数学推导 涉及范围:正整数范围内二进制(Binary),八进制(Octonary),十进制(Decimal),十六进制(hexadecimal)之间的转换 数的进制有多种,比如 ...
- UVA - 10014 - Simple calculations (经典的数学推导题!!)
UVA - 10014 Simple calculations Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...
- 『sumdiv 数学推导 分治』
sumdiv(POJ 1845) Description 给定两个自然数A和B,S为A^B的所有正整数约数和,编程输出S mod 9901的结果. Input Format 只有一行,两个用空格隔开的 ...
- LDA-线性判别分析(二)Two-classes 情形的数学推导
本来是要调研 Latent Dirichlet Allocation 的那个 LDA 的, 没想到查到很多关于 Linear Discriminant Analysis 这个 LDA 的资料.初步看了 ...
- leetcode 343. Integer Break(dp或数学推导)
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- [hdu5307] He is Flying [FFT+数学推导]
题面 传送门 思路 看到这道题,我的第一想法是前缀和瞎搞,说不定能$O\left(n\right)$? 事实证明我的确是瞎扯...... 题目中的提示 这道题的数据中告诉了我们: $sum\left( ...
- ZOJ3329(数学推导+期望递推)
要点: 1.期望的套路,要求n以上的期望,则设dp[i]为i分距离终点的期望步数,则终点dp值为0,答案是dp[0]. 2.此题主要在于数学推导,一方面是要写出dp[i] = 什么,虽然一大串但是思维 ...
- [国家集训队]整数的lqp拆分 数学推导 打表找规律
题解: 考场上靠打表找规律切的题,不过严谨的数学推导才是本题精妙所在:求:$\sum\prod_{i=1}^{m}F_{a{i}}$ 设 $f(i)$ 为 $N=i$ 时的答案,$F_{i}$ 为斐波 ...
随机推荐
- sql server 2008 登录 4064 错误解决办法
出现这个错误是账户无法打开默认数据库导致的 修改一下该账户的默认打开数据库即可. 如果很不幸,你的sa帐号和windows身份验证默认都打开同一个数据库,那么无论换哪种方式登录都是一样没用的 如果你有 ...
- 第八章 self sizing cell
本项目是<beginning iOS8 programming with swift>中的项目学习笔记==>全部笔记目录 ------------------------------ ...
- 从实用主义深入理解c++虚函数
记得几个月前看过C++虚函数的问题,当时其实就看懂了,最近笔试中遇到了虚函数竟然不太确定,所以还是理解的不深刻,所以想通过这篇文章来巩固下. 装逼一刻: 最近,本人思想发生了巨大的转变,在大学的时候由 ...
- WCF入门(11)
前言 终于出太阳了. 可惜风太大,凉在阳台上的衣服全被吹到了地上,外加几双袜子被吹掉了,gone. 第11集 Difference between DataContract and MessageCo ...
- 第十八课:js样式操作需要注意的问题
样式分为,外部样式(<link />),内部样式(<style></style>),行内样式(style:).再加上一个important对选择器权重的干扰. 大体 ...
- NABCD分析java音乐播放器
程设计题目:java音乐播放器 一.课程设计目的 1.编程设计音乐播放软件,使之实现音乐播放的功能. 2.培养学生用程序解决实际问题的能力和兴趣. 3.加深java中对多媒体编程的应用. 二.课程设计 ...
- IntelliJ IDEA 设置 编辑器字体大小
1,打开File->settings 2,在Edit->colors->Fonts下创建新字体 保存即可.
- Maven-改变本地存储仓库位置
修改 maven 仓库存放位置: 找到 maven 下的 conf 下的 settings.xml 配置文件,假设maven安装在D:\Server目录中.那么配置文件应该在 D:\Server\ma ...
- 人工鱼群算法-python实现
AFSIndividual.py import numpy as np import ObjFunction import copy class AFSIndividual: "" ...
- BZOJ-1024 生日快乐 DFS+一丝sb的数学思考
1024: [SCOI2009]生日快乐 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 1948 Solved: 1391 [Submit][Statu ...