Children’s Queue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13333    Accepted Submission(s):
4364

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
 
Recommend
lcy   |   We have carefully selected several similar
problems for you:  2044 2045 2050 2046 1290 

题意:

F代表女孩,M代表男孩,女孩不能单独出现但是可以不出现,即不能出现MFM这种情况,给定一个数n,问有多少种站队方式。高精度递推。

题解:

a[i-1]:合法+男
a[i-2}:合法+女女
a[i-4}:合法+男女(即不合法)+女女

这是三种没有互相交叉的不同情况!

解释:

设:a(n)表示n个人的合法队列,则:

按照最后一个人的性别分析,他要么是男,要么是女,
所以可以分两大类讨论:

1、如果n个人的合法队列的最后一个人是男,
则前面n-1个人组成的队列只要是合法的队列即可,
最后一个男生只 需要站在最后即可,所以,这种情况一共有a(n-1);

2、如果n个人的合法队列的最后一个人是女,
则要求队列的第n-1个人务必也是女生,这就是说,
限定了最后两个人必须都是女生才能是合法的,这又可以分两种情况:

2.1、如果队列的前n-2个人是合法的队列,
则显然后面再加两个女生,也一定是合法的,这种情况有a(n-2);
2.2、但是,即使前面n-2个人不是合法的队列,加上两个女生也有可能是合法的,
当然,这种长度为n-2的不合法 队列,不合法的地方必须是尾巴,
就是说,这里说的长度是n-2的不合法串的形式必须是a(n-4)+男+女,
这种情况一共有a(n-4).

注意到本题的难点,就是第三种情况,可能前n - 2位以女孩为末尾的不合法队列(即单纯以1位女孩结尾),

也可以追加2位女孩成为合法队列,而这种n - 2不合法队列必然是由n - 4合法队列+1男孩+1女孩的结构,
即情况数为a[n - 4]。

得出递推公式如下:
a[i]=a[i-1]+a[i-2]+a[i-4];

若感觉本题较难,可先查看文章:[ACM_HDU_2045]LELE的RPG难题,思路与本题类似,但较为简单。

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
long long a[][]={};
int main()
{
int i,j,n;
a[][]=;
a[][]=;
a[][]=;
a[][]=;
a[][]=;
for(i=;i<=;i++)
{
for(j=;j<=;j++)
{
a[i][j]+=a[i-][j]+a[i-][j]+a[i-][j];
a[i][j+]+=a[i][j]/;
a[i][j]%=;
}
}
while(cin>>n)
{
for(j=;j>=;j--)
if(a[n][j]!=)
break;
cout<<a[n][j];
for(j=j-;j>=;j--)
printf("%08d",a[n][j]);
cout<<endl;
}
return ;
}

HDU1297 Children’s Queue (高精度+递推)的更多相关文章

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

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

  2. 【高精度递推】【HDU1297】Children’s Queue

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

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

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

  4. hdu1297 Children’s Queue

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

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

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

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

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

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

    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. GMM算法k-means算法的比较

    1.EM算法 GMM算法是EM算法族的一个具体例子. EM算法解决的问题是:要对数据进行聚类,假定数据服从杂合的几个概率分布,分布的具体参数未知,涉及到的随机变量有两组,其中一组可观测另一组不可观测. ...

  2. C#二进制文件的读写

    sing System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using ...

  3. android 常见死机问题--log分析

    http://blog.csdn.net/fangchongbory/article/details/7645815         android 常见死机问题--log分析============ ...

  4. laravel框架中的session问题

    这两天一直在鼓捣服务器,配置环境,在搭建laravel的过程之中,发现了laravel中的session的一些问题,这里总结一下: (1):我在服务器上搭建了多个sever,为了测试学习,分别使用不同 ...

  5. POJ3070Fibonacci(矩阵快速幂+高效)

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11587   Accepted: 8229 Descri ...

  6. 锋利的jQuery-5--网页换肤

    网页换肤原理:通过调用不同的样式表文件来实现不同的皮肤,并且将切换好的皮肤计入cookie. 例子:通过点击上边的颜色设置下边显示的背景色. html代码: <!-- head部分引入的css样 ...

  7. WPF 操作键盘

    #region 打开键盘的键 const uint KEYEVENTF_EXTENDEDKEY = 0x1; const uint KEYEVENTF_KEYUP = 0x2; [DllImport( ...

  8. wordPress Development

    site:http://codex.wordpress.org/Theme_Development 2014-03-24 This article is about developing WordPr ...

  9. SpringMvc 相关,bean map转换,百度天气,base64.js,jsBase64.java;

    1. Map<String, Object>与JavaBean[POJO, Model]转换; //model public class model{ private int id; pr ...

  10. Altera的几个常用的Synthesis attributes

    各厂商综合工具,对HDL综合时都定义了一些综合属性这些属性可指定a declaration,a module item,a statement, or a port connection 不同的综合方 ...