zoukankan      html  css  js  c++  java
  • codeforces166E

    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=0;
        bool f=0;
        char ch=' ';
        while(!isdigit(ch))
        {
            f|=(ch=='-'); ch=getchar();
        }
        while(isdigit(ch))
        {
            s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
        }
        return (f)?(-s):(s);
    }
    #define R(x) x=read()
    inline void write(ll x)
    {
        if(x<0)
        {
            putchar('-'); x=-x;
        }
        if(x<10)
        {
            putchar(x+'0'); return;
        }
        write(x/10);
        putchar((x%10)+'0');
        return;
    }
    #define W(x) write(x),putchar(' ')
    #define Wl(x) write(x),putchar('
    ')
    const int N=100005;
    int n;
    int a[N],A[N],Len[N];
    int main()
    {
        int i,j,ans=1;
        R(n);
        if(n<=2) {Wl(n); return 0;}
        for(i=1;i<=n;i++)
        {
            R(a[i]);
        }
        *A=0;
        for(i=1;i<=n;i++)
        {
            if(a[i]>0)
            {
                A[++*A]=a[i];
                Len[*A]=1;
            }
            else
            {
                A[++*A]=0;
                for(;i<=n&&a[i]==0;i++) Len[*A]++;
                i--;
            }
        }
        for(i=1;i<=n;i++) ans=max(ans,Len[i]);
        if(*A==1) ans=n;
        if(*A==2)
        {
            if(A[1]==0) ans=max(Len[1],Len[2]+1);
            else ans=Len[2];
        }
        for(i=1;i<=(*A)-2;i++)
        {
            int tmp;
            if(A[i]==0)
            {
                tmp=Len[i+1]+1;
            }
            else if(A[i+1]==0)
            {
                if(Len[i+1]==1) tmp=Len[i+1]+1;
                else
                {
                    tmp=1+Len[i+2];
                    for(j=i+3;j<=*A;j++)
                    {
                        if(A[j]==A[j-1]+A[j-2]) tmp+=Len[j];
                        else break;
                    }
                    ans=max(ans,tmp);
                    continue;
                }
            }
            else tmp=Len[i]+Len[i+1];
            for(j=i+2;j<=*A;j++)
            {
                if(A[j]==A[j-1]+A[j-2]) tmp+=Len[j];
                else break;
            }
            ans=max(ans,tmp);
        }
        Wl(ans);
        return 0;
    }
    /*
    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
    */
    View Code
  • 相关阅读:
    【干货分享】流程DEMO-制度发文和干部任免
    如何让流程表单禁用选人控件,但可以通过代码赋值?
    让任正非愤怒的到底是华为财管团队还是流程本身?
    品牌营销:不要Beat,要逼格!
    流程再造:以信息化管理解救“中国制造”
    三星Note 7停产,原来是吃了流程的亏
    BAT“搅局”B2B市场,CIO们准备好了吗?
    胡理辉:风电王国里的流程管控人
    周义:建设流程管理系统 我还在不停努力
    步步高彭雄:弄潮“互联网+零售”从端到端流程开始
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/10617507.html
Copyright © 2011-2022 走看看