ZOJ 2061: Buy the Ticket

题目来源:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1061

Buy the Ticket


Time Limit: 2 Seconds      Memory Limit: 65536 KB


The "Harry Potter andthe Goblet of Fire" will be on show in the next few days. As a crazy fanof Harry Potter, you will go to the cinema and have the first sight, wont you?

Suppose the cinema only has one ticket-officeand the price for per-ticket is 50 dollars. The queue for buying the tickets isconsisted of m + n persons (m persons each only has the 50-dollar bill and npersons each only has the 100-dollar bill).

Now the problem for you is to calculate thenumber of different ways of the queue that the buying process won't be stoppedfrom the first person till the last person.

Note: initially the ticket-office has nomoney. 

The buying process will be stopped on the occasion that the ticket-office hasno 50-dollar bill but the first person of the queue only has the 100-dollarbill.


Input

The input file contains several test cases. Each test case is made up of twointeger 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!

-每个人是不同的

总的序列数 = C(m)(m+n)

-m+n个人中挑出m个人持50

不合法序列数 = C(m+1)(m+n)

-将持50元的人简记为”0”, 100元的人简记为”1”

-任何不合法的序列,存在一个从下标0开始的子列,子列中1的个数多于0的个数

-例如,01100011是不合法序列,因为子列[0,3):011中有21,10

-将该序列除去不合法子列[0,3)的剩余子列翻转(即10,01)得01111100,此序列中1m+1个,0n-1

-定理:任意一个m个0,n个1的不合法序列(m>=n)与一个m+1个1,n-1个0的序列一一对应

-证明:

(1) 对于一个m个0,n个1的不合法序列,设其最小不合法子列为[0,a),其中有x+1个1, x个0(1和0的个数只相差1由“最小性”保证)。则剩余子列中(n-x-1)个1, (m-x)个0. 将剩余子列翻转,翻转剩余子列有(n-x-1)个0, (m-x)个1. 如此得到的序列1的个数=(x+1) + (m-x)=m+1, 0的个数=(x) + (n-x-1)=n-1.

(2) 对于一个m+1个1,n-1个0的序列,必然存在一个从0开始的子列,其中1的个数比0的个数多1。将这个子列作为“不合法子列”(x+1个1, x个0),将剩余子列( (m-x)个1, (n-x-1)个0)翻转得到翻转子列( (m-x)个0, (n-x-1)个1),与“不合法子列”拼接可以得到一个m个0, n个1的序列,由于该序列存在前述“不合法子列”,该序列是不合法的

-由此我们证明了任意一个m个0,n个1的不合法序列(m>=n)与一个m+1个1,n-1个0的序列一一对应,因此计算不合法序列个数也就是计算m+1个1,n-1个0的序列个数=C(m+1)(m+n)

故合法序列个数 = (C(m)(m+n) – C(m+1)(m+n))*m!*n!, m>=n

                         = 0, m<n

m>=n的情况可化简为(m-n+1) *(m+n)! /(m+1)

 

下面是实现上的问题:

1. 由于有多组测试数据且阶乘的计算颇费时间,故首先计算100以内的各个阶乘保存在一个101维的数组里,测试数据用到的时候从数组中调用即可

2. 整数除法要最后做

3. 千万别忘了特殊情况m<n的时候的特殊处理

-----------------------------------------------------

代码

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

public class Main {

	public static int NMAX = 202;
	public static BigInteger[] factor = new BigInteger[NMAX];
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		getfactor();
		int cnt = 0,mm,nn;
		BigInteger m,n,ans;
		Scanner sc = new Scanner(System.in);
		while (true)
		{
			cnt++;
			mm = sc.nextInt();
			nn = sc.nextInt();
			if (mm==0&&nn==0)
			{
				break;
			}
			if (mm<nn)
			{
				System.out.println("Test #"+cnt+":");
				System.out.println(0);
				continue;
			}
			m = BigInteger.valueOf(mm);
			n = BigInteger.valueOf(nn);
			ans = m.subtract(n);
			ans = ans.add(BigInteger.ONE);
			ans = ans.multiply(factor[mm+nn]);
			ans = ans.divide(m.add(BigInteger.ONE));
			System.out.println("Test #"+cnt+":");
			System.out.println(ans);
		}
	}
	
	public static void getfactor()						// 把所有的阶乘先计算好放在数组里
	{
		factor[0] = new BigInteger("1");
		int i = 1;
		for (i=1; i<NMAX; i++)
		{
			factor[i] = factor[i-1].multiply(BigInteger.valueOf(i));
		}
	}

}


猜你喜欢

转载自blog.csdn.net/da_kao_la/article/details/80662762