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. 我在eclipse输出的第一个hello world!

    下学期就要学习JAVA 语言,我现在对它好像还真的是一无所知.记得两次在帮学长做测评的时候,他们都说要装上eclipse.然后从放假我就忙着下载,安装,但是由于官网都是英文,似乎一直在出差错.询问了学 ...

  2. JS----事件3

    一 事件对象(event):与特定事件相关且包含有关该事件详细信息的对象通过事件可以触发event对象的元素,鼠标的位置及状态,按下的键等等event对象只在事件发生的过程中才有效非IE浏览器里的ev ...

  3. Javascript概念

    什么是JavaScript? 1 以前的目的:验证表单输入的正确性. 2 现在的目的:多做一些页面的交互效果. 3 javascript是一个跨平台的脚本语言. 4 网景公司开发的,由布兰登·艾奇最先 ...

  4. 2017面向对象程序设计(Java) 第4周学习指导及要求(2017.9.14-2017.9.18)

    学习目标 深入理解程序设计中算法与程序的关系: 深入理解java程序设计中类与对象的关系: 理解OO程序设计的第一个特征:封装: 需要掌握基本使用方法的预定义类有:Math类.String类.Arra ...

  5. Xshell 连接ubuntu虚拟机失败解决办法

    当我们在VMWare上安装好Ubuntu虚拟机后,有时候会需要使用多个terminal窗口,但是在虚拟机中多个窗口互相切换查看不方便,这时候用物理机的xshell工具连接到虚拟机中去就显得很方便.直接 ...

  6. polyfill

    [polyfill] 在JavaScript的世界里,有两个词经常被提到,shim和polyfill.它们指的都是什么,又有什么区别? 一个shim是一个库,它将一个新的API引入到一个旧的环境中,而 ...

  7. PHP 在 Mac 的安装之路

    半年前本以为有一些 Apache 和 PHP 的安装经验,今天在 Mac 上还是踩了很多坑. 坦诚地讲,这东西入门成本比 Node,Python 入门成本真的是大很多. Apache 的编译安装就是那 ...

  8. TOJ3448: 小学生的作业

    Python字符串的插入操作 传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3448 ...

  9. Max Points on a Line (HASH TABLE

    QUESTIONGiven n points on a 2D plane, find the maximum number of points that lie on the same straigh ...

  10. c# 文件过大时清空原有内容重新写入

    FileStream fs = new FileStream("E:\\Test\\HistoryData.txt", FileMode.Append, FileAccess.Wr ...