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. 用python爬校花网

    import requests import re import hashlib,time def get_index(url): response=requests.get(url) if resp ...

  2. ClassCastException: org.apache.tomcat.websocket.server.WsServerContainer cannot be cast to javax.websocket.server.ServerContainer

    21:09:22.221 [MessageBroker-3] INFO c.t.s.s.impl.StockNewsServiceImpl - [2017-12-16 21:09:22] execut ...

  3. 【bzoj1593-预定旅馆】线段树维护连续区间

    题解: 这题非常经典啊似乎..经典模型要记住啊.. 对于每个节点维护该区间里的最大的连续区间,然后我们就可以logn递归找最前面的一段. 那就维护mx(无限制),lmx(必须从左边开始),rmx(必须 ...

  4. Java面试通关要点汇总集(山东数漫江湖)

    这里,笔者结合自己过往的面试经验,整理了一些核心的知识清单,帮助读者更好地回顾与复习 Java 服务端核心技术.本文会以引出问题为主,后面有时间的话,笔者陆续会抽些重要的知识点进行详细的剖析与解答.敬 ...

  5. 【1】记一次破解wifi

    当然,使用的依旧是aircrack套件,这次依旧是跑字典,今天,捉到了另一个实验室icephone的wpa握手包,我猜测实验室的wifi一般都跟自己的名字有关,icephone刚好是8位字母,于是我就 ...

  6. 从零开始PHP攻略(001)——Bob的汽车零部件商店

    1.创建订单表单 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  7. mysql之基本数据库操作(二)

    环境信息 数据库:mysql-5.7.20 操作系统:Ubuntu-16.04.3 mysql的启动.退出.重启 # 启动 $ sudo service mysqld start # 停止 $ sud ...

  8. 网络知识===wireshark抓包出现“TCP segment of a reassembled PDU”的解释(载)

    网上胡说八道,众说风云,感觉这篇还算靠谱点. 原文链接:http://blog.csdn.net/dog250/article/details/51809566 为什么大家看到这个以后总是会往MSS, ...

  9. Oracle 内存管理

    --内存分配建库时可以先分配系统内存的50%-80%给Oracle,后期根据业务再进行调整.SGA.PGA分配比例:OLTP:SGA %80 , PGA %20OLAP:SGA %50 , PGA % ...

  10. 【python】资料记录

    今天看了一些关于python的知识: 1.装饰器:https://www.zhihu.com/question/25950466/answer/31731502 2.*args的用法:http://b ...