Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
3s | 8192K | 952 | 230 | Standard |
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).
1 2 3
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; }