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 Dto 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
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

【题意】给你一个四面体,每一个顶点可以走一步到达他相邻的顶点,每一步必须走,不能停留在原地。问你从D点出发经过n步再回到

D点的方案数是多少。

【分析】简单DP,dp[i][j]表示第j步走到顶点i的方案数,然后用其他三个顶点更新就行了。但是...会MLE。我们发现每一种状态只与他前一步的状态有关,之前的没用了,空间浪费,所以可以考虑用滚动数组。

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
using namespace std;
typedef long long LL;
const int N = 1e7+;
const int mod = 1e9+;
int n;
LL dp[][];
int main(){
scanf("%d",&n);
dp[][]=dp[][]=dp[][]=;
for(int i=;i<=n;i++){
int k=i&;
dp[][k]=(dp[][k^]+dp[][k^]+dp[][k^])%mod;
dp[][k]=(dp[][k^]+dp[][k^]+dp[][k^])%mod;
dp[][k]=(dp[][k^]+dp[][k^]+dp[][k^])%mod;
dp[][k]=(dp[][k^]+dp[][k^]+dp[][k^])%mod;
}
printf("%lld\n",dp[][n&]);
return ;
}

Codeforces Round #113 (Div. 2) Tetrahedron(滚动DP)的更多相关文章

  1. Codeforces Round #113 (Div. 2)

    Codeforces Round #113 (Div. 2) B. Polygons 题意 给一个\(N(N \le 10^5)\)个点的凸包 \(M(M \le 2 \cdot 10^4)\)次询问 ...

  2. Tetrahedron(Codeforces Round #113 (Div. 2) + 打表找规律 + dp计数)

    题目链接: https://codeforces.com/contest/166/problem/E 题目: 题意: 给你一个三菱锥,初始时你在D点,然后你每次可以往相邻的顶点移动,问你第n步回到D点 ...

  3. Codeforces Round #131 (Div. 1) B. Numbers dp

    题目链接: http://codeforces.com/problemset/problem/213/B B. Numbers time limit per test 2 secondsmemory ...

  4. Codeforces Round #131 (Div. 2) B. Hometask dp

    题目链接: http://codeforces.com/problemset/problem/214/B Hometask time limit per test:2 secondsmemory li ...

  5. Codeforces Round #276 (Div. 1) D. Kindergarten dp

    D. Kindergarten Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/proble ...

  6. Codeforces Round #260 (Div. 1) A - Boredom DP

    A. Boredom Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/problem/A ...

  7. Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS

    题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...

  8. Codeforces Round #539 (Div. 2) 异或 + dp

    https://codeforces.com/contest/1113/problem/C 题意 一个n个数字的数组a[],求有多少对l,r满足\(sum[l,mid]=sum[mid+1,r]\), ...

  9. Codeforces Round #374 (Div. 2) C. Journey DP

    C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...

随机推荐

  1. js高阶函数--判断数据类型、函数胡柯里化;

    一.判断数据类型: 常见的判断有typeof.instanceof. constructor. prototype,先来看typeof: var a = "hello world" ...

  2. Java 必看的 Spring 知识汇总

    Spring框架是由于软件开发的复杂性而创建的.Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情.然而,Spring的用途不仅仅限于服务器端的开发.从简单性.可测试性和松耦 ...

  3. MySQL 基于 GTID 主从架构添加新 Slave 的过程

    内容全部来自: How to create/restore a slave using GTID replication in MySQL 5.6 需求说明 需求: 对于已经存在的 MySQL 主从架 ...

  4. grunt、Browsersync服务及weinre远程调试

    一.grunt server服务 前端开发时,经常需要把静态文件映射成web服务,传统的做法是丢到apache,但太重太不友好了.开发angular的时候,官方的chrome插件对file:///的支 ...

  5. js基础知识点收集

    js基础知识点收集 js常用基本类型 function show(x) { console.log(typeof(x)); // undefined console.log(typeof(10)); ...

  6. https 多路复用的理解~转载

    https://segmentfault.com/q/1010000005167289 这里面的http/2连接是指tcp/ip层的连接还是http应用层(也就是我们平常在chrome F12 net ...

  7. 细数雷军系成员,27家公司3家IPO

    自 2004 年至今,作为天使投资人和顺为基金创始合伙人,雷军共投了移动互联网.电子商务.互联网社区等领域内的 27 家创业公司,其中欢聚时代.猎豹移动.迅雷三家公司成功上市.小米科技虽然还未 IPO ...

  8. python基础===Sublime Text 3 快捷键

    选择类 Ctrl+D 选中光标所占的文本,继续操作则会选中下一个相同的文本. Alt+F3 选中文本按下快捷键,即可一次性选择全部的相同文本进行同时编辑.举个栗子:快速选中并更改所有相同的变量名.函数 ...

  9. linux===Ubuntu修改设备名称

    step1 sudo vim /etc/hostname 修改你需要的,保存退出 step2 sudo vim /etc/hosts 修改你需要的,保存退出 step3 reboot

  10. Git 常用命令速查表(图文+表格)【转】

    转自:http://www.jb51.net/article/55442.htm 一. Git 常用命令速查 git branch 查看本地所有分支git status 查看当前状态 git comm ...