Buy the Ticket

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

Author

HUANG, Ninghai

第一眼看到這個題,是購票問題的變種。也就是卡特蘭數的應用題,比較不同的是,這個題需要重新構建公式。這套公式和卡特蘭數的公式只是相似而並沒有什麼聯繫。這是因為這個題要輸入兩個參數進去,顯然這就不能用遞推公式了,必須構建直接等於的公式。
當n>m,顯然就不存在方案數了。
其餘情況下,我們嘗試這把50圓看成入棧,100圓看成出棧,棧可以不空,但是不可以為負。我們就可以想明白了~
這道題的關鍵就是不要被卡特蘭數的慣性思維給阻擋,要重新規劃和構建。
根據組合數學的知識,我們不難得到公式就是:
(C(m+n,m)-C(m+n,m+1))*m!*n!
化簡后為
(m+n)!*(m-n+1)/(m+1);
於是我寫了兩套計算公式,都可以AC,相比之下化簡后的公式更高效。

高精度乘法+高精度除法+高精度減法

代碼如下:

#include<iostream>
#include<cstring>
using namespace std;
#define maxsize 1200
#define maxn 1200
int m, n;
void mem();
int b[maxsize];
int c[maxsize];
void mem()
{
    memset(b, 0, sizeof(b));
    memset(c, 0, sizeof(c));
    b[1] = c[1] = 1;
}
void division(int m, int b[])//高精度除法
{
    int len, carry, temp;
    for (int i = maxsize-1; i >=1; i--)
    {
        if (b[i] != 0)
        {
            len = i;
            break;
        }
    }
    carry = 0;
    for (int i = len; i >= 1; i--)
    {
        b[i] += 10*carry;
        carry = b[i] % m;
        b[i] /= m;
    }
}
void factorial(int m,int b[])//求階乘
{
    int len, carry, temp;
    for (int i = maxsize - 1; i >= 0; i--)
    {
        if (b[i] != 0)
        {
            len = i;
            break;
        }
    }
    carry = 0;  
    for (int i = 1; i <= m; i++)
    {
        for (int j = 1; j <= len; j++)
        {
            b[j] *= i;
        }
        for (int j = 1; j <= len; j++)
        {
            b[j] += carry;
            carry = b[j] / 10;
            b[j] %= 10;
        }
        while (carry)
        {
            b[++len] = carry % 10;
            carry /= 10;
        }
    }
}
void subtraction(int b[], int c[])
{
    int len, carry, temp;
    for (int i = maxsize - 1; i >= 0; i--)
    {
        if (b[i] != 0)
        {
            len = i;
            break;
        }

    }
    carry = 0;
    for (int i = 1; i <=len; i++)
    {
        b[i] -= carry+c[i];
        carry = 0;
        if (b[i] < 0)
        {
            carry = 1;
            b[i] += 10;
        }   
    }
}
void getans()//根據推導公式 (C(m+n,m)-C(m+n,m+1))*m!*n!
{
    factorial(m+n, b);
    factorial(m+n, c);
    //針對分子上的m!以及(m+n-m)!分部計算
    for (int i = 1; i <= m; i++)//計算(m+n)!/m!
    {
        division(i, b);
    }
    for (int i = 1; i <= n; i++)//計算C(m+n,m)
    {
        division(i, b);
    }
    for (int i = 1; i <= m + 1; i++)//計算(m+n)!/(m+1)!
    {
        division(i, c);
    }
    for (int i = 1; i <= n-1; i++)//計算C(m+n,m+1)
    {
        division(i, c);
    }
    if (n > m)
    {
        memset(c, 0, sizeof(c));
    }
    subtraction(b, c);//計算C(m+n,m)-C(m+n,m+1)
    factorial(m, b);
    factorial(n, b);
}
void getans2()//是getans()的化簡版本,如此操作可以大大加快運行時間。實際上只是進行了數學上的公式化簡。
              //(m+n)!*(m-n+1)/(m+1);
{
    mem();
    int len, carry, temp;
    factorial(m + n, b);//計算m+n的階乘
    for (int i = maxsize - 1; i >= 0; i--)
    {
        if (b[i] != 0)
        {
            len = i;
            break;
        }

    }
    carry = 0;
    for (int j = 1; j <= len; j++)
    {
        b[j] *= m-n+1;
    }
    for (int j = 1; j <= len; j++)
    {
        b[j] += carry;
        carry = b[j] / 10;
        b[j] %= 10;
    }
    while (carry)
    {
        b[++len] = carry % 10;
        carry /= 10;
    }
    division(m + 1, b);
}
int main()
{

    int point = 1;
    while (~scanf("%d%d", &m, &n) && m + n)
    {
        mem();
        //getans();//方案一
        getans2();//方案二
        //ps:方案二更為高效,雖然兩者皆可AC
        printf("Test #%d:\n", point);
        if (n > m)
        {
            printf("0\n");
        }
        else
        {
            int len;
            for (int k = maxsize - 1; k>= 0; k--)
            {
                if (b[k] != 0)
                {
                    len = k;
                    break;
                }
            }
            for (int i = len; i >= 1; i--)
            {
                printf("%d", b[i]);
            }
            printf("\n");
        }
        point++;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值