HDU1133-Buy the Ticket

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的同种商品,商店没有小于100的零钱,问所有人都能买到这种商品的排队方法有多少?

解析:
听说这道题有两种解法!那我们就来研究一下:
首先,如果n>m,那么肯定是不行的!
主要还是n<=m,我也是看了别个大佬(fangzhiyang和Ivanzn)的代码:
1.动态规划:dp[i][j]表示i个人拿50,j个人拿100;
dp[i][j]=dp[i-1][j]+dp[i][j-1];

2.公式:sum=(n+m)!*(m-n+1)/(m+1);

代码:(第二种)

#include<iostream>
#include<cstdio>
#include<cstring>
#define N 105

using namespace std;

int dp[N<<1][N<<2];
int book[N<<2];

int main()
{
	int n,m,t=0;
	dp[0][0]=dp[0][1]=1;
	dp[1][0]=dp[1][1]=1;
	dp[2][0]=1;
	dp[2][1]=2;
	for(int i=3;i<=200;i++)
	{
		int len=dp[i-1][0],v=0;
		for(int j=1;j<=len;j++)
		{
			dp[i][j]=dp[i-1][j]*i+v;
			v=dp[i][j]/10;
			dp[i][j]%=10;
		}
		while(v)
		{
			dp[i][++len]=v%10;
			v/=10;
		}
		dp[i][0]=len;
	}
	while(~scanf("%d%d",&m,&n)&&m)
	{
		t++;
		if(n>m)
			printf("Test #%d:\n0\n",t);
		else
		{
			int v=0,len=dp[n+m][0];
			for(int i=1;i<=len;i++)
			{
				book[i]=dp[n+m][i]*(m-n+1)+v;
				v=book[i]/10;
				book[i]%=10;
			}
			while(v)
			{
				book[++len]=v%10;
				v/=10;
			}
			for(int i=len;i>0;i--)
			{
				v=v*10+book[i];
				book[i]=v/(m+1);
				v%=(m+1);
			}
			while(!book[len])
			{
				len--;
			}
			printf("Test #%d:\n",t);
			for(int i=len;i>0;i--)
			{
				printf("%d",book[i]);
			}
			printf("\n");
			memset(book,0,sizeof(book));
		}
	}
	return 0;
 }
发布了46 篇原创文章 · 获赞 16 · 访问量 380

猜你喜欢

转载自blog.csdn.net/weixin_43357583/article/details/105231665