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. PAT A1026 Table Tennis (30 分)——队列

    A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For a ...

  2. Libgdx学习记录28——创建Desktop程序

    1.新建Java Project. 2.添加libs,添加相关的jar文件. 3. 在Project Build Path中,添加Reference. 4. 添加文件夹assets,并右键Build ...

  3. VitualBox安装linux记录

    下载镜像 CentOS 7镜像下载 阿里云站点:http://mirrors.aliyun.com/centos/7/isos/x86_64/ VirtualBox安装linux https://ww ...

  4. CentOS搭建NAT和DHCP服务,实现共享上网

    什么是NAT? NAT(Network address translation)即网络地址转换,作为一种过渡解决手段,可以用来减少对全球合法IP地址的需求.简单的说,NAT就是在内部专用网络中使用内部 ...

  5. Python_命名空间和作用域_25

    # 函数进阶 a = def func(): print(a) func() # 命名空间和作用域 # print() # input() # list # #命名空间 有三种 #内置命名空间 —— ...

  6. 全景3d

    Three.js Tour.js Run.js 3D Css3 酷家乐:https://www.kujiale.com/ 爱空间:http://bj.ikongjian.com/?utm_source ...

  7. 10-Python3从入门到实战—基础之函数

    Python从入门到实战系列--目录 函数的定义 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数的语法 def 函数名(参数列表): 函数体 函数代码块以 def 关键词开头 ...

  8. [BUAA_SE_2017]第零次博客

    结缘计算机 你为什么选择计算机专业?你认为条件如何? 计算机是你喜欢的领域吗?是你擅长的领域吗? 说来也巧,高考选择专业时并不知道自己会这般喜欢计算机这个专业,却将其填在了北航的第一志愿. 第一次接触 ...

  9. Linux recursively find files

    https://stackoverflow.com/questions/5905054/how-can-i-recursively-find-all-files-in-current-and-subf ...

  10. CentOS7 卸载mariadb 安装mysql的过程:

    1. 检查安装的mariadb rpm -qa |grep mariadb 得到已经安装的安装包 mariadb-libs-5.5.56-2.el7.x86_64mariadb-devel-5.5.5 ...