1133

Buy the Ticket

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

Problem Description
The "Harry Potter and the Goblet of Fire" will be on show in the next few days. As a crazy fan of Harry Potter, you will go to the cinema and have the first sight, won’t you?

Suppose the cinema only has one ticket-office and the price for per-ticket is 50 dollars. The queue for buying the tickets is consisted of m + n persons (m persons each only has the 50-dollar bill and n persons each only has the 100-dollar bill).

Now the problem for you is to calculate the number of different ways of the queue that the buying process won't be stopped from the first person till the last person. 
Note: initially the ticket-office has no money.

The buying process will be stopped on the occasion that the ticket-office has no 50-dollar bill but the first person of the queue only has the 100-dollar bill.

 
Input
The input file contains several test cases. Each test case is made up of two integer numbers: m and n. It is terminated by m = n = 0. Otherwise, m, n <=100.
 
Output
For each test case, first print the test number (counting from 1) in one line, then output the number of different ways in another line.
 
Sample Input
3 0
3 1
3 3
0 0
 
Sample Output
Test #1:
6
Test #2:
18
Test #3:
180
 
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1133 



 题意:
m+n个人排队买票,并且满足m≥n,票价为50元,其中m个人各手持一张50元钞票,n个人各手持一张100元钞票,除此之外大家身上没有任何其他的钱币,并且初始时候售票窗口没有钱,问有多少种排队的情况数能够让大家都买到票。
 

这个题目是Catalan数的变形,不考虑人与人的差异,如果m=n的话那么就是我们初始的Catalan数问题,也就是将手持50元的人看成是+1,手持100元的人看成是-1,任前k个数值的和都非负的序列数。

这个题目区别就在于m>n的情况,此时我们仍然可以用原先的证明方法考虑,假设我们要的情况数是Dm+n,无法让每个人都买到的情况数是Um+n,那么就有Dm+n+Um+n= C(m+n, n),此时我们求Um+n,我们假设最早买不到票的人编号是k,他手持的是100元并且售票处没有钱,那么将前k个人的钱从50元变成100元,从100元变成50元,这时候就有n-1个人手持50元,m+1个手持100元的,所以就得到,于是我们的结果就因此得到了,表达式是C(m+n, m+1)

然后每个人都是不一样的,所以需要全排列 m! * n! 所以最后的公式就是(C(m+n, n)-C(m+n, m+1))*m!*n! 化简后为 (m+n)!*(m-n+1)/(m+1);

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std ;
const int MAXN=;
int f[MAXN],a[MAXN];
int main()
{
int i,j,t,k,n,m;
int x;
int c,s;
int sign;
k=;
while(cin>>m>>n&&!(m==&&n==))
{
printf("Test #%d:\n",++k);
memset(f,,sizeof(f));
memset(a,,sizeof(a));
f[]=;
for(i=; i<=m+n; i++)
{
c=;
for(j=; j<MAXN; j++)
{
s=f[j]*i+c;
f[j]=s%;
c=s/;
}
}
c=;
if(m>=n)
{
x=m-n+;
for(i=; i<MAXN; i++)
{
s=f[i]*x+c;
f[i]=s%;
c=s/;
}
t=;
for(j=MAXN-; j>=; j--) if(f[j]) break;
for(i=j; i>=; i--) a[t++]=f[i];
x=m+;
c=;
sign=;
for(i=; i<t; i++)
{
s=((c*)+a[i])/x;
if(s!=) sign=;
if(sign!=) cout<<s;
c=(c*+a[i])%x;
}
}
else cout<<"";
cout<<endl;
}
return ;
}

 

