DP(1)

Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

 Status

Description

On the Day of the Flag of Russia a shop-owner decided to decorate the show-window of his shop with textile stripes of white, blue and red colors. He wants to satisfy the following conditions:
  1. Stripes of the same color cannot be placed next to each other.
  2. A blue stripe must always be placed between a white and a red or between a red and a white one.
Determine the number of the ways to fulfill his wish.
Example. For N = 3 result is following:
Problem illustration

Input

N, the number of the stripes, 1 ≤ N ≤ 45.

Output

M, the number of the ways to decorate the shop-window.

Sample Input

inputoutput
3
4

Source

Problem Source: 2002-2003 ACM Central Region of Russia Quarterfinal Programming Contest, Rybinsk, October 2002 
 
n 1 2 3 4 5 6 7 8 9 10
a[n] 2 2 4 6 10 16 26 42 68 110
#include<cstdio>
using namespace std;
long long a[50];
int main(){
    int n;
    while(~scanf("%d",&n)){
    a[1] = 2;
    a[2] = 2;
    for(int i = 3; i <= n; i++)
        a[i] = a[i-1] + a[i-2];
    printf("%I64d\n",a[n]);
    }
    return 0;
}

0 ~ red; 1 ~ white; x ~ bule

if( n == 3 && tail == 0) 1 x 0 , 0 1 0;dp[i][0] = dp[i-1][1] + dp[i-2][1];

else if(n == 3 && tail == 1) 0 x 1, 1 0 1;dp[i][1] = dp[i-1][0] + dp[i-2][0];

#include<cstdio>
#define ll __int64
using namespace std;
ll dp[50][2];
int main(){
    int n;
    while(~scanf("%d",&n)){
        for(int i = 1; i <= n; i++)
            dp[i][0] = dp[i][1] = 0;
        dp[1][0] = 1;
        dp[1][1] = 1;
        dp[2][0] = 1;
        dp[2][1] = 1;
        for(int i = 3; i <= n; i++){
            dp[i][0] = dp[i-1][1] + dp[i-2][1];
            dp[i][1] = dp[i-1][0] + dp[i-2][0];
        }
        printf("%I64d\n",dp[n][0] + dp[n][1]);
    }
    return 0;
}

由于 : dp[i][0] = dp[i][1]; 两式可以合并,也就推出了第一个递推公式!

posted @ 2015-08-27 10:32  Tobu  阅读(153)  评论(0)    收藏  举报