现在的位置: 首页 > 综合 > 正文

HDU1133 Buy the Ticket

2014年08月29日 ⁄ 综合 ⁄ 共 2373字 ⁄ 字号 评论关闭
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>
using namespace std;
#include <queue>

const int N=400;
char store [203][N];

//整数n转化为字符串s
void itos(int n,char *s){
	int i,j,t[5];
	if(n==0){
		s[0]='0';
		s[1]=0;
		return;
	}
	i=0;
	while(n){
		t[i++]=n%10;
		n/=10;
	}
	i--;
	j=0;
	while(i>=0)
		s[j++]=t[i--]+'0';
	s[j]=0;
}
//计算m*c=res.默认m和c不为0
void mul(char *m,char *c,char *res){
	int i,j,len1,len2;
	len1=strlen(m),len2=strlen(c);
	int *r=new int[len1+len2+1];
	for(i=0;i<=len1+len2;i++)
		r[i]=0;
	for(i=0;i<len1;i++)
		for(j=0;j<len2;j++)
			r[i+j+1]+=(m[i]-'0')*(c[j]-'0');
	for(i=len1+len2-1;i>=1;i--)//处理进位
		if(r[i]>9){
			int tmp=r[i]/10;
			r[i]%=10;
			r[i-1]+=tmp;
		}
	for(i=0;i<len1+len2&&!r[i];i++);//处理前导0
	if(i==len1+len2){
		res[0]='0';
		res[1]=0;
		return;
	}
	j=0;
	while(i<len1+len2)res[j++]=r[i++]+'0';
	res[j]=0;
	delete[] r;
}
//计算阶乘
void factorial(int n,char *res){
	char s[N],t[N];
	if(n==0){
		res[0]='1';
		res[1]=0;
	}
	itos(n,res);
	while(--n>1){
		itos(n,s);
		mul(res,s,t);
		strcpy(res,t);
	}
}
//小数d去除大数m
void div(char *m,int d,char *res){
	int i,len;
	int r,tmp;
	len=strlen(m);
	r=0;//余数
	for(i=0;i<len;i++){
		r=r*10+m[i]-'0';
		res[i]=r/d+'0';//商
		r%=d;
	}
	res[len]=0;
	//忽略前导0
	i=0;
	while(res[i]=='0')
		i++;
	if(i!=0)
		strcpy(res,res+i);
}
int main()
{
	freopen("C:\\in.txt","r",stdin);
	int m,n,i;
	int cnt=0;
	char r[N],s[N],t[N];
	for(i=1;i<=200;i++){
		factorial(i,store[i]);
	}
	while(scanf("%d %d",&m,&n)!=EOF){
		if(m==0&&n==0)break;
		if(m<n){
			printf("Test #%d:\n%d\n",++cnt,0);
			continue;
		}
		i=0;
		while((s[i]=store[m+n][i])!=0)i++;
		itos(m+1-n,t);
		mul(s,t,r);
		div(r,m+1,t);
		printf("Test #%d:\n%s\n",++cnt,t);
	}
	return 0;
} 

抱歉!评论已关闭.