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
  • 相关阅读:
    vector与iterator的一些用法
    动态规划 hdu 1024
    dfs bfs hdu 1045
    hdu 2795
    poj 2828
    线段树染色
    线段树比大小
    A
    ?线程局部变量
    JRE、JDK、JVM 及 JIT
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/10617507.html
Copyright © 2011-2022 走看看