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 ...
随机推荐
- Java中的 WeakReference 和 SoftReference
我们知道Java语言中没有指针,取而代之的是引用reference.Java中的引用又可以分为四种:强引用,弱引用(WeakReference),软引用(SoftReference),虚引用(Phan ...
- GDI学习之俄罗斯方块
做个玩玩 public Form1() { InitializeComponent(); } #region 定义砖块int[i,j,y,x] Tricks:i为那块砖,j为状态,y为列,x为行 pr ...
- DevExpress GridControl使用方法总结(转)
一.如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 二.如何新增一条记录 (1).gridView.AddN ...
- Python Tomcat Script(多实例)
之前书写过 Tomcat 单实例的 Python 脚本,本次增加 Tomcat 多实例的操作脚本. 1:准备 安装所需 Python 插件 A方法: pip install argparse B方法: ...
- 在VMware Workstation11虚拟机上安装黑苹果
图文详解如何在VMware Workstation11虚拟机上安装黑苹果Mac OS X 10.10系统-网络教程与技术 -亦是美网络 http://www.yishimei.cn/network/5 ...
- JS键盘事件监听
window.onload=function(){ var keyword = document.getElementById("keyword"); keyword.onkeyu ...
- 嵌入式开发板iTOP4412学习开发板
网站:http://www.topeetboard.com 淘宝:https://item.taobao.com/item.htm?_u=okcahs0f42a&id=38712193806 ...
- 使用flume的一个例子
新项目中需要使用到hadoop和vertica,使用flume把数据加载到hadoop中,我做了一个例子, 即监控一个sharefolder,如果里面有文件,则会文件load到hadoop. 开启Fl ...
- NOIP2008提高组火柴棒等式(模拟)——yhx
题目描述 给你n根火柴棍,你可以拼出多少个形如“A+B=C”的等式?等式中的A.B.C是用火柴棍拼出的整数(若该数非零,则最高位不能是0).用火柴棍拼数字0-9的拼法如图所示: 注意: 加号与等号各自 ...
- docker containerd中的容器操作
containerd的中的各种操作都是通过Task来进行的,因此对于容器的create, start, delete等等操作其实都是一个个的Task而已. Task的数据结构如下所示: type Ta ...