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

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);
        }
    }
}
时间: 2025-01-01 19:56:43

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

hdoj 1133 Buy the Ticket 【卡特兰】

题意:有m个人(拿50元)和n个人(拿100元)去买票,售票厅刚开始没有,问最后所有人都能够买到的方式的种类数. 这道题也是经典的卡特兰数类型题. 我们可以将他们看做是火车进出站,但是由于人是不同的,所以最后还要乘上m!*n! 最后的数学表达是就是(C(m+n,n)-C(m+n, m+1))*m!*n!=> 结果为 (m!*n!)*(m+1-n)/(m+1) 注:m<n时 直接输出0 代码: import java.util.Scanner; import java.math.*; publi

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

[分析] 当m<n,显然一定不合法. 所以我们考虑m>=n,类比n=m时的方案数推法,总方案为C(m,m+n),要减去不符合的情况. 我们扫描一个数,找到第一个不符合的位置,假设是有a个1,a+1个1,那么我们后面会填n-a-1个-1,m-a个1,我们把后面的1和-1交换,就得到了一个有m-a个-1,n-a-1个1的数,方案为C(m+1,m+n),把它减掉即可, 即ans=C(m,m+n)-C(m+1,m+n).因为每个人不相同最后1还要乘n!*m!. 为什么要用1和-1互换呢, 首先对于一个

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.util.*; public class Main { /** * @param args */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int cas = 1; while(tru

HDU——1133 Buy the Ticket

Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7152    Accepted Submission(s): 2998 Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the nex

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

Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1886 Accepted Submission(s): 832   Problem Description The \\\\\\\"Harry Potter and the Goblet of Fire\\\\\\\" will be on show i

HDU 1133 Buy the Ticket

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

hdu 1134 Game of Connections 【卡特兰数+大数】

Game of Connections Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3327    Accepted Submission(s): 1896 Problem Description This is a small but ancient game. You are supposed to write down the

2014年百度之星程序设计大赛 - 初赛(第一轮) hdu Grids (卡特兰数 大数除法取余 扩展gcd)

题目链接 分析:打表以后就能发现时卡特兰数, 但是有除法取余. f[i] = f[i-1]*(4*i - 2)/(i+1); 看了一下网上的题解,照着题解写了下面的代码,不过还是不明白,为什么用扩展gcd, 不是用逆元吗.. 网上还有别人的解释,没看懂,贴一下: (a / b) % m = ( a % (m*b)) / b 笔者注:鉴于ACM题目特别喜欢M=1000000007,为质数: 当gcd(b,m) = 1, 有性质: (a/b)%m = (a*b^-1)%m, 其中b^-1是b模m的逆

【HDU 5370】 Tree Maker(卡特兰数+dp)

Tree Maker Problem Description Tree Lover loves trees crazily. One day he invents an interesting game which is named Tree Maker. In this game, all trees are binary trees. Initially, there is a tree with only one vertex and a cursor on it. Tree Lover