• 杭电1133 排队买票 catalan


    1133

    Buy the Ticket

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 5511    Accepted Submission(s): 2309


    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
     
    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1133 



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

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

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

    然后每个人都是不一样的,所以需要全排列 m! * n! 所以最后的公式就是(C(m+n, n)-C(m+n, m+1))*m!*n! 化简后为 (m+n)!*(m-n+1)/(m+1);

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    using namespace std ;
    const int MAXN=500;
    int f[MAXN],a[MAXN];
    int main()
    {
        int i,j,t,k,n,m;
        int x;
        int c,s;
        int sign;
        k=0;
        while(cin>>m>>n&&!(m==0&&n==0))
        {
            printf("Test #%d:
    ",++k);
            memset(f,0,sizeof(f));
            memset(a,0,sizeof(a));
            f[0]=1;
            for(i=2; i<=m+n; i++)
            {
                c=0;
                for(j=0; j<MAXN; j++)
                {
                    s=f[j]*i+c;
                    f[j]=s%10;
                    c=s/10;
                }
            }
            c=0;
            if(m>=n)
            {
                x=m-n+1;
                for(i=0; i<MAXN; i++)
                {
                    s=f[i]*x+c;
                    f[i]=s%10;
                    c=s/10;
                }
                t=0;
                for(j=MAXN-1; j>=0; j--) if(f[j]) break;
                for(i=j; i>=0; i--) a[t++]=f[i];
                x=m+1;
                c=0;
                sign=0;
                for(i=0; i<t; i++)
                {
                    s=((c*10)+a[i])/x;
                    if(s!=0) sign=1;
                    if(sign!=0) cout<<s;
                    c=(c*10+a[i])%x;
                }
            }
            else cout<<"0";
            cout<<endl;
        }
        return 0;
    }

     
    I am a slow walker,but I never walk backwards.
  • 相关阅读:
    tesseract的简单使用
    快速添加请求头
    1010. 一元多项式求导 (25)
    1009. 说反话 (20)
    1008. 数组元素循环右移问题 (20)
    1007. 素数对猜想 (20)
    1006. 换个格式输出整数 (15)
    素数判断
    1002. 写出这个数 (20)
    1005. 继续(3n+1)猜想 (25)
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/4892997.html
Copyright © 2020-2023  润新知