Buy the Ticket

HDU1133

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元的钞票找零?(将持50元者到达视作将50元入栈,持100元者到达视作使栈中某5元出栈
首先:如果m<n,直接gg了.因为这种情况不可能会全部通过,总会单独剩下100元的people
其次,这样考虑:合法序列数量 = 序列总数量 - 不合法序列的总量

先考虑 序列总数量,就是C(n+m,m),

再考虑不合法序列的情况,假设有m+1个人持50元,这种肯定是不满足情况的.就是C(n+m,m+1).

然后再组合起来。

公式就是:C(n+m,m)-C(n+m,m+1)*n!*m!(可以化简:(m-n+1)/(n+1) *(m+n)!,当时脑抽没化简,直接Java大数怼上去了)

code:

import java.io.*;
import java.util.*;
import java.math.*;
public class Main
{
	static Scanner cin = new Scanner(System.in);
	final static int M=300;
	static BigInteger num[][]= new BigInteger[M][M];
    public static void main(String args[])
    {
        int n,m,i,t=1;
        Main ma=new Main();
    	ma.slove();
        while(cin.hasNextInt())
        {
            n = cin.nextInt();
            m = cin.nextInt();  
            if(n==0&&m==0) break;
            System.out.println("Test #"+(t++)+":");
            if(n<m){System.out.println("0");continue;}
            BigInteger a=BigInteger.ONE,b=BigInteger.ONE,ans=BigInteger.ONE,ant;
            for(i=n;i>=2;--i)
            	a=a.multiply(BigInteger.valueOf(i));
            for(i=m;i>=2;--i)
            	b=b.multiply(BigInteger.valueOf(i));
            ans=ans.multiply(a);
            ans=ans.multiply(b);
            ant=num[n+m][n].subtract(num[n+m][n+1]);
            ans=ans.multiply(ant);  
            System.out.println(ans);
        }
    }
	public  void slove() {
		// TODO Auto-generated method stub
		int i,j;
		for(i=0;i<M;++i)
			for(j=0;j<M;++j)
				num[i][j]=BigInteger.ZERO;
		for(i=0;i<M;++i)
		{
			num[0][i]=BigInteger.ZERO;
			num[i][0]=BigInteger.ONE;
		}
		for(i=1;i<M;++i)
			for(j=1;j<M;++j)
			num[i][j]=num[i-1][j-1].add(num[i-1][j]);
//		for(i=1;i<M;++i)
//			for(j=1;j<=i;++j)
//			System.out.println("n="+i+"m="+j+"  "+num[i][j]);	
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值