HDU 1133

来源:互联网 发布:渠道 知乎 编辑:程序博客网 时间:2024/05/18 00:21

Buy the Ticket

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

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

题意: 有m个人手里有50元,n个人手里有100元,都到店铺去买价值50元的东西,前提是m+n个人手里的钱都是整数,店铺一开始没有钱,问有多少中排队的方式,0,0结束

分析: 这题想了半天呀,最后还是用递推搞出来的,搜了下发现正是一个神奇的组合数学上的定理呀,先说下我的思路吧,首先呢答案就是问你有多少中可行的排列,既然是排列,那么大方向呢就是所有的减去不可行的。
F[i][j]来表示当m = i,n = j时的不可行方案
所有的好求,就是 (n+m)!,主要是不可行的,首先呢先考虑下特殊情况,当n = 0的时候,我们发现都是可行的,当m = 0的时候,我们发现都不可行(相应的方案数为n!),其余的呢就是一般的情况,我们可以枚举排列的最后一位是50还是100,如果是50的话,那么不可行的就是iF[i1][j]这里解释下为什么要乘上i呢,因为求的是排列,可以这样理解,我在i个50中挑一个放在最后一位,如果最后一位是100的话同理

最后的答案就是(i+j)!F[i][j],阶乘预处理即可,因为F数组里的值很大,考虑java的大数,当然c++也可以,偷懒了下

说下为什么和卡特兰偶遇了呢,因为当n == m的时候所有的组合数恰好就是对应的卡特兰数,直接给出结论吧,证明有点麻烦参考这里吧,组合数为C(m+n,n)C(m+n,n1) 说一下,这里的n指的是100面额的个数,也就是不符合的

参考代码(dp)

import java.util.Arrays;import java.util.Scanner;import  java.math.BigInteger;public class Main {    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        BigInteger[] s = new BigInteger[210];        BigInteger[][] dp = new BigInteger[105][105];        BigInteger[][] res = new BigInteger[105][105];        s[0] = BigInteger.valueOf(1);        for(int i = 1;i < 201;i++)            s[i] = s[i-1].multiply(BigInteger.valueOf(i));        dp[0][0] = BigInteger.ZERO;        for(int i = 1;i <= 100;i++) {            dp[0][i] = s[i];            dp[i][0] = BigInteger.valueOf(0);        }        for(int i = 1;i <= 100;i++) {            for(int j = 1;j <= 100;j++) {                if(j > i) dp[i][j] = s[i+j];                else dp[i][j] = dp[i-1][j].multiply(BigInteger.valueOf(i)).add(dp[i][j-1].multiply(BigInteger.valueOf(j)));            }        }        for(int i = 0;i <= 100;i++) {            for(int j = 0;j <= 100;j++) {                res[i][j] = s[i+j].subtract(dp[i][j]);            }        }        int T = 1;        while (in.hasNextInt()) {            int m = in.nextInt();            int n = in.nextInt();            if(n + m == 0) break;            System.out.println("Test #"+(T++)+":");            System.out.println(res[m][n]);        }    }}

参考代码(组合公式)

import java.math.BigInteger;import java.util.Scanner;public class Main {    public  static BigInteger[] f = new BigInteger[205];    public static void main(String[] args){        Scanner in = new Scanner(System.in);        init();        int m,n,T = 1;        BigInteger ans;        while (in.hasNextInt()){            m = in.nextInt();            n = in.nextInt();            if(m + n == 0) break;            if(n == 0) ans = f[m];            else  ans = f[m+n].divide((f[n].multiply(f[m]))).subtract(f[m+n].divide(f[n-1].multiply(f[m+1]))).multiply(f[n]).multiply(f[m]);            if(ans.compareTo(BigInteger.valueOf(0)) < 0) ans = BigInteger.ZERO;            System.out.println("Test #"+(T++)+":");            System.out.println(ans);        }    }    static  void init() {        f[0] = BigInteger.valueOf(1);        for(int i = 1;i <= 200;i++) {            f[i] = f[i-1].multiply(BigInteger.valueOf(i));        }    }}
  • 如有错误或遗漏,请私聊下UP,thx
原创粉丝点击