• HDU 1133 Buy the ticket


    Buy the Ticket

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

    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
     
    题意:
    有n+m个人买票,票价一张50元,其中m个人有50元的钱,有n个人只有100元的钱,问有多少种排列方式使得柜台不会出现没钱找人的情况。
    思路:
    其实是-1,1序列问题的加强版,不过不能那么做,可以从中得到一些关于推导公式的思考。
    自己再推导的时候没有注意到人是不同的这个条件。而且自己推得公式好像确实比较麻烦。。
    我是想,如果n=m,直接就是卡特兰数,如果n>m,0,如果n<m,可以先考虑2n的情况,然后再把剩下的m-n个人插入这2n+1个空中,但是这里的组合还涉及到分组的情况,还是比较麻烦的。
    这个时候,我去看题解了,还能这样推?
    我们要求合法序列的数目,可以通过求不合法序列的数目来求得。
    从m+n个位子里给n个0,就是(m+n,n),
    假设在2k+1位置,有k+1个0,k个1,
    则后面有n-k-1个0,m-k个1.
    讲2k+2及之后部分01反转,则对应一个m+1个0,n-1个1的二进制数。
    也就有种数=(m+n,n)-(m+n,m+1);最后考虑人不一样,乘上n,m的阶乘。
     
    #include <algorithm>
    #include <iostream>
    #include <iomanip>
    #include <cstring>
    #include <climits>
    #include <complex>
    #include <fstream>
    #include <cassert>
    #include <cstdio>
    #include <bitset>
    #include <vector>
    #include <deque>
    #include <queue>
    #include <stack>
    #include <ctime>
    #include <set>
    #include <map>
    #include <cmath>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define maxn 100
    #define maxm 30000
    #define ll long long
    #define mod 1000000007
    #define mem(a,b) memset(a,b,sizeof a)
    #ifndef ONLINE_JUDGE
       #define dbg(x) cout<<#x<<"="<<x<<endl;
    #else 
       #define dbg(x) 
    #endif
    inline int read()
    {
        int x=0,f=1;
        char ch=getchar();
        while(ch<'0'||ch>'9')
        {
            if(ch=='-') f=-1;
            ch=getchar();
        }
        while(ch>='0'&&ch<='9')
        {
            x=10*x+ch-'0';
            ch=getchar();
        }
        return x*f;
    }
    inline void Out(int a) 
    {
        if(a>9)
            Out(a/10);
        putchar(a%10+'0');
    }
    int fac[205][100];
    void multiply(int a[],int ma,int b)
    {
          int i,array=0;
          for(i=ma-1;i>=0;i--)
          {
                array+=b*a[i];
                a[i]=array%10000;
                array/=10000;
          }
    }
    void init()
    {
          fac[0][maxn-1]=fac[1][maxn-1]=1;
          for(int i=2;i<=200;++i)
          {
                memcpy(fac[i],fac[i-1],maxn * sizeof(int));
                multiply(fac[i],maxn,i);
          }
    }
    
    void divide(int a[],int ma,int b)
    {
          int i,div=0;
          for(i=0;i<ma;++i)
          {
                div=div*10000+a[i];
                a[i]=div/b;
                div%=b;
          }
    }
    void output(int ctl[maxn])
    {
          int i=0;while(i<maxn&&ctl[i]==0) i++;
          printf("%d",ctl[i++]);
          while(i<maxn) printf("%04d",ctl[i++]);
          putchar('
    ');
    }
    int res[105];
    int main()
    {
          int n,m;
          int index=1;
          init();
          while(~scanf("%d%d",&m,&n)&&n+m)
          {
                printf("Test #%d:
    ",index++);
                if(n>m) {puts("0");continue;}
                //dbg(n);
                memcpy(res,fac[n+m],maxn * sizeof(int));
                multiply(res,maxn,m-n+1);
                divide(res,maxn,m+1);
                output(res);
          }
          return 0;
    }
  • 相关阅读:
    便签一
    当代大学生存在问题及需求分析
    字符 单词数 行数
    流量管理系统——需求分析
    简单的四则运算
    强大的修图app--美图秀秀
    js 中 setTimeout()的用法
    .net spit 用法
    关闭 Activity 关闭方式 finish(), exit(), killProcess(), restartPackage()(转载)
    Android实现购物车功能
  • 原文地址:https://www.cnblogs.com/TYH-TYH/p/9445878.html
Copyright © 2020-2023  润新知