1225. Flags
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
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:
- Stripes of the same color cannot be placed next to each other.
- 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](https://i-blog.csdnimg.cn/blog_migrate/04933c84f16bded6af2f5d41645e5105.png)
Input
N, the number of the stripes, 1 ≤
N ≤ 45.
Output
M, the number of the ways to decorate the shop-window.
Sample
input | output |
---|---|
3 | 4 |
Problem Source: 2002-2003 ACM Central Region of Russia Quarterfinal Programming Contest, Rybinsk, October 2002
Tags:
dynamic programming
)
Difficulty: 56
Printable version
Submit solution
Discussion (17)
My submissions All submissions (17982) All accepted submissions (7478) Solutions rating (6251)
My submissions All submissions (17982) All accepted submissions (7478) Solutions rating (6251)
我太弱了,看了如此多稀奇古怪说不清楚的题解。。。。
还是自己想出来了,尽管题目很简单。。。
但好像我这个说的清楚一些
//该题
//1.注意long long啊!斐波那契数列到第45个正好爆int!
//2.dp[i]=dp[i-1]+dp[i-2]这个方程是这样得到的!
//原因:①当n=i-1时,最后一块旗子的颜色不是蓝色
//②所以当n=i时,最后一块旗子的颜色也不是蓝色,且若从n=i-1时得到该旗子,则最后一面旗子与n=i-1的最后一面颜色也不一样
//因而dp[i]可由dp[i-1]得到
//③当n=i时,倒数第二块旗子的颜色为蓝(即与②中的倒数第二块为红白颜色不一样),此时对最后一面旗子与倒数第三面旗子一样,
//因而dp[i]可由dp[i-2]得到
#include <iostream>
#include <cstdio>
using namespace std;
const int maxnum=45;
typedef long long ll;
ll dp[maxnum]={2,2,4};
int main()
{
int n;
cin>>n;
for(int i=3;i<45;++i)
dp[i]=dp[i-1]+dp[i-2];
cout<<dp[n-1];
return 0;
}