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结束
分析: 这题想了半天呀,最后还是用递推搞出来的,搜了下发现正是一个神奇的组合数学上的定理呀,先说下我的思路吧,首先呢答案就是问你有多少中可行的排列,既然是排列,那么大方向呢就是所有的减去不可行的。
用
所有的好求,就是
最后的答案就是
说下为什么和卡特兰偶遇了呢,因为当n == m的时候所有的组合数恰好就是对应的卡特兰数,直接给出结论吧,证明有点麻烦参考这里吧,组合数为
参考代码(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
- HDU 1133
- hdu-1133
- hdu-1133
- hdu 1133
- hdu 1133
- hdu 1133
- HDU 1133
- hdu 1133
- HDU 1133
- hdu 1133 DP
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- 实验七参考命令
- linux安装mysql
- C++ Tea
- 1025. 反转链表 (25)
- JPA 笔记
- HDU 1133
- 浅谈Linux下的EXT3文件系统
- 叮铃铃,搜狐科技手机月系列活动上线了
- 51nod 1435 数位阶乘 思路题
- 解决Qt:无法用“资源编辑器”打开prc文件的问题
- C++重载,隐藏,覆盖
- 深入理解android架构组件这个大家庭(一)
- 焦大seo:为什么交换友链“增加seo投票”获得排名
- 171111—函数练习:梯形法求积分