HDU - 1133 Buy the Ticket

21 篇文章 0 订阅
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 元;一开始售票处没有钱,问有多少种排队方式可以让全部人顺利买票;

思路:(转)

1. 当m<n时,肯定不能满足条件,因为50元的张数小于100元的张数,所以不能为所以的100元找回50元。

2. 当m>=n时,通过合理的安排,就能满足条件:

2.1 首先所有的排列数为 C[m+n][m],即从m+n个位置中挑选m个,作为50元的位置。

2.2 然后排除掉非法排列:假设第k个100元不能找钱, 则前面有k-1个50元,所以后面有n-k个100元, m-k+1个50元,如果把第k个之后的人50换成100,100换成50,那么总的就变成了有:m+1个100元, n-1个50元,那么C[m+n][m+1]就是非法的排列数。

2.3 所以合法的排列数为:C[m+n][m] - C[m+n][m+1],由于人是有区别的, 所以还需要乘上两种人的全排列,所以 ans = (C[m+n][m] - C[m+n][m+1])*A[m]*A[n]。

ans可以进一步化简,得到(m+n)!*(m-n+1)/(m+1);

注意特判m<n的情况就行了;

import java.math.BigInteger;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sin=new Scanner(System.in);
		BigInteger [] jc=new BigInteger [205];
		jc[1]=BigInteger.ONE;
		for(int i=2;i<201;i++){
			jc[i]=jc[i-1].multiply(BigInteger.valueOf(i));
		}
		int t=1;
		while(sin.hasNext()){
			int m=sin.nextInt();
			int n=sin.nextInt();
			if(m==0&&n==0)
				break;
			System.out.println("Test #"+t+":");
			t++;
			if(m<n){
				System.out.println(0);
				continue;
			}
			BigInteger ans=jc[n+m].multiply(BigInteger.valueOf(m-n+1)).divide(BigInteger.valueOf(m+1));
			System.out.println(ans);
		}
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值