2171: An Easy Problem!
Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
![]() | 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; }