hdu 1133 Buy the Ticket (dp)

Buy the Ticket

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6439    Accepted Submission(s): 2693


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
 

Author
HUANG, Ninghai
 

Recommend
Eddy   |   We have carefully selected several similar problems for you:   1267  1297  1134  2067  1023 
 

Statistic |  Submit |  Discuss | Note

题目大意: n个用50元买票的,m个用100元买票的。每张电影票50元,一开始没有零钱,保证有人用100元买票是能找钱,求满足条件的方案数。(注意每个人都只能买一张)

题解:dp

分析一下,设50元的个数为x,100元的个数为y ,其实就是保证任意时刻x>=y。

f[i][j]表示到第i个人,已经有j个人用50元买了门票。

f[i][j]=f[i-1][j]+f[i-1][j-1] f[i-1][j]表示当前这个人用100元,f[i-1][j-1]表示当前这个人用50元。  设k=i-j 即100元的个数,递推的时候保证k<=j。

因为每个人是不一样的所以最后的答案是 f[n+m][n]*n!*m!

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define N 103
using namespace std;
struct data{
	int num[403];
}f[N*2][N],ans;
int n,m,a[N],b[N],c[N];
int calc(int a[N],int x)
{
	a[0]=1; a[1]=1;
	for (int i=1;i<=x;i++)
	 {
	 	for (int j=1;j<=a[0];j++)
	 	 a[j]*=i;
	 	for (int j=1;j<=a[0];j++) {
	 		a[j+1]+=a[j]/10000;
	 		a[j]%=10000;
		 }
		int t=a[0];
		while (a[t+1]) {
			t++; 
			a[t+1]+=a[t]/10000;
			a[t]%=10000;
		}
		a[0]=t;
	 }
}
void add(data &x,data y,data z)
{
	int t=max(y.num[0],z.num[0]);
	for (int i=1;i<=t;i++)
	 x.num[i]=y.num[i]+z.num[i];
	for (int i=1;i<=t;i++)
	 x.num[i+1]+=x.num[i]/10000,x.num[i]%=10000;
	while (x.num[t+1]){
		t++;
		x.num[t+1]+=x.num[t]/10000;
		x.num[t]%=10000;
	}
	x.num[0]=t;
}
void mul(data &x,int a[N])
{
	memset(c,0,sizeof(c));
	for (int i=1;i<=x.num[0];i++)
	 for (int j=1;j<=a[0];j++)
	  c[i+j-1]+=x.num[i]*a[j];
	int t=max(x.num[0],a[0]);
    for (int i=1;i<=t;i++)
     c[i+1]+=c[i]/10000,c[i]%=10000;
    while(c[t+1]) {
    	t++;
    	c[t+1]+=c[t]/10000;
    	c[t]%=10000;
	}
	c[0]=t;
	for (int i=0;i<=c[0];i++) x.num[i]=c[i];
}
int main()
{
   freopen("a.in","r",stdin);
   freopen("my.out","w",stdout);
   int n=100; int m=100;
   f[0][0].num[0]=1;
   f[0][0].num[1]=1;
   for (int i=1;i<=n+m;i++)
   	  for (int j=min(i,n);j>=1;j--)
   	   {
   	   	 int k=(i-j);
   	   	 if (k>j) break;
   	   	 add(f[i][j],f[i-1][j],f[i-1][j-1]);
	   }
   int cnt=0;
   while (true){
   	 scanf("%d%d",&n,&m); cnt++;
   	 if (n==0&&m==0) break;
   	 memset(a,0,sizeof(a));
   	 memset(b,0,sizeof(b));
	 calc(a,n); calc(b,m);
	 for (int i=0;i<=400;i++) ans.num[i]=0;
	 for (int i=0;i<=f[n+m][n].num[0];i++)
	  ans.num[i]=f[n+m][n].num[i];
	 mul(ans,a); mul(ans,b);
	 printf("Test #%d:\n",cnt);
	 if (ans.num[0]==0||n<m) {
	 	printf("0\n");
	 	continue;
	 }
	 for (int i=ans.num[0];i>=1;i--){
	 	int t=ans.num[i];
	 	if (i!=ans.num[0]){
		 	if (t<10) printf("000");
		 	else if (t<100) printf("00");
		 	else if (t<1000) printf("0");
	    }
	 	printf("%d",t);
	 }
	 printf("\n");
   }	
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值