HDU 1133 Buy the Ticket【卡特兰数】
来源:互联网 发布:mathematics软件 编辑:程序博客网 时间:2024/05/22 11:55
Buy the Ticket
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 11 Accepted Submission(s) : 3
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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.
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 03 13 30 0
Sample Output
Test #1:6Test #2:18Test #3:180
Author
推導過程如下:
m個人拿50,n個人拿100
1、如果n > m,那麼排序方法數為0,這一點很容易想清楚
2、現在我們假設拿50的人用‘0’表示,拿100的人用‘1’表示。
如果有這麼一個序列0101101001001111。
當第K個位置出現1的個數多餘0的個數時就是一個不合法的序列了
假設m=4,n=3的一個序列是:0110100 。顯然,它不合法,現在我們把它稍微變化一下:
把第二個1(這個1前面的都是合法的)後面的所有位0變成1,1變成0.
就得到0111011這個序列1的數量多餘0的數量,顯然不合法,但現在的關鍵不是看這個序列是不是合法的
關鍵是:他和我們的不合法序列0110100成一一對應的關係。
也就是說任意一個不合法序列(m個0,n個1),都可以由另外一個序列(n-1個0和m+1個1)得到。
另外我們知道,一个序列要麼是合法的,要麼是不合法的
所以,合法序列數量 = 序列總數量 - 不合法序列的總量
序列總數可以這樣計算 m+n個位置中,選擇n個位置出來填上1,所以是C(m+n,n).
不合法序列的數量就是: m+n個位置中,選擇m+1個位置出來填上1,所以是C(m+n,m+1).
然後每個人都是不一樣的,所以需要全排列m! * n!.
所以最後的公式為:( C(m+n,n) - C(m+n,m+1) ) * m! * n!
化簡即為:(m+n)!*(m-n+1)/(m+1)
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); } }}
阅读全文
0 0
- hdu 1133 Buy the Ticket(卡特兰数 + 高精度)
- HDU 1133 Buy the Ticket 卡特兰数
- hdu 1133 Buy the Ticket(卡特兰数)
- HDU 1133 Buy the Ticket 卡特兰数 JAVA
- HDU-1133Buy the Ticket 卡特兰数买票问题
- HDU-1133Buy the Ticket 卡特兰数买票问题
- HDU 1133 Buy the Ticket【卡特兰数】
- HDOJ 1133 Buy the Ticket(高精度卡特兰数变形)
- HDU 1133 Buy the Ticket 50,100钱买票,大数乘除法,卡特兰数变形
- hdu 1133 Buy the Ticket(卡特兰数变形+组合数学+java大数)
- hdu 1133 Buy the Ticket 卡特兰数+递推+java
- HDOJ/HDU 1133 Buy the Ticket(数论~卡特兰数~大数~)
- hdu 1133 Buy the Ticket(卡特兰数+大整数)
- hdoj 1133 Buy the Ticket 【卡特兰】
- Buy the Ticket(卡特兰数应用与大数)
- 【高精度练习+卡特兰数】【Uva1133】Buy the Ticket
- 【hdoj_1133】Buy the Ticket(卡特兰数+大数)
- Buy the Ticket(卡特兰数 + JAVA大数)
- 字符串的排列
- 《java多线程编程核心技术》
- 封装自定义滚动条
- MaxCompute分区表和非分区表使用对比
- Scala练习-斐波那契查找
- HDU 1133 Buy the Ticket【卡特兰数】
- 2017.6.26~2017.7.5训练总结
- DeepLearning tutorial(2)机器学习算法在训练过程中保存参数
- 《Spring3实战》摘要(3-1)--最小化Spring XML配置之自动装配Bean属性
- 列转行
- 【转】nodejs全局安装和本地安装的区别
- Android Webview 详解
- VS2012远程调试
- 在java中使用word来简单进行分词