杭电_ACM_Buy the Ticket
2012-11-06 16:25
316 查看
[align=left]Problem Description[/align] 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. |
[align=left]Input[/align] 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. |
[align=left]Output[/align] 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. |
[align=left]Sample Input[/align]3 0 3 1 3 3 0 0 |
[align=left]Sample Output[/align]Test #1: 6 Test #2: 18 Test #3: 180 |
#include <stdio.h> int factical[205][400]; int result[400]; //get the factical from 1 to 200 void work() { int carry, length, i, j; factical[1][0] = 1; factical[1][1] = 1; for (i = 2; i <= 200; i++) { length = factical[i - 1][0]; carry = 0; for (j = 1; j <= length; j++) { carry += factical[i - 1][j] * i; factical[i][j] = carry % 10; carry /= 10; } while (carry) { factical[i][++length] = carry % 10; carry /= 10; } factical[i][0] = length; } } int main() { int m, n, count, length, carry, remainder, i, j, num; count = 0; work(); while (scanf("%d %d", &m, &n) != EOF) { if (!m && !n) break; count++; //if m < n, the result is zero. if (m < n) { printf("Test #%d:\n", count); puts("0"); continue; } num = n + m; carry = 0; length = factical[num][0]; //multiple m + 1 - n for (i = 1; i <= length; i++) { carry += factical[num][i] * (m + 1 - n); result[i] = carry % 10; carry /= 10; } while (carry) { result[++length] = carry % 10; carry /= 10; } //divide m + 1 remainder = 0; for (j = length; j > 0; j--) { remainder = remainder * 10 + result[j]; result[j] = remainder / (m + 1); remainder %= (m + 1); } while (!result[length]) { length--; } //formatting printing printf("Test #%d:\n", count); for (i = length; i > 0; i--) { printf("%d", result[i]); } puts(""); } return 0; }
Key points
firstly, this's catalan question.
secondly, the number of 200! is 375.
thirdly, in some cases, the answer is zero, you must care about.
last but not the least, if your code is wrong, you must check the answer step by step, you will be successful.
相关文章推荐
- 杭电ACM1133——Buy the Ticket
- Buy the Ticket&&http://acm.hdu.edu.cn/showproblem.php?pid=1133
- HDOJ HDU 1133 Buy the Ticket ACM 1133 IN HDU
- 杭电1133-Buy the Ticket
- ACM--哈利波特电影票--HDOJ 1133--Buy the Ticket--递推
- HDU 1133 Buy the Ticket
- HDU 1267 下沙的沙子有几粒? + HDU 1133 Buy the Ticket 递推 *
- HDU 1133 Buy the Ticket【卡特兰数】
- hdu 1133 Buy the Ticket(卡特兰数变形+组合数学+java大数)
- (卡特兰数变形m+n,找零)Buy the Ticket--HDOJ
- 【HDU 1133】 Buy the Ticket (卡特兰数)
- 杭电 acm 1032 The 3n + 1 problem
- hdu 1133 Buy the Ticket(Catalan)
- 杭电(hdu)ACM 1010 Tempter of the Bone
- 杭电ACM-HDU1004-Let the Balloon Rise
- 杭电acm 1004 (Let the Balloon Rise)
- hdu 1133 Buy the Ticket(卡特兰数+大整数)
- HDU-1133 Buy the Ticket (Catalan数)
- hdu1133 Buy the Ticket (卡兰特数应用+java大数)
- hdu 1133 Buy the Ticket(Catalan)