HDU-1133 Buy the Ticket (卡特兰数+大数)

题目回顾(HDU1133

Buy the Ticket

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
 
源码分析:
  我的第一反应是使用全排列,找出所有的排列情况,然后挨个判断。但这种方法是错误的,原因有两个,一是超时,二是没有考虑大数情况。
以下是全排列做法(只适用于数据较小的情况)
#include<iostream>
#include<algorithm>
using namespace std;
int m,n;
int x[200];
int num;

bool judge(int t){
	int a=0;
	for(int i=0;i<t;i++){
		a+=x[i];
		if(a<0){
			return false;
		}
	}
	return true;
}

void resove(int t){
	if(!judge(t)){
		return;
	}
	if(t==m+n){
		num++;
	}
	for(int i=t;i<n+m;i++){
		swap(x[t],x[i]);
		resove(t+1);
		swap(x[t],x[i]);
	}
}

int main(){
	int s=0;
	while(cin>>m>>n&&(n||m)){
		for(int i=0;i<m;i++){
			x[i]=1;
		}
		for(int i=m;i<m+n;i++){
			x[i]=-1;
		}
		num=0;
		resove(0);
		s++;
		cout<<"Test #"<<s<<":"<<endl<<num<<endl; 
	}
	return 0;
} 

  

后来参考了网上的做法,使用卡特兰数+大数将问题转化为计算(m+n)! * (m-n+1) / (m+1),具体思路可见原博客

#include<iostream>
#include<string.h>
using namespace std;
const int MAX=500;
int x[MAX];
int main(){
	int m,n;
	int c=1;
	while(cin>>m>>n&&(m||n)){
		cout<<"Test #"<<c++<<":"<<endl; 
		
		if(m<n){
			cout<<0<<endl;
			continue;
		}
		
//		求出(m+n)! 		
		memset(x,0,sizeof(x));
		x[0]=1;
		int c,sum=0;
		for(int i=2;i<=m+n;i++){
			for(int j=0;j<MAX;j++){
				c=x[j]*i+sum;
				x[j]=c%10;
				sum=c/10;
			}
		}
		
//		求出(m+n)!*(m+1-n) 
		int q=m+1-n;
		for(int j=0;j<MAX;j++){
			c=x[j]*q+sum;
			x[j]=c%10;
			sum=c/10;
		}
		
//		求出位数 
		int bit;
		for(int j=MAX-1;j>=0;j--){
			if(x[j]!=0){
				bit=j;
				break;
			}
		}
		
//		(m+n)!*(m+1-n)/(m+1)
		sum=0;
		int t=0;
		int temp;
		for(int j=bit;j>=0;j--){
			temp=sum*10+x[j];
			if(t==0){
				if(temp/(m+1)>0){
					t=1;
					cout<<temp/(m+1);
				}
			}else{
				cout<<temp/(m+1);
			}
			sum=temp%(m+1);
		}
		cout<<endl;
	}
	return 0;
}   

  

转载于:https://www.cnblogs.com/orangecyh/p/9843121.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值