博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 1297
阅读量:4114 次
发布时间:2019-05-25

本文共 1912 字,大约阅读时间需要 6 分钟。

Children’s Queue

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 959 Accepted Submission(s): 534
Problem Description
There are many students in PHT School. One day, the headmaster whose name is PigHeader wanted all students stand in a line. He prescribed that girl can not be in single. In other words, either no girl in the queue or more than one girl stands side by side. The case n=4 (n is the number of children) is like
FFFF, FFFM, MFFF, FFMM, MFFM, MMFF, MMMM
Here F stands for a girl and M stands for a boy. The total number of queue satisfied the headmaster’s needs is 7. Can you make a program to find the total number of queue with n children?
 
Input
There are multiple cases in this problem and ended by the EOF. In each case, there is only one integer n means the number of children (1<=n<=1000)
 
Output
For each test case, there is only one integer means the number of queue satisfied the headmaster’s needs.
 
Sample Input
123
 
Sample Output
124
 大数处理加dP,,dp的话f[n]=f[n-1]+f[n-2]+f[n-4]
假设当前要加入第n个学生,如果他是男生,则加入对前面的序列都不会构成影响,
即为f[n-1],假如他是女生,为保证合法,她前面的那个也就是第n-1个也必须是
女生,接下来分两种情况讨论,如果第n-2个及以前的都合法,那么显然加入最后
这两个女生也是合法的额,但是如果不合法呢?很明显,如果不合法,问题应该出在
尾部,也就是原来f[n-2]不合法,但是加入最后两个女生后也就变得合法了(这句话
很关键),那么
其只能为f[n-4]+男+女,所以即可得到公式
其实这个讨论的关键在于确定最后两个为女生才能保证合法
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
long long dp[1001][1001];
int main()
{
   int n;
   memset(dp,0,sizeof(dp));
   dp[1][0]=1;dp[1][1]=1;
   dp[2][0]=1;dp[2][1]=2;
   dp[3][0]=1;dp[3][1]=4;
   dp[4][0]=1;dp[4][1]=7;
   for(int i=5;i<=1000;i++)
     {
       int j,t=dp[i-1][0];
       for(j=1;j<=t;j++)
        {
           dp[i][j]+=dp[i-1][j]+dp[i-2][j]+dp[i-4][j];
           if(dp[i][j]>=10)
           {
               dp[i][j+1]+=dp[i][j]/10;
               dp[i][j]%=10;
           }
        }
       while(dp[i][j]>=10)
       {
           dp[i][j+1]+=dp[i][j]/10;
           dp[i][j]%=10;
           j++;
       }
       j=1000;
       while(!dp[i][j])
           j--;
       dp[i][0]=j;
     }
   while(cin>>n)
   {
      for(int i=dp[n][0];i>=1;i--)
         cout<<dp[n][i];
      cout<<endl;
   }
   return 0;
}

转载地址:http://dcgsi.baihongyu.com/

你可能感兴趣的文章
Angular 4.x 学习导引
查看>>
Angularjs with Asp.net/core
查看>>
国庆长假学习收获
查看>>
uni-app开发日志[2019090601]:开发上拉加载时的一些情况整理
查看>>
TinyMCE 富文本编辑器 ━━ 自定义插件 [转载]
查看>>
TinyMCE 富文本编辑器 ━━ 自定义插件之弹窗基础设置(整理)
查看>>
TinyMCE 富文本编辑器 ━━ 自定义插件相关的一些网站(整理)
查看>>
TinyMCE 富文本编辑器 ━━ 一键排版功能所需正则表达式整理及学习
查看>>
TinyMCE 富文本编辑器 ━━ (Version: 5.0.4)内含icon对照表(转载)
查看>>
TinyMCE 富文本编辑器 ━━ 自定义插件之弹窗控件布局
查看>>
PHP开发日志 ━━ PhpSpreadsheet使用
查看>>
jQuery资料整理 ━━ ajaxfileupload.js报错:jQuery.handleError is not a function
查看>>
服务器配置篇 ━━ windows iis快速关闭ssl3.0 ssl2.0 rc4 等
查看>>
PHP开发日志 ━━ 与上传相关的资料整理~突破2M限制
查看>>
PHP开发日志 ━━ zip压缩
查看>>
服务器配置篇 ━━ 中文域名(.公益)解析、党政机关挂标及如何正确运行在服务器
查看>>
学习日志 ━━ 关键字和保留字
查看>>
Golang学习日志 ━━ 下载及安装
查看>>
Golang学习日志 ━━ 一图一代码看懂range、byte、rune、uint8、int32
查看>>
Golang学习日志 ━━ 简单写文件操作的四种方法
查看>>