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}$ 为斐波 ...
随机推荐
- android加固签名工具(源码下载)
背景 每次android加固了都要命令行签名好麻烦,正好之前做了个图标生成工具. 所以改了改,比写批处理还要省事. 原理 其实就是用winform程序调用控制台执行命令,android签名的命令如下 ...
- SQL替换语句之批量修改、增加、删除字段内容
语法 REPLACE ( original-string, search-string, replace-string ) 用法 update 表的名称 set 替换字段=REPLACE(替换字段,原 ...
- js中的运动
缓慢隐藏与出现 效果: 鼠标移至分享上黄色区域自动向左隐藏. <!DOCTYPE html> <html> <head> <title></tit ...
- 1、面向对象以及winform的简单运用(开篇)
面向对象概述: 要学习好面向对象,我们应该从三个问题入手: 1.什么是面向对象? 2.为什么要面向对象? 3.该怎么面向对象? 面向对象,首先要有一个对象,那么对象是什么呢? 对象的定义是人们要进行研 ...
- jQuery理解之(二)功能函数
在javascript编程中,开发者通常需要编写很多小程序来实现一些特定的功能.例如浏览器检测,字符串处理.数组的编辑等.jQuery对这些常用的程序进行了总结,提供了很多实用的函数. 1.检测浏览器 ...
- struts2支持的结果类型
在struts2-core.jar/struts-default.xml中,我们可以找到关于result-type的一些配置信息,从中可以看出struts2组件默认为我们提供了这 些result-ty ...
- Java设计模式-适配器模式(Adapter)
适配器模式将某个类的接口转换成客户端期望的另一个接口表示,目的是消除由于接口不匹配所造成的类的兼容性问题.主要分为三类:类的适配器模式.对象的适配器模式.接口的适配器模式.首先,我们来看看类的适配器模 ...
- bootstrap table简洁扁平的表格
使用方法 1.在html页面的head标签中引入Bootstrap库(假如你的项目还没使用)和bootstrap-table.css. <link rel="stylesheet&qu ...
- 19.Android之文件存储方法学习
Android开发中会用到文件存储,今天来学习下. 先改下布局界面: <?xml version="1.0" encoding="utf-8"?> ...
- 设置div居中
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...