CodeForces 166E -Tetrahedron解题报告
这是本人写的第一次博客,学了半年的基础C语言,初学算法,若有错误还请指正。
E. Tetrahedron
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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).
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
#include <cstdio>
#include <cstring>
const int N = 1e7 + ;
const int mod = 1e9 + ;
long long dp[N], n;
void init(){
dp[] = ;
dp[] = ;
for(int i = ; i < N; i ++){
dp[i] = * dp[i - ] + * dp[i - ];
dp[i] %= mod;
}
}
int main(){
init();
while(scanf("%d", &n) == )
printf("%d\n", dp[n]);
return ;
}
不过刚开始这点思路非常复杂,所有笔者就去想dp[n] = 2 * dp[n - 1] + 3 *dp[n - 2]的含义
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int mod = 1e9 + ;
int n;
long long a, b;
int main(){
while(scanf("%d", &n) == ){
if(n < )
printf("0\n");
else if(n == )
printf("3\n");
else{
a = ;
b = ;
for(int i = ; i <= n - ; i ++){
if(i & )
a = ( * a + * b)%mod;
else
b = ( * b + * a)%mod;
}
if(n & )
printf("%I64d\n", a);
else
printf("%I64d\n", b);
}
}
return ;
}
#include <cstdio>
#include <cstring>
typedef long long lld;
const int N = 1e7 + ;
const int mod = 1e9 + ;
int dp[N][], n;
void init(){
dp[][] = ;
dp[][] = ;
dp[][] = ;
for(int i = ; i < N; i ++){
for(int j = ; j < ; j ++){
for(int k = ; k < ; k ++){
if(k == j )
continue;
dp[i][j] += dp[i - ][k];
dp[i][j] %= mod;
}
}
}
}
int main(){
init();
while(scanf("%d", &n) == )
printf("%d\n", dp[n][]);
return ;
}
其实可以从这个递推关系式可以推出dp[n] = 2 * dp[n - 1] + 3 *dp[n - 2]
#include <cstdio>
#include <cstring>
const int N = 1e7 + ;
const int mod = 1e9 + ;
long long dp[N], n;
void init(){
dp[] = ;
dp[] = ;
for(int i = ; i < N; i ++){
if(i & )
dp[i] = * dp[i - ] - ;
else
dp[i] = * dp[i - ] + ;
dp[i] %= mod;
}
}
int main(){
init();
while(scanf("%d", &n) == )
printf("%d\n", dp[n]);
return ;
}
CodeForces 166E -Tetrahedron解题报告的更多相关文章
- codeforces 31C Schedule 解题报告
题目链接:http://codeforces.com/problemset/problem/31/C 题目意思:给出 n 个 lessons 你,每个lesson 有对应的 起始和结束时间.问通过删除 ...
- codeforces 499B.Lecture 解题报告
题目链接:http://codeforces.com/problemset/problem/499/B 题目意思:给出两种语言下 m 个单词表(word1, word2)的一一对应,以及 profes ...
- codeforces 495C. Treasure 解题报告
题目链接:http://codeforces.com/problemset/problem/495/C 题目意思:给出一串只有三种字符( ')','(' 和 '#')组成的字符串,每个位置的这个字符 ...
- codeforces 490B.Queue 解题报告
题目链接:http://codeforces.com/problemset/problem/490/B 题目意思:给出每个人 i 站在他前面的人的编号 ai 和后面的人的编号 bi.注意,排在第一个位 ...
- codeforces 489A.SwapSort 解题报告
题目链接:http://codeforces.com/problemset/problem/489/A 题目意思:给出一个 n 个无序的序列,问能通过两两交换,需要多少次使得整个序列最终呈现非递减形式 ...
- codeforces 485A.Factory 解题报告
题目链接:http://codeforces.com/problemset/problem/485/A 题目意思:给出 a 和 m,a 表示第一日的details,要求该日结束时要多生产 a mod ...
- codeforces 483A. Counterexample 解题报告
题目链接:http://codeforces.com/problemset/problem/483/A 题目意思:给出一个区间 [l, r],要从中找出a, b, c,需要满足 a, b 互质,b, ...
- codeforces 479C Exams 解题报告
题目链接:http://codeforces.com/problemset/problem/479/C 题目意思:简单来说,就是有个人需要通过 n 门考试,每场考试他可以选择ai, bi 这其中一个时 ...
- codeforces 479B Towers 解题报告
题目链接:http://codeforces.com/problemset/problem/479/B 题目意思:有 n 座塔,第 i 座塔有 ai 个cubes在上面.规定每一次操作是从最多 cub ...
随机推荐
- MongoDB学习笔记——文档操作之查询
查询文档 使用db.COLLECTION_NAME.findOne()可以查询所有满足条件的第一条数据 预发格式如下: db.COLLECTION_NAME.findOne(<query> ...
- Navicat for Mysql远程连接数据时报(1045错误)Access denied for user 'root'@'localhost' (using password yes);
原因:用户访问被拒绝,更改用户赋予密码即可 mysql命令行执行语句如下 //使用mysql,读取表信息 //更改用户赋予登录密码 //更新权限 注意点:使用flush privileges是为了刷新 ...
- 有关RDS上只读实例延时分析-同适用于自建MySQL主从延时分析判断
个人不是很喜欢在技术上跟人互喷,尤其是不在同一个岗位上的人.一方面本人的性格如此,另一方面,我自身的口水也确实是不行,人生经历了第一次的双11洗礼,在大促的环境下,总算知道了有些东西是否应该规避,这次 ...
- JS高级程序设计2nd部分知识要点4
ECMAScript中所有函数的参数都是按值传递的. 5种基本数据类型: Undfined,Null,Boolean,Number,String. ECMAScript中的所有参数传递的都是值,不可能 ...
- Uva-11374-Airport Express
A - Airport Express Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Appoi ...
- Linux 磁盘与文件系统管理
介绍一本书叫<Linux 鸟哥私房菜>, 一本教人用linux很经典的一本书,这两天又看了里面的一章节,做一点笔记.有一些很细节的东西的, 在平时运用过很容易被忽略. 1)U盘使用的文件格 ...
- 【分享】iTOP-4412开发板使用之初体验[多图]
近期入手了4412开发板,配的7寸屏和WIFI模块,GPS模块,下面晒个照片介绍一下,手机拍摄图片有点模糊,实物很精致,是我所见过最好的板子.b( ̄▽ ̄)d 预装的Android4.0.3系统,5点以 ...
- Beeline known issues
If you use nohup myscript.sh , You beeline scripts may not work, Pay attention to this in your job.
- Java语言中的基本词汇
1.标识符包.类.方法.参数和变量的名称.大小写字母.数字._和$符号的组合,不以数字开始,不能使关键字,不能包括分隔符和换行.(严格区分大小写,最大长度255个字符) 2.字面量 某种类型的值(具 ...
- 【MVC 4】4.MVC 基本工具(Visual Studio 的单元测试、使用Moq)
作者:[美]Adam Freeman 来源:<精通ASP.NET MVC 4> 3.Visual Studio 的单元测试 有很多.NET单元测试包,其中很多是开源和免费的.本 ...