hdoj Buy the Ticket 1133 (排列组合题)

来源:互联网 发布:firefox linux 安装 编辑:程序博客网 时间:2024/05/21 03:26

Buy the Ticket

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


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 03 13 30 0

Sample Output
Test #1:6Test #2:18Test #3:180
//题意:
要去买票,每张票50元,但买票商没有多余的零钱。现在问有n个人是50元,m个人是100元,问总共有几种排列方式。
//思路:
转换:也就是从左往右遍历,并且50元的个数总大于等于100的个数,只有这样才符合要求。
先找出符合要求的排列,也就是在m+n个空中最多填n个100,及C(n+m,n)
再找出不符合要求的,也就是在m+n个空中填m+1个100,及C(n+m,m+1)
所以共有( C(m+n, n) - C(m+n, m+1) ) * m! * n! 化简即 (m+n)! * (m-n+1/ (m+1) 种情况。
#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>using namespace std;int a[1001];void dsx(int n){int c=0,i;for(i=0;i<=10000;i++){c=a[i]*n+c;a[i]=c%10;c/=10;}}void dsz(int n){int c=0,i;for(i=10000;i>=0;i--){c=a[i]+c*10;a[i]=c/n;c%=n;}}int main(){int n,m,s,i,j,T=1;while(scanf("%d%d",&n,&m),n|m){if(n<m)printf("Test #%d:\n0\n",T++);else{memset(a,0,sizeof(a));a[0]=1;s=n+m;for(i=1;i<=s;i++)dsx(i);dsx(n-m+1);dsz(n+1);for(i=10000;i>=0;i--)if(a[i])break;printf("Test #%d:\n",T++);for(j=i;j>=0;j--)printf("%d",a[j]);printf("\n");}}}

0 0
原创粉丝点击