joj2171

 2171: An Easy Problem!


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s 8192K 952 230 Standard
It's an easy problem!

N-bit sequences is a string of digitals which contains only '0' and '1'. You should determine the number of n-bit sequences that contain no three continuous 1's. For example, for n = 3 the answer is 7 (sequences 000, 001, 010, 011, 100, 101, 110 are acceptable while 111 is not).

Input

For each line, you are given a single positive integer N no more than 40 on a line by itself.

Output

Print a single line containing the number of n-bit sequences which have no three continuous 1's.

Sample Input

1
2
3

Sample Output

2
4
7

Problem Source: sea


这个一个动态规划的问题,将三个数看做一个组合进行分析



#include<stdio.h>
#include<iostream>
using namespace std;

int n;
int main()
{
    while(scanf("%d",&n)==1)
    {
        long long b[5];
        long long a[]={4,2,1};
        if(n==1)cout<<2<<endl;
        else if(n==2)cout<<4<<endl;
        else if(n==3)cout<<7<<endl;
        else
        {
            n=n-3;
            while(n>=3)
            {
                b[0]=2*(a[0]+a[1]+a[2])+a[0]+a[1]+a[0];
                b[1]=a[0]+a[1]+a[2]+a[0]+a[1];
                b[2]=a[0]+a[1]+a[2];
                a[0]=b[0];
                a[1]=b[1];
                a[2]=b[2];
                n=n-3;
            }
            if(n==2)
            {
                a[0]=a[0]*4;
                a[1]=a[1]*3;
                a[2]=a[2]*2;
            }
            else if(n==1)
            {
                a[0]=a[0]*2;
                a[1]=a[1]*2;
                a[2]=a[2]*1;
            }
            cout<<a[0]+a[1]+a[2]<<endl;
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值