码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1133 Buy the Ticket

时间:2015-04-24 14:14:51      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

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
公式:(C(m+n, n)-C(m+n, m+1)) * m! * n! 化简即 (m+n)! * (m-n+1) / (m+1)
由公式:C(m, n) = m! / (n!*(m-n)!)推导得来
(C(m+n, n)-C(m+n, m+1)) * m! * n!:总共的排列减去违法的排列,又因为每个人不一样再乘以本身的全排列
#include
<stdio.h> #include<string.h> # define N 1010 int a[N], m, n; void Fact() { int i, j, s, r = 0; for (i = 2; i <= (m+n); i++) { for (j = 0; j < N; j++) { s = a[j] * i + r; r = s / 10; a[j] = s % 10; } } } //求出(m+n)! void Sum() { int i, j, c = m-n+1, d = m+1, b[N], k = 0, s, r = 0; for (i = 0; i < N; i++) a[i] *= c; for (i = 0; i < N-1; i++) { if (a[i] > 9) { a[i+1] += a[i] / 10; a[i] %= 10; } } //求出(m+n)! * (m-n+1) i = N-1; while (a[i] == 0) i--; for (j = i; j >= 0; j--) b[k++] = a[j]; for (i = 0; i < k; i++) { s = b[i] + 10*r; r = s % d; b[i] = s / d; } //求出(m+n)! * (m-n+1) / (m+1) i = 0; while (b[i] == 0) i++; for (j = i; j < k; j++) printf("%d", b[j]); printf("\n"); } int main () { int k = 0; while (scanf("%d %d", &m, &n), m+n) { k++; printf("Test #%d:\n", k); if (m < n) printf("0\n"); //要是拿100的人比拿50的人还多的话,那么不存在这种排法 else { memset(a, 0, sizeof(a)); a[0] = 1; Fact(); Sum(); } } return 0; }

 

HDU 1133 Buy the Ticket

标签:

原文地址:http://www.cnblogs.com/syhandll/p/4453170.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!