Children’s Queue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 11117    Accepted Submission(s): 3577

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
 
Author
SmallBeer (CML)
 
Source
 

题目大意就是:

长度为N的序列 F要么不出现 要么不单独出现 即MFM FMM FMF 的情况

一开始的时候想的是排列组合的方法
N个中有k个M,然后再将N-k个F拆开 插入 k+1个空格内的方案数。
不用想 这样的复杂度十分的高 先不谈时间复杂度
空间复杂度也得爆 要开C[1000][1000][70];

所以考虑递推
设A[i] 表示长度为i时的方案数
    F[i] 表示末尾为F 且除去末尾的一串F后满足题目条件的方案数(即F,MF,MMFF也满足条件。除去末尾的一串F后为空,M,MM)
    M[i]表示末尾为M满足条件的方案数
   
    递推方程
    A[i]=A[i-1]+F[i-1] 
    F[i]=M[i-1]+F[i-1]
    M[i]=A[i-1];

 要写高精度

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
#define B 10000
using namespace std;
struct Bigint
{
int operator[](int index) const {
return digit[index];
} int& operator[](int index) {
return digit[index];
}
int len;
int digit[100];
};
Bigint A[1001],F[1001],M[1001];
void highadd(Bigint &c,Bigint &a,Bigint &b)
{
c.len=max(a.len,b.len)+1;
int temp=0;
for(int i=0;i<c.len;i++)
{
temp+=a[i]+b[i];
c[i]=temp%B;
temp=temp/B;
}
if(c[c.len-1]==0) c.len--;
}
void YCL()
{
A[1].len=1,A[1][0]=1;F[1].len=1;F[1][0]=1;M[1].len=1,M[1][0]=1;
for(int i=2;i<=1000;i++)
{
highadd(A[i],A[i-1],F[i-1]);
highadd(F[i],M[i-1],F[i-1]);
M[i]=A[i-1];
}
}
void output(int n)
{
printf("%d",A[n][A[n].len-1]);
for(int i=A[n].len-2;i>=0;i--)
printf("%04d",A[n][i]);
printf("\n");
}
int main()
{
int n;
YCL();
while(cin>>n)
{
output(n);
}
}



另外一种递推思路:

思路如下:

一个长度n的队列可以看成一个n - 1的队列再追加的1个小孩,这个小孩只可能是:

a.男孩,任何n - 1的合法队列追加1个男孩必然是合法的,情况数为f[n - 1];

b.女孩,在前n - 1的以女孩为末尾的队列后追加1位女孩也是合法的,我们可以转化为n - 2的队列中追加2位女孩;

一种情况是在n - 2的合法队列中追加2位女孩,情况数为f[n - 2];

但我们注意到本题的难点,可能前n - 2位以女孩为末尾的不合法队列(即单纯以1位女孩结尾),也可以追加2位女孩成为合法队列,而这种n - 2不合法队列必然是由n - 4合法队列+1男孩+1女孩的结构,即情况数为f[n - 4]。

【高精度递推】【HDU1297】Children’s Queue的更多相关文章

  1. HDU1297 Children’s Queue (高精度+递推)

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

  2. 紫书 习题 10-16 UVa 1647 (高精度+递推)

    这道题我已经推出00和1过两步变成00了,可我没有继续做下去-- 后来看了博客发现自己已经做了90%了-- 可惜了,以后不要轻易放弃. 1的个数有个规律,就是每次都乘以2,因为0和1下一步都会变出1 ...

  3. hdu1297 Children’s Queue

    再加上男人:dp[i-1]: 加2一个女人:dp[i-2]+x. 上述的另一种情况下dp[i-2]它不仅包括加2女人对法律状况.和x是一个加号ff原违法的法律案后加入,这最后是mf案例,然后,x=dp ...

  4. BZOJ 1002 FJOI2007 轮状病毒 递推+高精度

    题目大意:轮状病毒基定义如图.求有多少n轮状病毒 这个递推实在是不会--所以我选择了打表找规律 首先执行下面程序 #include<cstdio> #include<cstring& ...

  5. Children’s Queue(hdu1297+递推)

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

  6. (递推 大整数) Children’s Queue hdu1297

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

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

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

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

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

  9. codeforces D. Queue 找规律+递推

    题目链接: http://codeforces.com/problemset/problem/353/D?mobile=true H. Queue time limit per test 1 seco ...

随机推荐

  1. SpringMVC+easyUI CRUD 添加数据C

    接一篇文章,今天上午实现了添加数据.以下是Jsp.里面主要是看newUser()和saveUser().注意这函数里的url,newUser()里面去掉url属性.还要注意的一个问题 <div ...

  2. [Protractor] Use protractor to catch errors in the console

    For any reason, there is an error in your code, maybe something like undefined error. Protractor sti ...

  3. HttpServletRequest对象请求转发和HttpServletResponse对象请求重定向之间的区别

    HttpServletRequest对象request代表浏览器请求对象,HttpServletResponse对象代表服务器响应对象,当用浏览器访问web服务器,发出请求时,Servlet调用ser ...

  4. flexible.js字体大小诡异现象解析及解决方案

    最近在做一个手机端页面时,遇到了一个奇怪的问题:字体的显示大小,与在CSS中指定的大小不一致.大家可以查看这个Demo(记得打开Chrome DevTools). 就如上图所示,你可以发现,原本指定的 ...

  5. EffectiveC#8--确保0对于值类型数据是有效的(初始化问题)

    1.决不要创建一个不包括0在内的枚举类型 2.举例如下: public enum Planet { Mercury = 1, Venus = 2, Earth = 3, Mars = 4, Jupit ...

  6. 学习AJAX(二)

  7. Entity Framework中实现查询的几种方法

    在介绍几种方法前,献上一张图,希望图的作者不要追究我的盗图之过.本文的内容是我自学时的笔记,自学的内容来自网络.手打的代码,切不可直接复制过去用,会有好多错别字什么的. Entity SQL 类似于S ...

  8. java加载资源文件

    className.class.getResourceAsStream 用法: 第一: 要加载的文件和.class文件在同一目录下,例如:com.x.y 下有类Test.class ,同时有资源文件c ...

  9. C++服务器设计(四):超时管理机制设计

    前四章介绍了系统层的设计,从这一章开始进入服务层的设计. 连接断开 在常见的服务器场景中,客户端断开连接的方式为被动关闭.即作为客户端请求完服务器的服务后,选择主动关闭同服务器的连接.在服务器的角度看 ...

  10. Linux下VMWare虚拟机的使用技巧

    使用技巧: 1.虚拟机安装文件:vm-workstation-full-8.0.3-703057.x86_64.bundle,./vm-workstation-full-8.0.3-703057.x8 ...