Buy the Ticket HDU - 1133 (递推+动态规划+组合)

题目:
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时,就缓冲一个50 ,当进来一个100 时,如果缓冲区存在一个50,即将此50从缓冲区 移去,来抵消这个100,如果缓冲区没有50,且进来的是一个100,则队列终止。现在要求有m个50和n个100,即队列总长度是m+n是,问满足条件的队列有多少种,
我的思路:
首先我观察题目的样例数据,m=3,n=0时,答案是6而不是1说明即使三个人拿的都是50,但是仍不能算作是相同的人,即m+n个人里,每个人 都是不一样的,其实我们可以先看成拿50的人都是一样的,100的也都是一样的,假设我们把这个种类数(设为t)求出来,那么答案是就是
tmn t ∗ m ! ∗ n ! ,这个需要数学的排列组合知识,m个人的位置都是相对有顺序的,所以只要乘以m的阶乘即可,n同理。
现在分析这个t,我们观察可以知道 ,显然 m小于n时这种队列是不存在的,所以为0
当n=0时,显然t=1;
这两个就是动态规划的递推的边界。
好,现在我们定义函数
fmn f ( m , n ) :当m个50,n个100时的t的值;
状态转移方程: fmn=fm1n+fmn1 f ( m , n ) = f ( m − 1 , n ) + f ( m , n − 1 )
假设队列最后一个人是m,那我只需要知道n+m-1里的总排列顺序,且减少的是m。 fmn1 f ( m , n − 1 ) 同理得出。
好了,有了转移方程,代码也很容易了
有一点要注意的就是精度问题,别傻傻用int,或者是long。

代码

import java.math.BigInteger;
import java.util.Scanner;

public class Main 
{   
    static BigInteger num[][]=new BigInteger[101][101];
    static BigInteger j[]=new BigInteger[101];
    public static void main(String[]agrs)
    {
        Scanner sc=new Scanner(System.in);
        j[0]=BigInteger.ONE;
        int count=0;
        for(;;)
        {
            int m=sc.nextInt();
            int n=sc.nextInt();
            if(m==0&&n==0)break;
            count++;
            BigInteger t=get(m,n);
            System.out.println("Test #"+count+":");
            System.out.println(t.multiply(g(m)).multiply(g(n)));
        }
    }
    static BigInteger get(int m,int n)
    {
        if(num[m][n]!=null)return num[m][n];//记忆化的实现细节
        if(m<n)return num[m][n]=BigInteger.ZERO;//边界
        if(n==0)return num[m][n]=BigInteger.ONE;//边界
        return num[m][n]=get(m,n-1).add(get(m-1,n));
    }
    static BigInteger g(int n)
    {
        if(j[n]!=null)return j[n];
        return j[n]=g(n-1).multiply(BigInteger.valueOf(n));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值