您的位置:首页 > 其它

杭电_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


View Code

#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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: