http://poj.org/problem?id=1953
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 13171 | Accepted: 6520 |
Description
"KO-RE-A, KO-RE-A" shout 54.000 happy football fans after their team has reached the semifinals of the FIFA World Cup in their home country. But although their excitement is real, the Korean people are still very organized by nature. For example, they have organized huge trumpets (that sound like blowing a ship's horn) to support their team playing on the field. The fans want to keep the level of noise constant throughout the match.
The trumpets are operated by compressed gas. However, if you blow the trumpet for 2 seconds without stopping it will break. So when the trumpet makes noise, everything is okay, but in a pause of the trumpet,the fans must chant "KO-RE-A"!
Before the match, a group of fans gathers and decides on a chanting pattern. The pattern is a sequence of 0's and 1's which is interpreted in the following way: If the pattern shows a 1, the trumpet is blown. If it shows a 0, the fans chant "KO-RE-A". To ensure that the trumpet will not break, the pattern is not allowed to have two consecutive 1's in it.
Problem
Given a positive integer n, determine the number of different chanting patterns of this length, i.e., determine the number of n-bit sequences that contain no adjacent 1's. For example, for n = 3 the answer is 5 (sequences 000, 001, 010, 100, 101 are acceptable while 011, 110, 111 are not).
Input
For each scenario, you are given a single positive integer less than 45 on a line by itself.
Output
Sample Input
2 3 1
Sample Output
Scenario #1: 5 Scenario #2: 2
Source
题目要求的是,长度为n的01序列,没有相邻的两个1,这种序列有多少种。
使用递推来解决,设长度为n的没有相邻1的01序列总数为f[n]。假设序列中最后一个元素为0,则前面n-1个元素只要本身满足没有相邻的两个1就可以,所以最后一个元素为0的情况有f[n-1]种;若最后一个元素为1,则倒数第二个元素必须为0,则前面n-2个元素只要本身满足没有相邻的两个1就可以,所以最后一个元素为1的情况有f[n-2]种。综合这两种情况,得到递推公式f[n]=f[n-1]+f[n-2],婓波拉契数列是一样的。
#include<stdio.h>
int main()
{
}