Buy the Ticket HDU 1133 卡特兰数应用+Java大数
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.
import java.util.*;
import java.math.BigInteger;
public class Main
{
public static void main(String[] args)
{
int a,b;
Scanner in=new Scanner(System.in);
int cnt=0;
while(in.hasNext())
{
cnt++;
a=in.nextInt();
b=in.nextInt();
BigInteger ans=BigInteger.ONE;
if(a==0&&b==0)break;
if(a<b) ans=BigInteger.ZERO;
else
{
for(int i=1; i<=a+b; i++)
{
ans=ans.multiply(BigInteger.valueOf(i));
}
int mpl=(a-b+1);
int dvd=(a+1);
ans=ans.multiply(BigInteger.valueOf(mpl));
ans=ans.divide(BigInteger.valueOf(dvd));
}
System.out.println("Test #"+cnt+":");
System.out.println(ans);
}
}
}
https://blog.csdn.net/qq_33171970/article/details/50644971
这位大佬的想法也很好,通过一个二维数组构造成一个矩阵,递推打表,看起来很清晰。

这张图真是太棒了。
则第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时计算结果就会出现错误~~
import java.util.*;
import java.math.*;
public class Main
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
BigInteger a=new BigInteger("1");
BigInteger c=new BigInteger("0");
BigInteger ans[][]=new BigInteger[105][105];
for(int i=0; i<=100; i++)
for(int j=0; j<=100; j++)
{
if(i<j) ans[i][j]=c;
else if(j==0) ans[i][j]=a;
else
{
ans[i][j]=ans[i][j-1].add(ans[i-1][j]);
}
}
int count=0;
while(true)
{
int m=input.nextInt(),n=input.nextInt();
if(m==0 && n==0) break;
BigInteger e=new BigInteger("1");
BigInteger f=new BigInteger("0");
for(int i=1; i<=m; i++)
{
f=f.add(a);
e=e.multiply(f);
}
BigInteger g=new BigInteger("1");
BigInteger h=new BigInteger("0");
for(int i=1; i<=n; i++)
{
h=h.add(a);
g=g.multiply(h);
}
BigInteger b=ans[m][n].multiply(e).multiply(g);
System.out.println("Test #"+ ++count+":");
System.out.println(b);
}
}
}
Buy the Ticket HDU 1133 卡特兰数应用+Java大数的更多相关文章
- hdu1133 Buy the Ticket (卡兰特数应用+java大数)
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1133 [题意] 电影票50块一张 有m个人手里正好有50块,n个人手里正好有100块,售票厅開始没有 ...
- hdu 1133(卡特兰数变形)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1133 题意:排队买50块一张的票,初始票台没有零钱可找,有m个人持有50元,n人持有100元, ...
- Buy the Ticket HDU 1133 递推+大数
题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1133 题目大意: 有m+n个人去买电影票,每张电影票50元, m个人是只有50元一张的, n个人 ...
- Buy the Ticket HDU 1133
传送门 [http://acm.hdu.edu.cn/showproblem.php?pid=1133] 题目描述和分析 代码 #include<iostream> #include< ...
- Buy the Ticket HDU - 1133 大数dp
题意: 演唱会门票售票处,那里最开始没有零钱.每一张门票是50元,人们只会拿着100元和50元去买票,有n个人是拿着50元买票,m个人拿着100元去买票. n+m个人按照某个顺序按序买票,如果一个人拿 ...
- hdu 1133 卡特兰 高精度
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- hdu 1023 卡特兰数+高精度
Train Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 4828 (卡特兰数+逆)
HDU 4828 Grids 思路:能够转化为卡特兰数,先把前n个人标为0.后n个人标为1.然后去全排列,全排列的数列.假设每一个1的前面相应的0大于等于1,那么就是满足的序列,假设把0看成入栈,1看 ...
- hdu 1130,hdu 1131(卡特兰数,大数)
How Many Trees? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
随机推荐
- 使用属性Props完成一张卡片
一:我们先安装bootstrap,为了使我们的样式好看些 cnpm install bootstrap --save 二:我们在index.js中引入bootstap Import ‘bootst ...
- 19-3-14Python中函数的进阶
1.动态参数: def func(*args): #在形参位置*叫做聚合 print(args) #元组形式 func(1,2,3,4,5,6) def func(**kwargs): # 动态关键字 ...
- (Les16 执行数据库恢复)-控制文件恢复
测试丢失所有控制文件恢复[20180517] rman target / show all; configure channel 1 device type disk format ' ...
- Nginx与Tomcat实现请求动态数据与请求静态资源的分离
上篇博客说明了Nginx在应用架构中的作用,以及负载均衡的思路.这篇实践一下其中的访问静态资源与访问动态资源的操作. 一.认识访问静态资源与访问动态资源的区别 静态资源:指存储在硬盘内的数据,固定的数 ...
- HTML5常用标签及特殊字符表
*http://html5doctor.com/nav*http://html5doctor.com/article*http://html5doctor.com/section*http://htm ...
- JavaScript入门学习(1)
<html> <script type ="text/javascript"> var i,j; for (i=1;i<10;i++){ for (j ...
- 一起来学大数据——走进Linux之门,学习大数据的重中之重
昨天我们看了有关大数据Hadoop的一些知识点,但是要在学习大数据之前,我们还是要为大数据的环境做一些的部署. 那么,今天我们就来讲讲开启我们大数据之路的Linux,跟上我们的脚步yo~ Linux介 ...
- 20155216 实验二 Java面向对象程序设计
实验二:Java面向对象程序设计 实验内容 1.初步掌握单元测试和TDD. 2.理解并掌握面向对象的三要素:封装.继承.多态. 3.初步掌握UML建模. 4.熟悉S.O.L.I.D原则. 5.了解设计 ...
- 20155233 2006-2007-2 《Java程序设计》第2周学习总结
20155233 2006-2007-2 <Java程序设计>第2周学习总结 教材学习内容总结 本章主要学习Java的基础语法,这些语法在C语言的学习中基本上都涉及到过,基本上,Java可 ...
- 【LG4317】花神的数论题
[LG4317]花神的数论题 题面 洛谷 题解 设\(f_{i,up,tmp,d}\)表示当前在第\(i\)位,是否卡上界,有\(tmp\)个一,目标是几个一的方案数 最后将所有\(d\)固定,套数位 ...