• World Cup Noise(DP基础)


    World Cup Noise
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 13853   Accepted: 6827

    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
     
    二维数组a[i][j]表示尾数为i(i = 0 or i = 1),长度为j的不同序列的个数。显然有
    a[0][1] = a[1][1] = 0;
    a[0][j] = a[0][j-1] + a[1][j-1];
    a[1][j] = a[0][j-1];
     于是得到
    AC Code:
     1 #include <iostream>
     2 #include <cstdio>
     3 
     4 using namespace std;
     5 
     6 int main()
     7 {
     8      int t, ca = 1, l, a[2][46];
     9      a[0][1] = a[1][1] = 1;
    10     for(int i = 2; i < 46; i++)
    11     {
    12         a[0][i] = a[0][i-1] + a[1][i-1];
    13         a[1][i] = a[0][i-1];
    14     } 
    15     scanf("%d", &t);
    16     while(t--)
    17     {
    18         scanf("%d", &l);
    19         printf("Scenario #%d:\n%d\n\n", ca++, a[0][l] + a[1][l]);
    20     }
    21     return 0;
    22 }

     以上代码0MS跑完,但我们可以进一步化简,令C(j)为长度为j的互异序列数,根据上面分析有

    C(j)=a[0][j]+a[1][j] = (a[0][j-1] + a[1][j-1])+a[0][j-1] = (a[0][j-1] + a[1][j-1]) + (a[0][j-2] + a[1][j-2])

    =C(j-1)+C(j-2).

    其实这就是斐波拉契数列的递推公式。

  • 相关阅读:
    git常用命令大全
    谷粒商城遇到的问题
    谷粒商城Seata(四十一)
    通过VMware Horizon Client访问虚拟机
    Qt 让窗口(或控件)居中
    QT 设置QDockWidget的初始大小
    Qt QDockWidget小结
    Qt QDockWidget停靠窗相关的信号
    Qt 基于Qt的词典开发系列--无边框窗口的缩放与拖动
    Qt 创建停靠悬浮窗口 QDockWidget
  • 原文地址:https://www.cnblogs.com/cszlg/p/2911292.html
Copyright © 2020-2023  润新知