Buy the Ticket

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

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
 
Author
HUANG, Ninghai
 
Recommend
Eddy
将卡特兰扩展中的排队买票及相关问题都OVER了!!
解题思路(转):( C(m+n, n) - C(m+n, m+1) ) * m! * n! 化简即 (m+n)! * (m-n+1) / (m+1)

推導過程如下:

m個人拿50,n個人拿100
1、如果n > m,那麼排序方法數為0,這一點很容易想清楚
2、現在我們假設拿50的人用‘0’表示,拿100的人用‘1’表示。
如果有這麼一個序列0101101001001111。
當第K個位置出現1的個數多餘0的個數時就是一個不合法的序列了
假設m=4,n=3的一個序列是:0110100 。顯然,它不合法,現在我們把它稍微變化一下:
把第二個1(這個1前面的都是合法的)後面的所有位0變成1,1變成0.
就得到0111011這個序列1的數量多餘0的數量,顯然不合法,但現在的關鍵不是看這個序列是不是合法的
關鍵是:他和我們的不合法序列0110100成一一對應的關係。
也就是說任意一個不合法序列(m個0,n個1),都可以由另外一個序列(n-1個0和m+1個1)得到。
另外我們知道,一个序列要麼是合法的,要麼是不合法的
所以,合法序列數量 = 序列總數量 - 不合法序列的總量
序列總數可以這樣計算 m+n個位置中,選擇n個位置出來填上1,所以是C(m+n,n).
不合法序列的數量就是: m+n個位置中,選擇m+1個位置出來填上1,所以是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)
 
推廣:
如果原來有p張50元的話,那麼不合法的序列的數量應該是:任意一個不合法序列(m個0,n個1),都可以由另外一個序列(n-1個0和m+1+p個1)得到,所以是m+n個位置中,選擇m+1+p個位置,出來填上1所以是C(m+n,m+1+p),接下來簡化就不推了
代码:
 #include<iostream>
#include<cstring>
#define maxn 500
using namespace std;
int arr[maxn+];
int main()
{
int m,n,time=,i,c,j,s,k; //m stand for person has 50 yuan, n stand for 100 yuan
while(cin>>m>>n,m+n)
{
memset(arr,,sizeof arr);
cout<<"Test #"<<time++<<":"<<endl;
if(m<n)
{
cout<<<<endl;
continue;
}
arr[]=;
for(i=,c=;i<=m+n;i++) //(m+n)!
{
for(j=;j<=maxn;j++)
{
s=arr[j]*i+c;
arr[j]=s%;
c=(s-arr[j])/;
}
}
for(c=j=;j<=maxn;j++) //(m+n)!*(m+1-n)
{
s=arr[j]*(m+-n)+c;
arr[j]=s%;
c=(s-arr[j])/;
}
for(k=maxn;arr[k]==;k--);
for(c=,j=k;j>=;j--) //(m+n)!*(m+1-n)/(m+1)
{
s=(arr[j]+*c);
c=s%(m+);
arr[j]=(s-c)/(m+);
}
for(k=maxn;arr[k]==;k--);
for(j=k;j>=;j--)
cout<<arr[j];
cout<<endl;
}
return ;
}

HDUOJ---1133(卡特兰数扩展)Buy the Ticket的更多相关文章

  1. Buy the Ticket HDU 1133 卡特兰数应用+Java大数

    Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...

  2. CodeForces - 1204E Natasha, Sasha and the Prefix Sums (组合数学,卡特兰数扩展)

    题意:求n个1,m个-1组成的所有序列中,最大前缀之和. 首先引出这样一个问题:使用n个左括号和m个右括号,组成的合法的括号匹配(每个右括号都有对应的左括号和它匹配)的数目是多少? 1.当n=m时,显 ...

  3. HDOJ 5184 Brackets 卡特兰数扩展

    既求从点(0,0)仅仅能向上或者向右而且不穿越y=x到达点(a,b)有多少总走法... 有公式: C(a+b,min(a,b))-C(a+b,min(a,b)-1)  /// 折纸法证明卡特兰数: h ...

  4. hdu 1133(卡特兰数变形)

      题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1133   题意:排队买50块一张的票,初始票台没有零钱可找,有m个人持有50元,n人持有100元, ...

  5. 【HDU 1133】 Buy the Ticket (卡特兰数)

    Buy the Ticket Problem Description The "Harry Potter and the Goblet of Fire" will be on sh ...

  6. HDOJ/HDU 1133 Buy the Ticket(数论~卡特兰数~大数~)

    Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...

  7. HDU 1133 Buy the Ticket 卡特兰数

    设50元的人为+1 100元的人为-1 满足前随意k个人的和大于等于0 卡特兰数 C(n+m, m)-C(n+m, m+1)*n!*m! import java.math.*; import java ...

  8. 【高精度练习+卡特兰数】【Uva1133】Buy the Ticket

    Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  9. Buy the Ticket(卡特兰数+递推高精度)

    Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...

随机推荐

  1. 解决hue报错:timed out (code THRIFTSOCKET): None

    报错栈: [/Jun/ :: +] decorators ERROR error running <function execute at 0x7fba2804ecf8> Tracebac ...

  2. [实时更新]jquery全部版本号下载

    jquery-2.1.0   注!不再支持IE 6/7/8 直接引用地址:  开发版地址1: <script src="http://code.jquery.com/jquery-2. ...

  3. HTML常用标记

    HTML文档由4个主要标记组成,这4个标记是<html>.<head>.<title>和<body>.举例如下: <html> <he ...

  4. iOS:多个单元格的删除(方法一)

    采用存取indexPath的方式,来对多个选中的单元格进行删除 删除前: 删除后: 分析:如何实现删除多个单元格呢?这需要用到UITableView的代理方法,即选中单元格时对单元格做的处理,同时我们 ...

  5. trapping-rain-water-ii

    https://leetcode.com/problems/trapping-rain-water-ii/ // https://discuss.leetcode.com/topic/60418/ja ...

  6. 百度编辑器ueditor通过ajax方式提交,不需要事先转义字符的方法(异常:从客户端(xxx)中检测到有潜在危险的 Request.Form 值)

    最近项目中使用百度编辑神器ueditor,确实是很好用的一款编辑器.官网教程提供的与后端数据交互都是跟表单方式有关的,项目中使用的是ajax方式提交,因此出现了不少问题,现在记录备忘下. 环境:.ne ...

  7. 本地主机DNS劫持演示及防范

    劫持演示                                                                                      如果要进行DNS劫持 ...

  8. tcp协议的端口状态

    Listening Syn_sent syn_rcvd established close_wait time_wait --------- 对方主动关闭(对方调用close()), 我方的状态变为 ...

  9. idea 设置代码的颜色

  10. Linux学习笔记--which命令(搜索命令的命令)

    which.哪一个的意思.作用是从PATH环境变量指定的路径中,搜索命令所在位置及命令别名. which命令特点: 1) "which" 命令仅仅能查找系统命令.不能搜索普通文件. ...