POJ1953 World Cup Noise

这题纯暴力会超时,只需简单推一下递推公式即可,其实就是一个斐波拉契数列。

World Cup Noise

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 15937   Accepted: 7861

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).

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.

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

Source

TUD Programming Contest 2002, Darmstadt, Germany

 1 //oimonster
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<iostream>
 5 using namespace std;
 6 int a[101];
 7 int main(){
 8     int i,j,n,k;
 9     scanf("%d",&n);
10     a[0]=0;
11     a[1]=2;
12     a[2]=3;
13     for(i=3;i<=45;i++){
14         a[i]=a[i-1]+a[i-2];
15     }
16     for(i=1;i<=n;i++){
17         scanf("%d",&k);
18         printf("Scenario #%d:\n",i);
19         printf("%d\n",a[k]);
20         printf("\n");
21     }
22     return 0;
23 }

时间: 2024-11-17 14:03:03

POJ1953 World Cup Noise的相关文章

poj 1953 world cup noise

题目大意:给出一个数n,求n位二进制中有多少个数不包含相邻的1. 思路:推出前3项后就可以发现满足斐波那契数列.先用数组记录下1~n位的结果,再通过输入的值访问相应下标的元素的值即可. #include<iostream> #include<stdio.h> #include<string.h> using namespace std; int bit[50]; void calc() { int i; bit[1]=2; bit[2]=3;//将特殊的两个值记录 fo

poj - 1953 - World Cup Noise(dp)

题意:n位长的01序列(0 < n < 45),但不能出现连续的两个1,问序列有多少种. 题目链接:http://poj.org/problem?id=1953 -->>设dp[i][j]表示前 i 位中第 i 位为 j 时的序列数,则状态转移方程为: dp[i][0] = dp[i - 1][0] + dp[i - 1][1]; dp[i][1] = dp[i - 1][0]; 因为对于相同的n,其结果是固定的,所以可以对一个n只计算一次,然后记住她.. #include <

UVa 10450 - World Cup Noise

题目:构造一个01串,使得其中的1不相邻,问长度为n的串有多少中. 分析:数学,递推数列. 设长度为n的串有n个,则有递推关系:f(n)= f(n-1)+ f(n-2): 长度为n的结束可能是0或者1: 如果结束是0,则前面是0或者是1都可以所以是f(n-1): 如果结束是1,则前面的必然是0,则更前面的随意,所以是f(n-2): 这显然是Fib的递推公式,f(n)= Fib(n+1). 说明:用long long防止溢出. #include <iostream> #include <c

poj 动态规划DP - 1953 World Cup Noise

这一题其实就是斐波那契数列,但是我一开始用的是DP,不过应该思想差不多. 一数组dp[i][2],dp[i][0]代表前i个数里面最后一个数字为0的数量,dp[i][1]代表前i个数里面最后一个数字为1的数量,可知dp[i][1]+dp[i][0]就是前i个数能组成序列的数量. 这里因为不能2个1在一起,所以: dp[i][0] = dp[i-1][0]+dp[i-1][1] dp[i][1] = dp[i-1][0] #include<stdio.h> #include<string.

北大ACM题库习题分类与简介(转载)

在百度文库上找到的,不知是哪位大牛整理的,真的很不错! zz题 目分类 Posted by fishhead at 2007-01-13 12:44:58.0 -------------------------------------------------------------------------------- acm.pku.edu.cn 1. 排序 1423, 1694, 1723, 1727, 1763, 1788, 1828, 1838, 1840, 2201, 2376, 23

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

ACM训练方案-POJ题目分类

ACM训练方案-POJ题目分类 博客分类: 算法 ACM online Judge 中国: 浙江大学(ZJU):http://acm.zju.edu.cn/ 北京大学(PKU):http://acm.pku.edu.cn/JudgeOnline/ 杭州电子科技大学(HDU):http://acm.hdu.edu.cn/ 中国科技大学(USTC):http://acm.ustc.edu.cn/ 北京航天航空大学(BUAA)http://acm.buaa.edu.cn/oj/index.php 南京

转载:poj题目分类(侵删)

转载:from: POJ:http://blog.csdn.net/qq_28236309/article/details/47818407 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K–0.50K:中短代码:0.51K–1.00K:中等代码量:1.01K–2.00K:长代码:2.01K以上. 短:1147.1163.1922.2211.2215.2229.2232.2234.2242.2245.2262.2301.2309.2313.2334.2346.2348

poj 动态规划题目列表及总结

此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276,1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740(博弈),1742, 1887, 1926(马尔科夫矩阵,求平衡), 1936, 1952, 1953, 1958, 1959, 1962, 1975,