前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ-1953 World Cup Noise(线性动规)

POJ-1953 World Cup Noise(线性动规)

作者头像
ShenduCC
发布2018-04-25 17:34:21
6980
发布2018-04-25 17:34:21
举报
文章被收录于专栏:算法修养算法修养

World Cup Noise Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16374 Accepted: 8097 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

注意结果int存不下。简单的动规,递推即可

代码语言:javascript
复制
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <math.h>

using namespace std;
int n;
long long int dp[50][2];
int main()
{
    int a;
    while(scanf("%d",&a)!=EOF)
    {
        int cas=0;
        while(a--)
        {
            memset(dp,0,sizeof(dp));
            dp[1][0]=1;
            dp[1][1]=1;
            scanf("%d",&n);
            for(int i=2;i<=n;i++)
            {
                dp[i][1]+=dp[i-1][0];
                dp[i][0]+=(dp[i-1][0]+dp[i-1][1]);
            }
            printf("Scenario #%d:\n",++cas);
            printf("%lld\n\n",dp[n][1]+dp[n][0]);
        }

    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-01-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档