World Cup Noise
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 17674 | Accepted: 8615 |
Description
Background
"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).
"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
The first line contains the number of scenarios.
For each scenario, you are given a single positive integer less than 45 on a line by itself.
For each scenario, you are given a single positive integer less than 45 on a line by itself.
Output
The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the number of n-bit sequences which have no adjacent 1's. Terminate the output for the scenario with a blank line.
Sample Input
2 3 1
Sample Output
Scenario #1: 5 Scenario #2: 2
这题开始看了一下,准备用枚举写的,用二进制一个一个判断。后来才明白题目的意思,即给一个数n,一排n个方格,给方格填数字0和1,1和1不能相邻。这样就简单多了,常规的递推题,以前在HDU上也写过一道差不多的题。水题水题。假设第一个方格是1,那么第二位只能是0,则还有f(n-2)种排法,假设第一个方格是0,后面的无限制,则有f(n-1)种方法,综合起来就是dp方程dp(n)=dp(n-1)+dp(n-2)。典型的斐波那契数列。值得注意的是45可能有点大,要用long long存,以下是我的代码
#include <stdio.h>
int main()
{
int t,digit;
long long arr[50];
arr[1]=2;
arr[2]=3;
for(int i=3; i<50; i++)
arr[i]=arr[i-1]+arr[i-2];
scanf("%d",&t);
for(int i=1; i<=t; i++)
{
scanf("%d",&digit);
printf("Scenario #%d:\n%lld\n\n",i,arr[digit]);
}
return 0;
}
输出有点坑,要两个换行符,错过一次。