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

题意:

就是m+n个人去买票,票价50元,m个人只带了50元一张的纸币,n个人只带了100元一张的纸币,售票员一开始手里没有钱。

问,如何让售票员在可以全部都找零的情况下,安排这些人买票的顺序~

也就是说:到100元的人买票之前,售票员手里必须要有一张50元的。

推导过程如下:

m个人拿50,n个人拿1001、

如果n > m,那么排序方法数为0,这一点很容易想清楚

2、现在我们假设拿50的人用‘0’表示,拿100的人用‘1’表示。 如果有这么一个序列0101101001001111。

当第K个位置出现1的个数多于0的个数时就是一个不合法的序列了 假设m=4,n=3的一个序列是: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)

如果没看懂:可以参考我的前面一篇卡特兰数的分析:

http://blog.csdn.net/qq_26525215/article/details/51453493

import java.math.BigInteger;
import java.util.Scanner; /**
* @author 陈浩翔
*
* 2016-5-19
*/
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t=0;
while(sc.hasNext()){
int m =sc.nextInt();
int n =sc.nextInt();
if(n==0&&m==0){
return;
}
System.out.println("Test #"+(++t)+":"); if(n>m){
System.out.println(0);
continue;
} BigInteger a = new BigInteger("1"); for(int i=2;i<=m+n;i++){
a=a.multiply(new BigInteger(""+i));
}
a=a.multiply(new BigInteger(""+(m-n+1)));
a=a.divide(new BigInteger(""+(m+1)));
System.out.println(a);
}
}
}

HDOJ/HDU 1133 Buy the Ticket(数论~卡特兰数~大数~)的更多相关文章

  1. HDU 1133 Buy the Ticket (数学、大数阶乘)

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

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

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

  3. 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 ...

  4. hdu 1133 Buy the Ticket(Catalan)

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

  5. hdu 1133 Buy the Ticket (大数+递推)

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

  6. HDU——1133 Buy the Ticket

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

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

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

  8. HDU 1134 Game of Connections(卡特兰数+大数模板)

    题目代号:HDU 1134 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1134 Game of Connections Time Limit: 20 ...

  9. hdu 1133 Buy the Ticket

    首先,记50的为0,100的为1. 当m=4,n=3时,其中的非法序列有0110010; 从不合法的1后面开始,0->1,1->0,得到序列式0111101 也就是说,非法序列变为了n-1 ...

随机推荐

  1. 在Android上模拟登录广工正方教务系统查询成绩

    这是在博客园里开博以来写的第一篇博客. 因为之前看过很多人都有发过关于模拟登录正方软件获取数据的文章,自己觉得挺好玩的便也去动手一做,开始还以为挺难的,但实际做起来还蛮简单的,当然其中还有些小插曲. ...

  2. linq 中的分组查询

    直接看代码: //一个字段分组 var data1 = from a in query group a by a.Name into b select new { Total = b.Sum(c=&g ...

  3. 基于shiro授权过程

    1.对subject进行授权,调用方法isPermitted("permission串")2.SecurityManager执行授权,通过ModularRealmAuthorize ...

  4. 基于url拦截实现权限控制

    用户表,角色表,用户角色表,权限表,权限角色表 1.用户通过认证(可以是验证用户名,密码等) 2.登陆拦截器,为公开的url放行, 登陆时,将用户信息放入session中,获得用户的权限集合,将集合放 ...

  5. 【html】【0】开始的序言

    人生总得做点什么才显得有意义,在牛逼的梦想也抵挡不住你傻逼似的坚持! 1>本系列适用于没有任何计算机语言基础的小白入门级教程 2>为了我喜欢的一个女生小娜娜 3>为自己系统的学习ht ...

  6. 【BZOJ1875】【矩阵乘法】[SDOI2009]HH去散步

    Description HH有个一成不变的习惯,喜欢饭后百步走.所谓百步走,就是散步,就是在一定的时间 内,走过一定的距离. 但是同时HH又是个喜欢变化的人,所以他不会立刻沿着刚刚走来的路走回. 又因 ...

  7. ul ol dl

    1.ul是无序列表,也就是说没有排列限制可以随意加li: <ul> <li>可以随意放置</li> <li>可以随意放置</li> < ...

  8. 如何禁用easyui-linkbutton 中的Click事件

    eg: <a id="btn" href="#" class="easyui-linkbutton" data-options=&qu ...

  9. 有效解决js中添加border后动画bug问题

    做了个demo发现如果一个div不加border属性,用对象的offsetWidth属性来控制width没问题,但是如果一旦加了border属性,问题就来了. 其实offsetWidth属性获取的的是 ...

  10. pgsql与mysql 下 varchar类型的数字文本的排序 区别

    两者都有cast函数,但使用方法完全不同. 1.在mysql中,cast( value as type) 将value的数据类型转换成type类型,其type可以为 二进制,同带binary前缀的效果 ...