POJ1953 World Cup Noice




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
思路:
利用斐波拉契
#include<iostream>
#include <string>
using namespace std;
int main()
{
    int number;///方案数
    int n;
    int f[50],a[50];
    int count;
    f[0]=0,f[1]=2,f[2]=3;
    for(int i=3;i<=46;i++)
    {
        f[i]=f[i-1]+f[i-2];
    }
    cin>>number;
    for(int i=1;i<=number;i++)
    {
        cin>>a[i]; ///储存方案数
    }
    for(int i=1;i<=number;i++)
    {
        count=a[i];
        cout<<"Scenario #"<<i<<":"<<endl;
        cout<<f[count]<<endl;
        cout<<endl;
    }
}

<think>好的,我现在要解决用户关于北京大学POJ平台问题1953的疑问。用户需要了解这个问题的解题思路或者相关讨论。首先,我需要明确问题1953的具体内容,可能是什么题目呢? 根据用户提供的引用内容中的Java代码,我看到代码中有一个数组a,初始值a[0]=1,a[1]=2,然后通过循环计算a[i] = a[i-1] + a[i-2]。这看起来像是斐波那契数列的变种。再看输出部分,用户输入m,然后输出a[m]。这说明问题可能涉及斐波那契数列的应用。 接下来,我需要确认POJ1953的题目。由于无法直接访问POJ平台,我需要通过其他途径回忆或查找。根据经验,POJ1953的题目可能是关于计算没有连续两个1的二进制字符串的数量,这类问题通常被称为“没有连续1的二进制串”问题,或者类似的情况。例如,当给定长度n时,求满足条件的排列数目,而斐波那契数列正好适用于这种情况。 例如,当n=1时,可能的序列是0和1,共2种;当n=2时,可能的序列是00、01、10,不能有11,所以是3种。这里用户提供的代码中a[0]=1,a[1]=2,a[2]=3,这似乎与斐波那契数列的递推关系相符。因此,可以推断该问题确实是要求解这样的递推关系。 接下来,解题思路应该是动态规划或者斐波那契数列的应用。对于每个位置i,如果最后一个字符是0,那么前面i-1个字符可以是任何有效排列;如果最后一个字符是1,那么倒数第二个字符必须是0,所以前面i-2个字符的有效排列数。因此,状态转移方程是f(n) = f(n-1) + f(n-2),即斐波那契数列,初始条件为f(1)=2,f(2)=3,这样斐波那契数列的起始项可能与常规不同。 用户提供的Java代码中,数组a的长度是46,这说明n的范围可能到45,因为a[0]对应n=1的情况,或者可能是其他起始情况。例如,当n=1时,输出a[1]=2,对应两种情况;n=2时,输出a[2]=3,这符合之前的例子。因此,问题1953的解法确实是基于斐波那契数列的递推。 现在需要将解题思路整理成中文,并确保符合用户的要求,包括使用正确的LaTeX格式,生成相关问题等。需要检查是否有相关的讨论点,比如边界条件的处理,递推关系的推导,或者可能的优化方法。例如,可以使用动态规划或矩阵快速幂来优化大数情况,但根据题目给出的代码,n的范围到45,所以普通的递推已经足够。 此外,用户可能想知道为什么这个问题和斐波那契数列有关,或者如何推导出递推公式。因此,在解释时需要详细说明状态的定义和转移过程。同时,可能需要注意题目的输入输出格式,比如每个测试用例的输出格式,是否需要换行等,这在代码示例中已经体现。 总结,解题思路分为以下几个步骤: 1. 确定问题类型为递推或动态规划。 2. 定义状态:f(n)表示长度为n的有效排列数目。 3. 推导递推关系:f(n) = f(n-1) + f(n-2)。 4. 确定初始条件:f(1)=2,f(2)=3。 5. 预处理斐波那契数列,直接查表输出结果。 这样用户就可以理解问题是如何被解决的。同时,生成的相关问题可能涉及斐波那契数列的应用、动态规划的优化方法,或者类似题目的扩展。</think>### 北京大学 POJ 1953 解题思路 **题目分析** POJ1953 要求计算长度为 $n$ 的二进制字符串中**没有连续两个1**的不同排列数。例如: - 当 $n=1$ 时,有 $0$ 和 $1$ 两种可能 - 当 $n=2$ 时,有效排列为 $00$, $01$, $10$,共三种可能 通过观察可以发现,该问题满足**斐波那契数列递推关系**,即: $$ f(n) = f(n-1) + f(n-2) \quad (n \geq 3) $$ 初始条件为: $$ f(1) = 2, \quad f(2) = 3 $$ **推导过程** 1. **状态定义**:设 $f(n)$ 表示长度为 $n$ 的满足条件的二进制字符串数量 2. **状态转移**: - 若最后一个字符为 $0$,则前 $n-1$ 个字符可以是任意有效排列 → 贡献 $f(n-1)$ - 若最后一个字符为 $1$,则倒数第二个字符必须为 $0$,前 $n-2$ 个字符可以是任意有效排列 → 贡献 $f(n-2)$ 3. **边界条件**: - $n=1$ 时直接返回 $2$ - $n=2$ 时直接返回 $3$[^1] **代码实现** 用户提供的代码通过预处理斐波那契数列实现高效查询: ```java int[] a = new int[46]; a[0] = 1; // 对应n=1的情况 a[1] = 2; // 对应n=2的情况 for(int i = 2; i <= 45; i++){ a[i] = a[i-1] + a[i-2]; } // 输入m后直接输出a[m-1] ``` 注意:代码中数组下标与问题规模的对应关系需要特别处理。例如,当输入 $m=1$ 时实际对应 $a[0]$。 **关键优化点** - 预处理时间复杂度为 $O(n)$,查询时间复杂度为 $O(1)$ - 使用动态规划避免了递归的重复计算 - 题目限制 $n \leq 45$,普通递推即可满足需求
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值