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
1
2
3
 

Sample Output
1
2
4
 大数处理加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;

}


hdu 1297的更多相关文章

  1. Children’s Queue HDU 1297 递推+大数

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1297 题目大意: 有n个同学, 站成一排, 要求 女生最少是两个站在一起, 问有多少种排列方式. 题 ...

  2. HDOJ/HDU 1297 Children’s Queue(推导~大数)

    Problem Description There are many students in PHT School. One day, the headmaster whose name is Pig ...

  3. HDU 1297 Children’s Queue (递推、大数相加)

    Children’s Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. HDU——PKU题目分类

    HDU 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 ...

  5. hdu 1465:不容易系列之一(递推入门题)

    不容易系列之一 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  6. [转] HDU 题目分类

    转载来自:http://www.cppblog.com/acronix/archive/2010/09/24/127536.aspx 分类一: 基础题:1000.1001.1004.1005.1008 ...

  7. HDU ACM 题目分类

    模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 104 ...

  8. HDU 5643 King's Game 打表

    King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...

  9. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

随机推荐

  1. 【原创】Linux基础之logrotate

    logrotate logrotate ‐ rotates, compresses, and mails system logs logrotate is designed to ease admin ...

  2. sccrapy 爬虫框架网数据库储存时去重的问题

    from scrapy.exceptions import DropItem #导入异常处理模块 class Baidu03Pipeline(object): def __init__(self): ...

  3. JS遍历对象和数组总结

    在日常工作过程中,我们对于javaScript遍历对象.数组的操作是十分的频繁的,今天把经常用到的方法总结一下! 一.遍历对象 1.使用Object.keys()遍历 返回一个数组,包括对象自身的(不 ...

  4. ASE19 团队项目 模型组 scrum report集合

    scrum report 链接 scrum1 report scrum2 report scrum3 report scrum4 report scrum5 report scrum6 report ...

  5. 安装grafna已经grafna对接zabbix

    安装插件 grafana-cli plugins install alexanderzobnin-zabbix-app 重启grafna systemctl restart grafana-serve ...

  6. UE中正则表达式

    UltraEdit(后简称UE),是我经常使用的文本编辑软件,其功能的强大,令我由衷地爱上了它.每天不用就全身不爽.从最开始的9.0到现在的 12.10a(本人只用到这个版本),UE都是系统重装后必安 ...

  7. Invalid property value

    又见这个错误!头几天同事遇到这个问题,我查到去年写的并按此解决了,原文在这里,查了半天,才查出是ftShortInt造成的这个错误. 当我们在设计期将ClientQuery.Active设置为True ...

  8. JavaJDBC【一、概述】

    其实这个内容在学习java基础的时候就有看过了,只是没有详细整理,在这再整理一下 数据库操作对于任何一门后端语言来说都是很重要的 JDBC:Java Data Base Connectivity 内容 ...

  9. ProgressDialog 进度条的初步认识

    public class MainActivity extends Activity implements View.OnClickListener{ private ProgressBar prog ...

  10. NLP/CL 顶会收录

    全文转载自知乎@刘知远老师:初学者如何查阅自然语言处理学术资料(2016修订版). 1. 国际学术组织.学术会议与学术论文 自然语言处理(natural language processing,NLP ...