【高精度递推】【HDU1297】Children’s Queue
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
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?
1
2
3
1
2
4
题目大意就是:
#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的更多相关文章
- HDU1297 Children’s Queue (高精度+递推)
Children’s Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 紫书 习题 10-16 UVa 1647 (高精度+递推)
这道题我已经推出00和1过两步变成00了,可我没有继续做下去-- 后来看了博客发现自己已经做了90%了-- 可惜了,以后不要轻易放弃. 1的个数有个规律,就是每次都乘以2,因为0和1下一步都会变出1 ...
- hdu1297 Children’s Queue
再加上男人:dp[i-1]: 加2一个女人:dp[i-2]+x. 上述的另一种情况下dp[i-2]它不仅包括加2女人对法律状况.和x是一个加号ff原违法的法律案后加入,这最后是mf案例,然后,x=dp ...
- BZOJ 1002 FJOI2007 轮状病毒 递推+高精度
题目大意:轮状病毒基定义如图.求有多少n轮状病毒 这个递推实在是不会--所以我选择了打表找规律 首先执行下面程序 #include<cstdio> #include<cstring& ...
- Children’s Queue(hdu1297+递推)
Children’s Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- (递推 大整数) Children’s Queue hdu1297
Children’s Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1297 Children’s Queue (递推、大数相加)
Children’s Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Children’s Queue HDU 1297 递推+大数
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1297 题目大意: 有n个同学, 站成一排, 要求 女生最少是两个站在一起, 问有多少种排列方式. 题 ...
- codeforces D. Queue 找规律+递推
题目链接: http://codeforces.com/problemset/problem/353/D?mobile=true H. Queue time limit per test 1 seco ...
随机推荐
- Rhythmbox中文乱码解决的方法
转自:http://hi.baidu.com/morgensonne/item/3470aef58747abde6325d2d9 今天在网络上找到了一个比較好的解决Rhythmbox中文乱码的问题的方 ...
- LR使用Java User协议环境报错Please add the <JDK>\bin to the path and try again
看标题报错信息就知道,这是java编译及运行环境配置问题,运行LR脚本时,LR代理找不到java的JDK环境,当然,可能有人会遇到说,我在cmd窗口javac 环境是没问题的呀,是的,这就要看你的jd ...
- 在后台操作标记的runat="server"的html标签
价格: <input id="tbPrice" type="text" runat="server" /> HtmlInputT ...
- XML读写
private string fileName = HttpContext.Current.Server.MapPath("~/Student.xml"); protected v ...
- 数据库对于null值的处理
对于null值的处理,不同的数据库的处理函数是不同的,这里列举了部分数据库对于null的处理函数以及使用: Oracle:是用函数nvl(), ----nvl(chinese,0);如果语文成绩为nu ...
- 关于降低android手机摄像头预览分辨率
假设现在有这样一个需求需要一直开着手机摄像头 但是不做任何拍照动作 但是每个手机的相机分辨率都不同 而默认预览的时候参数是最大分辨率 这样有时候就回导致电量损耗的加快 所以我们可以采取降低相机分辨率的 ...
- Oracle相关的知识点
1. 如何在Oracle SQLPlus中执行SQL Script文件 以下面的格式在提示符中输入@{file name} SQL>@{file} 假设你要运行的文件的名字是script.sql ...
- 关于css的优先级
样式的优先级 外部样式 < 内部样式 < 内联样式 选择器的优先权 解释: 1. 内联样式表的权值最高 1000: 2. ID 选择器的权值为 100; 3. Class 类选择器的 ...
- FileBeat
FileBeat使用说明 FileBeat是一个日志收集器,基于Logstash-Forwarder的源代码.FileBeat一般以代理的身份运行在客户端服务器中,并监视用户指定的目录.文件,同时把日 ...
- JavaScript 全局变量命名空间生成函数
<script type="text/javascript"> var GLOBAL = {}; GLOBAL.namespace = function(str){ v ...