zoukankan      html  css  js  c++  java
  • Buy the Ticket HDU 1133 卡特兰数应用+Java大数

    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
     
     
    题目意思:电影票50一张,m个人手里恰好有50元,n个人手里只有100元,开始电影售票员手里没有钱,问有多少种排队方式,使得每个人都能买上票。
     
    解题思路:为了使得每个人都买上票,手里有50的好说,而手里有100元的需要其排列之前有50的人,这样才会有找回的钱,是不是和入栈出栈问题很像?是的,如果n=m,那么这就是一个卡特兰数的应用,直接套用公式即可,对于这道题,我们需要变换一下。
    我们知道如果m<n,必然为0。当m>=n时,如果用0代表拿有50的人,用1代表拿有100的人。我们可以把拿有同一种钞票的人都看成一样的,现在只关注这一种的排列方式,之后再乘上n!和m!来消去把同一种钞票的人看成一种排列方法的影响就行了。
     
    如果有这么一个序列 0101101001001111..........  当第K个位置出现1的个数多余0的个数时就是一个不合法序列了 。
    假设m=4 n=3的一个序列是:0110100 显然,它不合法, 现在我们把它稍微变化一下:  把第二个1(这个1前面的都是合法的)后面的所有位0变成1,1变成0  就得到 0111011 这个序列1的数量多于0的数量, 显然不合法, 但现在的关键不是看这个序列是不是合法的  关键是:它和我们的不合法序列 0110100 成一一对应的关系  也就是说任意一个不合法序列(m个0,n个1), 都可以由另外一个序列(n-1个0和m+1个1)得到 
    另外我们知道,一个序列要么是合法的,要么是不合法的  所以,合法序列数量 = 序列总数量 - 不合法序列的总量 
    序列总数可以这样计算m+n 个位置中, 选择 n 个位置出来填上 1, 所以是 C(m+n, n) 
    不合法序列的数量就是m+n 个位置中, 选择 m+1 个位置出来填上1, 所以是 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);
     1 import java.util.*;
     2 import java.math.BigInteger;
     3 public class Main
     4 {
     5     public static void main(String[] args)
     6     {
     7         int a,b;
     8         Scanner in=new Scanner(System.in);
     9         int cnt=0;
    10         while(in.hasNext())
    11         {
    12             cnt++;
    13             a=in.nextInt();
    14             b=in.nextInt();
    15             BigInteger ans=BigInteger.ONE;
    16             if(a==0&&b==0)break;
    17             if(a<b) ans=BigInteger.ZERO;
    18             else
    19             {
    20                 for(int i=1; i<=a+b; i++)
    21                 {
    22                     ans=ans.multiply(BigInteger.valueOf(i));
    23                 }
    24                 int mpl=(a-b+1);
    25                 int dvd=(a+1);
    26                 ans=ans.multiply(BigInteger.valueOf(mpl));
    27                 ans=ans.divide(BigInteger.valueOf(dvd));
    28             }
    29             System.out.println("Test #"+cnt+":");
    30             System.out.println(ans);
    31         }
    32     }
    33 }

     https://blog.csdn.net/qq_33171970/article/details/50644971

    这位大佬的想法也很好,通过一个二维数组构造成一个矩阵,递推打表,看起来很清晰。

    这张图真是太棒了。

      题目分析,有n+m个人去买票,m个人拿50,n个人拿100,问有几种安排人排队的方法。明显是一个有关组合的问题,我们先判断一下特殊情况,只有在m>=n的情况下才可能安排排队,否则钱是找不开的,而且n=0时全是拿50的,那直接n的阶乘就好了。在一般情况下,可以列一个n*m的矩阵来表示答案,而且因为只要有几种排队方法确定以后直接乘以n的阶乘和m的阶乘就可以了,所以我们可以把n个人视为一样的,m个人视为一样的。那么就可以确定一般情况时排队方法有多少种。

        则第m+n个人的排队方式可以看做多了第m+n个人,本来已经有了(m+n-1)个人,如果这个人拿的是50,那么就是在((m-1)+n)的基础上多了一个人,此时第m+n个人站在最后面(因为每个人都一样,所以实际上已经考虑了所有的情况),同样,如果这个人拿的是100,那么就是在(m+(n-1))的基础上多了一个人,因为人 都一样,所以又有(m,n-1)这种情况的种类,那么第m+n个人的排队类数就是(m,n-1)和(m-1,n)的和,(事实上如果最后来的那个人不站最后面那么就会出现重复的排队数,你可以试试用笔推一下)。那么递推式就出来了,我们就可以用打表的方法利用递推把m,n个人对应的排队数目用数组存储起来
     我们可以发现,对角线上的数字就是卡特兰数,也就是说如果m=n,那么排队数目就是卡特兰数 。

        之后求阶乘的步骤就不用再说,排队的种类数乘以m的阶乘和n的阶乘就去掉了我们之前把拿同一种钞票的人视为一样的做法的影响了,那么我们得到的就是最终答案了。小提示,求m和n的阶乘的时候即使是0的阶乘也要注意返回一个1,否则当n=0时计算结果就会出现错误~~

     1 import java.util.*;
     2 import java.math.*;
     3 public class Main
     4 {
     5     public static void main(String[] args)
     6     {
     7         Scanner input=new Scanner(System.in);
     8         BigInteger a=new BigInteger("1");
     9         BigInteger c=new BigInteger("0");
    10         BigInteger ans[][]=new BigInteger[105][105];
    11         for(int i=0; i<=100; i++)
    12             for(int j=0; j<=100; j++)
    13             {
    14                 if(i<j) ans[i][j]=c;
    15                 else if(j==0) ans[i][j]=a;
    16                 else
    17                 {
    18                     ans[i][j]=ans[i][j-1].add(ans[i-1][j]);
    19                 }
    20             }
    21         int count=0;
    22         while(true)
    23         {
    24             int m=input.nextInt(),n=input.nextInt();
    25             if(m==0 && n==0) break;
    26             BigInteger e=new BigInteger("1");
    27             BigInteger f=new BigInteger("0");
    28             for(int i=1; i<=m; i++)
    29             {
    30                 f=f.add(a);
    31                 e=e.multiply(f);
    32             }
    33             BigInteger g=new BigInteger("1");
    34             BigInteger h=new BigInteger("0");
    35             for(int i=1; i<=n; i++)
    36             {
    37                 h=h.add(a);
    38                 g=g.multiply(h);
    39             }
    40             BigInteger b=ans[m][n].multiply(e).multiply(g);
    41             System.out.println("Test #"+ ++count+":");
    42             System.out.println(b);
    43         }
    44     }
    45 }
  • 相关阅读:
    ACwing98 分形之城 分形图
    ACwing96 奇怪的汉诺塔 递推
    ACwing95 费解的开关 bfs
    ACwing94 递归实现排列型枚举 dfs
    ACwing93 递归实现组合型枚举 dfs
    递归型枚举总结
    洛谷P2286 宠物收养场 splay
    python之路——初识数据库
    python之路——协程
    python之路——线程
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/9865127.html
Copyright © 2011-2022 走看看