题目链接:https://vjudge.net/problem/HDU-1133

Buy the Ticket

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

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
 
Recommend
Eddy

题意:

电影票50元一张,有m个拿着50元和n个拿着100元的人去买票。开始时售票处没有一分钱(即开始时不能为100元的找钱),问:这m+n个人应该怎么排队,才能使得每个人都能买到票(即都能找钱或不用找钱)?

题意:

1. 当m<n时,肯定不能满足条件,因为50元的张数小于100元的张数,所以不能为所以的100元找回50元。

2. 当m>=n时,通过合理的安排,就能满足条件:

2.1 首先所有的排列数为 C[m+n][m],即从m+n个位置中挑选m个,作为50元的位置。

2.2 然后排除掉非法排列:假设第k个100元不能找钱, 则前面有k-1个50元,所以后面有n-k个100元, m-k+1个50元,如果把第k个之后的人50换成100,100换成50,那么总的就变成了有:m+1个100元, n-1个50元,那么C[m+n][m+1]就是非法的排列数。

2.3 所以合法的排列数为:C[m+n][m] - C[m+n][m+1],由于人是有区别的, 所以还需要乘上两种人的全排列,所以 ans = (C[m+n][m] - C[m+n][m+1])*A[m]*A[n]。

代码如下:

 //package main;
import java.util.Scanner;
import java.math.BigInteger; public class Main { public static void main(String[] args){ BigInteger[] A = new BigInteger[];
BigInteger[][] C = new BigInteger[][]; A[] = BigInteger.valueOf();
for(int i = ; i<=; i++) {
A[i] = A[i-].multiply(BigInteger.valueOf(i));
} for(int i = ; i<=; i++)
for(int j = ; j<=; j++)
C[i][j] = BigInteger.valueOf(); for(int i = ; i<=; i++) {
C[i][] = BigInteger.valueOf();
for(int j = ; j<=i; j++) {
C[i][j] = C[i-][j].add(C[i-][j-]);
}
} int kase = ;
Scanner input = new Scanner(System.in);
while(input.hasNext()){
int m = input.nextInt();
int n = input.nextInt();
if(m+n==) break;
System.out.println("Test #"+(++kase)+":");
BigInteger ans = BigInteger.ZERO;
if(m>=n) {
ans = A[m].multiply(A[n].multiply((C[m+n][m].subtract(C[m+n][m+]))));
}
System.out.println(ans);
}
}
}

HDU1133 Buy the Ticket —— 卡特兰数的更多相关文章

  1. 【hdoj_1133】Buy the Ticket(卡特兰数+大数)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1133 题目的意思是,m个人只有50元钱,n个人只有100元整钱,票价50元/人.现在售票厅没钱,只有50元 ...

  2. HDU 1133 Buy the Ticket 卡特兰数

    设50元的人为+1 100元的人为-1 满足前随意k个人的和大于等于0 卡特兰数 C(n+m, m)-C(n+m, m+1)*n!*m! import java.math.*; import java ...

  3. hdu1133 Buy the Ticket (卡兰特数应用+java大数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1133 [题意] 电影票50块一张 有m个人手里正好有50块,n个人手里正好有100块,售票厅開始没有 ...

  4. 【高精度练习+卡特兰数】【Uva1133】Buy the Ticket

    Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  5. Buy the Ticket(卡特兰数+递推高精度)

    Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...

  6. 【HDU 1133】 Buy the Ticket (卡特兰数)

    Buy the Ticket Problem Description The "Harry Potter and the Goblet of Fire" will be on sh ...

  7. HDUOJ---1133(卡特兰数扩展)Buy the Ticket

    Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  8. HDOJ/HDU 1133 Buy the Ticket(数论~卡特兰数~大数~)

    Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...

  9. Buy the Ticket HDU 1133 卡特兰数应用+Java大数

    Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...

随机推荐

  1. C++: 多态 虚函数

    一.多态: 1.多态是什么:具有不同功能的函数可以用同一个函数名 2.静态多态:程序编译时决定,通过函数重载实现. 3.动态多态:程序运行时决定,通过虚函数实现. 二.虚函数: 1.引入目的:可以通过 ...

  2. python学习笔记1-numpy/enumerate

    1. np.size和np.prod import numpy as np x = np.zeros((3, 5, 2), dtype=np.complex128) # ndarray.size is ...

  3. Maven添加坐标(依赖)及在Eclipse中的操作

    例如:添加一个spring-test.jar的依赖过程,普遍的做法就是直接操作pom.xml文件. 1.打开maven的中央仓库:http://search.maven.org/ 2.搜索仓库: 进入 ...

  4. 【android】getDimension()、getDimensionPixelOffset()和getDimensionPixelSize()区别详解

    在自定义控件中使用自定义属性时,经常需要使用java代码获取在xml中定义的尺寸,相关有以下三个函数 getDimension() getDimensionPixelOffset() getDimen ...

  5. linux下添加自动启动项,linux 开机自动启动脚本方法

    #service servicename status是当前状态#chkconfig --list servicename是查看启动状态,也就是是否开机自动启动 首先写好脚本,如 mysql,把它放到 ...

  6. MFC office2007风格设置左侧导航栏 [转]

    当基础的框架搭好以后,我想为其添加一个左侧导航栏,过程如下:在框架类的头文件添加一个导航栏参数: CMFCOutlookBar m_navigation; 为了完善功能,在导航栏里面我添加了一个CTr ...

  7. Mac 安装配置Mysql

    Mac下安装配置Mysql By 白熊花田(http://blog.csdn.net/whiterbear) 转载需注明出处,谢谢. 下载安装 去官网下载Community版本号的mysql安装文件. ...

  8. 近期微信上非常火的小游戏【壹秒】android版——开发分享

    近期在朋友圈,朋友转了一个html小游戏[壹秒],游戏的规则是:用户按住button然后释放,看谁能精准地保持一秒的时间.^_^刚好刚才在linuxserver上调试程序的时候server挂了,腾出点 ...

  9. PS 基础知识 渐变编辑器如何使用

    ps渐变编辑器在哪 [ 标签:渐变,ps 渐变,编辑器 ] _______志 敏 回答:3 人气:9 解决时间:2009-04-16 15:28 满意答案 你先点渐变工具 然后左上出现渐变条设置 如图 ...

  10. UVA 10526 - Intellectual Property (后缀数组)

    UVA 10526 - Intellectual Property 题目链接 题意:给定两个问题,要求找出第二个文本抄袭第一个文本的全部位置和长度,输出前k个,按长度从大到小先排.长度一样的按位置从小 ...