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. wikioi 1163 訪问艺术馆 树形dp

    递归建树,由题知该树是一棵二叉树,且除根节点外其它点的度为0或2. dp[i][j]表示来到第i个走廊(还未走过这条走廊)还剩下j时间,能拿到最大的画的数量. dp[i][j]=max(dp[i][j ...

  2. 近看到的机器学习、NLP相关书单

    书单再多,不去读,也是白搭~~ 水木上的machine learning书单:http://www.newsmth.net/bbscon.php?bid=5&id=34859&ftyp ...

  3. Spark GraphX图处理编程实例

    所构建的图如下: Scala程序代码如下: import org.apache.spark._ import org.apache.spark.graphx._ // To make some of ...

  4. java中需要用equals来判断两个字符串值是否相等

    在C++中,两个字符串比较的代码可以为: (string1==string2) 但在java中,这个代码即使在两个字符串完全相同的情况下也会返回false Java中必须使用string1.equal ...

  5. go语言基础之go猜数字游戏

    1. 产生一个随机的4位数 示例1: package main import "fmt" import "math/rand" import "tim ...

  6. Android 跳转到系统应用管理

    Intent i = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS");String pkg = " ...

  7. input框设置onInput事件只能输入数字,能兼容火狐IE9

    使用onInput()事件 onInput()是 HTML5 的标准事件,对于检测 textarea, input:text, input:password 和 input:search 这几个元素通 ...

  8. 数据库操作语句类型(DQL、DML、DDL、DCL)简介

    SQL语言共分为四大类:数据查询语言DQL,数据操纵语言DML,数据定义语言DDL,数据控制语言DCL. 1. 数据查询语言DQL数据查询语言DQL基本结构是由SELECT子句,FROM子句,WHER ...

  9. MongoDB学习笔记(六)--复制集+sharding分片 && 总结

    复制集+sharding分片                                                               背景 主机 IP 服务及端口 Server A ...

  10. URAL 1807

    题目大意:给出一个正整数n(n为合数),求n的一个划分(a1,a2,...,ak,...)(k>=2).使得其在存在最大的最大公约数之下,存在最大的最小公倍数. KB     64bit IO ...