hdu 1133 Buy the Ticket

来源:互联网 发布:6300hq和7300hq知乎 编辑:程序博客网 时间:2024/06/08 10:18

# include <stdio.h># include <string.h>char s[1010], buff[1010] ;#define mul(s, x)\{\    for (cc = 0, len = strlen(s), j = len-1 ; j >= 0 ; j--)\        cc = (s[j]-'0') * (x) + cc ,\        s[j] = cc%10 + '0' ,\        cc/=10 ;\    if (cc)\        sprintf (buff, "%d", cc),\        strcat(buff, s),\        strcpy(s, buff);\}int main (){    int n, m, i, j, nCase = 1, cc, len ;    while (~scanf ("%d%d", &m, &n) && (m||n))    {        if (m < n) strcpy(s, "0") ;        else{            strcpy(s, "1") ;            for (i = 1 ; i <= (m+n) ; i++) if (i != m+1)                mul(s, i) ;            if (n != 0) mul (s, m-n+1) ;        }        printf ("Test #%d:\n%s\n", nCase++, s) ;    }    return 0 ;}

Buy the Ticket

买票
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5285    Accepted Submission(s): 2232


Problem Description
The "Harry Potter and the Goblet of Fire" will be on show in the next few days.
<span style="background-color: rgb(255, 255, 255);">“哈利·波特与火焰杯”将会在接下来的几天里放映。</span>
 As a crazy fan of Harry Potter, you will go to the cinema and have the first sight, won’t you?
<span style="background-color: rgb(255, 255, 255);">作为哈利波特的热粉,你会去看电影,会一见钟情吗?</span>
Suppose the cinema only has one ticket-office and the price for per-ticket is 50 dollars. 
<span style="background-color: rgb(255, 255, 255);">假设电影院只有一个售票处,每张票的价格是50美元。</span>
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).
<span style="background-color: rgb(255, 255, 255);">购买门票的队列有m + n人</span><span style="background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif;">(m人有50美元,n人有100美元)。</span>
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. 
<span style="background-color: rgb(255, 255, 255);">现在你的题目是计算队列的各种购买方式,购买过程是连续的。</span>
Note: initially the ticket-office has no money.
<span style="background-color: rgb(255, 255, 255);"> 注意:最初售票处没有零钱。</span>
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.
<span style="background-color: rgb(255, 255, 255);">如果零钱不够那么购买过程就会停止。</span>

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.
 输入有多个。输入由两个整数组成,分别为m和n。如果m=n =0,表示输入结束(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
 

Author
HUANG, Ninghai

{实例
result = A(3,3)*A(3,3)*( 1+C(2,3)+1 );//1表示100,5表示50
1) 5 1 5 1 5 11种
2) 5 5 1 1 5 1C(2,3)=3 种
3) 5 5 5 1 1 11种
}
0 0
 
Sample Output
Test #1:6Test #2:18Test #3:180
 

Author
HUANG, Ninghai


简略题意:电影院卖票。一张票50元。一开始没有零钱。有m+n个人买票,m个人拿50元的钞票,n个人拿100的。问队伍有多少种排列方式可以使得卖票能顺利进行下去。

注意:如果要使得卖票的行为进行下去,对于任意前k个人,必须满足这k个人里面拿100的人数不多于拿50的人数。结果会是一个大整数,要用高精度(long)。

公式是A(m,m)*A(n,n)*(m-n+1)/(m+1) = m!n!(m-n+1)/(m+1),条件(m>=n)。

Catalan(卡特兰)数问题的一个变形:(N与m与题意相反)

n+m个人排队买票,并且满足n \ge m,票价为50元,其中n个人各手持一张50元钞票,m个人各手持一张100元钞票,除此之外大家身上没有任何其他的钱币,并且初始时候售票窗口没有钱,问有多少种排队的情况数能够让大家都买到票。

这个题目是Catalan数的变形,不考虑人与人的差异,如果m=n的话那么就是我们初始的Catalan数问题,也就是将手持50元的人看成是+1,手持100元的人看成是-1,任前k个数值的和都非负的序列数。

这个题目区别就在于n>m的情况,此时我们仍然可以用原先的证明方法考虑,假设我们要的情况数是D_{n+m},无法让每个人都买到的情况数是U_{n + m},那么就有D_{n + m} + U_{n +m} = {n + m \choose n},此时我们求U_{n + m},我们假设最早买不到票的人编号是k,他手持的是100元并且售票处没有钱,那么将前k个人的钱从50元变成100元,从100元变成50元,这时候就有n+1个人手持50元,m-1个手持100元的,所以就得到U_{n + m} = {n + m \choose n + 1},于是我们的结果就因此得到了,表达式是D_{n + m} = {n + m \choose n} - {n + m \choose n + 1}


0 0
原创粉丝点击