杭电1133 排队买票 catalan的更多相关文章

  1. JDOJ 1928: 排队买票

    JDOJ 1928: 排队买票 JDOJ传送门 Description 一场演唱会即将举行.现有n个歌迷排队买票,一个人买一张,而售票处规定,一个人每次最多只能买两张票.假设第i位歌迷买一张票需要时间 ...

  2. [HZNUOJ1524]排队买票(DP)

    题目链接:http://acm.hznu.edu.cn/JudgeOnline/problem.php?id=1524 简单分析后可以知道每一个手持两元的小朋友前面,售票员手里至少有一个一元. 假设d ...

  3. 安科 OJ 1054 排队买票 (递归,排列组合)

    时间限制:1 s 空间限制:128 M 题目描述 有M个小孩到公园玩,门票是1元.其中N个小孩带的钱为1元,K个小孩带的钱为2元.售票员没有零钱,问这些小孩共有多少种排队方法,使得售票员总能找得开零钱 ...

  4. 杭电1023Train Problem II

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=1023 题目: Train Problem II Time Limit: 2000/1000 MS (Jav ...

  5. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  6. 杭电ACM题单

    杭电acm题目分类版本1 1002 简单的大数 1003 DP经典问题,最大连续子段和 1004 简单题 1005 找规律(循环点) 1006 感觉有点BT的题,我到现在还没过 1007 经典问题,最 ...

  7. 杭电acm习题分类

    专注于C语言编程 C Programming Practice Problems (Programming Challenges) 杭电ACM题目分类 基础题:1000.1001.1004.1005. ...

  8. 『ACM C++』HDU杭电OJ | 1415 - Jugs (灌水定理引申)

    今天总算开学了,当了班长就是麻烦,明明自己没买书却要带着一波人去领书,那能怎么办呢,只能说我善人心肠哈哈哈,不过我脑子里突然浮起一个念头,大二还要不要继续当这个班委呢,既然已经体验过就可以适当放下了吧 ...

  9. acm入门 杭电1001题 有关溢出的考虑

    最近在尝试做acm试题,刚刚是1001题就把我困住了,这是题目: Problem Description In this problem, your task is to calculate SUM( ...

随机推荐

  1. WebAPI 和 webservice接口

    1. webservice走HTTP协议和80端口.WebService则类似于bs架构,只需要开发服务器端,不需要开发客户端,客户端只要遵循soap协议,就可以调用. 2. api,用的协议和端口, ...

  2. canvas刮奖

    (function (global, factory) { typeof module === 'object' && typeof module.exports === 'objec ...

  3. 七个不可错过的React组件库与开发框架

    React是如今最火爆的前端技术,而React最棒的一个特点就是有大量功能丰富的组件库和开发框架可用.从按钮到卷轴到工具条,应有尽有,而且这些组件可以各行其是,也可以组装成复杂的UI,你也可以把UI分 ...

  4. linux下面设置密码失效参考

    chage -E 2005-12-31 user1 设置用户口令的失效期限

  5. eclipce连接数据库增删改查

    1.在mysql中新建一个名为course的数据库,并在其中新建一个course数据表,包含四个字段,id,name,teacher,classname如图(注意:将id设为自动递增,否则后面新增会出 ...

  6. SSL、TLS中间人攻击

    生成私钥 生成一个ca.key私钥签名 然后用私钥生成一个伪造证书服务器的根证书 生成一个伪造证书服务器的根证书ca.crt 在攻击主机启动路由转发功能 或者echo 1 > /proc/sys ...

  7. PHP中的变量与PHP中算false的情况

    一PHP中的变量 1.PHP中的变量,声明与使用必须用$开头 2.PHP是一种弱类型语言,变量其实并不需要声明.可以直接给变量赋任何类型的值: 3.PHP中可以使用连等同时声明多个变量, eg:num ...

  8. unity3d英语单词拼写小游戏Pics Quiz Maker With Categories 3.0

    下载地址: https://item.taobao.com/item.htm?spm=0.7095261.0.0.19f71debcef4hT&id=575991216080

  9. 关于后台执行JS代码总结

    方法一.FineUI的 pagecontext对象 string js="functionName();"; PageContext.RegisterStartUpScript(j ...

  10. 被遗忘的having

    清明节后公司网站搞活动主要功能很简单就是实现一个消费送的功能.比如, 当天消费金额满5000 返回10%,5000 及以下 返 7% 的功能.本身这个功能不是很难,但是  这个功能跟上次的一个 新用户 ...