Tetrahedron

CodeForces - 166E

You are given a tetrahedron. Let's mark its vertices with letters ABC and Dcorrespondingly.

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).

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

sol:直接dp丝毫不慌,dp[i][0/1/2/3]表示第i步,当前位于节点j的方案数

#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n;
int a[N],A[N],Len[N];
int main()
{
int i,j,ans=;
R(n);
if(n<=) {Wl(n); return ;}
for(i=;i<=n;i++)
{
R(a[i]);
}
*A=;
for(i=;i<=n;i++)
{
if(a[i]>)
{
A[++*A]=a[i];
Len[*A]=;
}
else
{
A[++*A]=;
for(;i<=n&&a[i]==;i++) Len[*A]++;
i--;
}
}
for(i=;i<=n;i++) ans=max(ans,Len[i]);
if(*A==) ans=n;
if(*A==)
{
if(A[]==) ans=max(Len[],Len[]+);
else ans=Len[];
}
for(i=;i<=(*A)-;i++)
{
int tmp;
if(A[i]==)
{
tmp=Len[i+]+;
}
else if(A[i+]==)
{
if(Len[i+]==) tmp=Len[i+]+;
else
{
tmp=+Len[i+];
for(j=i+;j<=*A;j++)
{
if(A[j]==A[j-]+A[j-]) tmp+=Len[j];
else break;
}
ans=max(ans,tmp);
continue;
}
}
else tmp=Len[i]+Len[i+];
for(j=i+;j<=*A;j++)
{
if(A[j]==A[j-]+A[j-]) tmp+=Len[j];
else break;
}
ans=max(ans,tmp);
}
Wl(ans);
return ;
}
/*
input
10
1 2 3 5 8 13 21 34 55 89
output
10 input
5
1 1 1 1 1
output
2 input
10
1 1 0 0 0 0 0 0 0 1
output
7
*/

codeforces166E的更多相关文章

随机推荐

  1. MySQL 基础七 视图

    -- 查看表 SELECT * FROM student; SHOW CREATE TABLE student; -- 创建视图 CREATE VIEW v_student1 AS SELECT *F ...

  2. jmeter(二十)阶梯式加压测试

    性能测试中,有时需要模拟一种实际生产中经常出现的情况,即:从某个值开始不断增加压力,直至达到某个值,然后持续运行一段时间. 在jmeter中,有这样一个插件,可以帮我们实现这个功能,这个插件就是:St ...

  3. [01] 浅谈RESTful风格的API

    1.什么是RESTful风格的API REST,即Representational State Transfer,可以理解为"(资源的)表现层状态转化". 在网络上,我们通过浏览器 ...

  4. 移动电力猫HG260GT pon实现路由拨号

    帐号CMCCAdmin密码aDm8H%MdA 需要将原来上网的路由模式改成如下图中的桥接模式 实际应该就是将上网vlan连接到了1号口,这样路由就可以通过一号口接入拨号了 修改后再通过无线接入路由就不 ...

  5. SessionState in ASP.NET Core(转载)

    问: In asp.net mvc we used to decorate controller for disabling session state by using attribute as [ ...

  6. java jdk 配置

    1.配置 C:\Program Files\Java\jdk1.8.0_131\bin 路径 到环境变量 Path

  7. Intel x86_64 Architecture Background 3

    多层次的cache结构解决了CPU和DRAM之间处理速度不一致的问题,在Intel体系架构下,CPU核心和主存DRAM之间有着三层的cache.其中一层缓存L1和二层缓存L2在CPU核心(core)中 ...

  8. Python 学习 第八篇:函数2(参数、lamdba和函数属性)

    函数的参数是参数暴露给外部的接口,向函数传递参数,可以控制函数的流程,函数可以0个.1个或多个参数:在Python中向函数传参,使用的是赋值方式. 一,传递参数 参数是通过赋值来传递的,传递参数的特点 ...

  9. markdown操作手册

    **1.标题** # h1 h1自带分割线 ## h2 ### h3 #### h4 ##### h5 ###### h6 **2.圆点** - 圆点 **3.分割线,-和*都可以** --- *** ...

  10. Runaway argument错误 [Overleaf: 在线Latex] [Type 3问题后续]

    [背景与问题描述] 在Latex中,经常出现各种问题: Runaway argument? {\contentsline {subsection}{\numberline {6.3}General c ...