Buy the Ticket

Submit Status

 当n=m时,是卡特兰数,当n<m时显然为0,当n>m时先将所有人视为一样,只关注其手中money,用dp[i][j]表示i个50,j个100的排队方式数,对最后一个人进行分类最后一个人为50时,则前面有i-1个50,j个100,其排列方式数为dp[i-1][j],最后一个人为100时同理前面人排列方式为dp[i][j-1];所以有递推公式dp[i][j]=dp[i-1][j]+dp[i][j-1];然后再来考虑人是有区别的因为有相同money的人可以相互交换位置故对每个i和j再乘以i!和j!便是其最终结果。

  0 1 2 3 4
0 1 0 0 0 0
1 1 1 0 0 0
2 1 2 2 0 0
3 1 3 5 5 0
4 1 4 9 14 14

 

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

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; 
struct bign{
	int num[500];//要开的足够大一百和试过不行
	int len;
	bign()
	{
		for(int i=0;i<100;i++)
		{
			num[i]=0;
		}
		len=0;
	 } 
};
bign x[105][105];

bign mut(bign a,int b)
{
	bign c;
	int flag=0,s,i=0; 
	for( i=0;i<a.len;i++)
	{
		s=a.num[i]*b+flag;
		c.num[i]=s%10;
		flag=s/10;
	}
		while(flag!=0){
		c.num[i++]=flag%10;
		flag=flag/10;
	}
	c.len=i;
	return c;
}
bign chu(bign a,int b)
{
	bign c;
	c.len=a.len;
	int flag=0,i=0;
	for(i=a.len-1;i>=0;i--)
	{
		flag=flag*10+a.num[i];
		if(flag<b)
		{
			c.num[i]=0;
		}
		else
		{
			c.num[i]=flag/b;
			flag=flag%b;
		}
	}
	while(c.num[c.len-1]==0&&c.len-1>=1)
	{
		c.len--;
	}
	return c;
}
bign add(bign x,bign y){
	bign c;
	int flag=0,sum;
	int i;
	for(i=0;i<x.len||i<y.len;i++){
		sum=0;
		sum=x.num[i]+y.num[i]+flag;
		c.num[i]=sum%10;
		flag=sum/10;
	}
	c.len=max(x.len,y.len);
	if(flag!=0){
	c.num[i]=flag;
	c.len++;
	}
	return c;
}
//以上大数运算模板
int main()
{
	int n,m;
	for(int k=0;k<=100;k++)
	{
		x[k][0].num[0]=1;
		x[k][0].len=1;
	}
	
	for(int i=1;i<=100;i++)//dp递推
	{
		for(int j=1;j<=i;j++)
		if(i==j)
		{
		int m=(4*i-2);
		int mm=i+1;
		x[i][j]=mut(x[i-1][j-1],m);
		x[i][j]=chu(x[i][j],mm);
		}
		else
		{
			x[i][j]=add(x[i-1][j],x[i][j-1]);
		}
	}
	int i=0; 
	while(1)
	{
		scanf("%d%d",&n,&m);
		if(n==0&&m==0)
		break;
		i++;
		printf("Test #%d:\n",i);
		if(m>n)
		{
			printf("0\n");
		}
		else
		{
			bign cm=x[n][m];
			for(int i=n;i>0;i--)
			cm=mut(cm,i);
			for(int i=m;i>0;i--)
			cm=mut(cm,i);
			for(int j=cm.len-1;j>=0;j--)
			{
			printf("%d",cm.num[j]);
			}
		printf("\n");
		}	
	}
 } 

猜你喜欢

转载自blog.csdn.net/s_h_w_s_n_g/article/details/81102565