HDU 1133 Buy the Ticket(卡特兰数/递推DP+大数)

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个人拥有50元,n个人拥有100元,电影票价50元,安排所有的人都能买到票,且电影院有零钱找钱(电影院最开始没有钱),求能够排列出的种类数。

思路:放在卡特兰数练习,那应该就是卡特兰数了,但是,,,不怎么会呀,公式什么的,,,但这个题递推DP也是能做的,但是很麻烦,而且涉及到了阶乘,感觉我会被逼疯。果断参考卡特兰数的应用:https://blog.csdn.net/zhangmh93425/article/details/44677891

该题适用公式m!*n!*((m+n)Cn-(m+n)C(n-1)),化简后成为(m+n)!*(m-n+1)/(m+1);

代码如下:

#include<stdio.h>
#include<string.h>
int main()
{
    int t=1,max,n,m,c,i,j,a[90001];
    while(scanf("%d%d",&m,&n)==2&&(n!=0||m!=0))
    {
        memset(a,0,sizeof(a));
        a[0]=1;max=1;
        for(i=1;i<=n+m;i++)
        {
            c=0;
            if(m+1!=i)//在阶乘的过程中除去m+1。 
            {
                for(j=0;j<max;j++)
                {
                    a[j]=a[j]*i+c;
                    c=a[j]/10;
                    a[j]%=10;
                    if(c!=0&&max<=j+1)
                    max++;
                }
            }
        }//(m+n)阶乘 
        if(n!=0)
        {
            for(j=0;j<max;j++)
            {
                a[j]=a[j]*(m-n+1)+c;
                c=a[j]/10;
                a[j]%=10;
                if(c&&max<=j+1)
                max++;
            }
        } //(m+n)阶乘 乘以(m-n+1); 
        printf("Test #%d:\n",t);
        if(n>m)
        printf("0");
        else
        {
            if(a[max]!=0)
            printf("%d",a[max]);
            for(j=max-1;j>=0;j--)
            printf("%d",a[j]);
        }
        printf("\n");
        t++;
    }
    return 0;
}//公式((m+n)!*(m-n+1))/(m+1) 

放上大佬的DP代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100+10;
const int mod = 100000000;
long long dp[maxn][maxn][101];
void init(void){
	int i,j,z;
	memset(dp,0,sizeof(dp));
	dp[0][1][0] = 0;
	dp[1][0][0] = 1;
	for(i=2 ;i<maxn ;i++){
		dp[i][0][0] = 1;
	}
	for(i=1 ;i<maxn ;i++){
		for(j=1 ;j<maxn ;j++){
			for(z=0 ;z<101 ;z++){
				if(i>=j){
					dp[i][j][z] += dp[i-1][j][z] + dp[i][j-1][z];
					if(dp[i][j][z] >= mod){
						dp[i][j][z+1] += dp[i][j][z] / mod;
						dp[i][j][z] %= mod; 
					}
				}
			}
		}
	}
	
}
int main(){
	int n,m,i,j,t,_ = 1;
	init();
	while(scanf("%d%d",&n,&m) != EOF && (m||n)){
		printf("Test #%d:\n",_++);
		if(n<m){
			printf("0\n");
			continue;
		}
		t = max(n,m);
		for(i=2 ;i<=t ;i++){
			for(j=0 ;j<101 ;j++){
				if(i<=n)
					dp[n][m][j] *= i;
				if(i<=m)
					dp[n][m][j] *= i;	
			}
			for(j=0 ;j<101 ;j++){
				if(dp[n][m][j] >= mod){
					dp[n][m][j+1] += dp[n][m][j] / mod;
					dp[n][m][j] %= mod;
				}
			}
		}
		i = 100;
		while(dp[n][m][i] == 0)
			i--;
		printf("%lld",dp[n][m][i]);
		for(i=i-1 ; i>=0;i--)
			printf("%08lld",dp[n][m][i]);
		printf("\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/pleasantly1/article/details/81085565