HDOJ/HDU 1133 Buy the Ticket(数论~卡特兰数~大数~)
Problem Description 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. 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 Output Sample Input Sample Output 题意: 就是m+n个人去买票,票价50元,m个人只带了50元一张的纸币,n个人只带了100元一张的纸币,售票员一开始手里没有钱。 也就是说:到100元的人买票之前,售票员手里必须要有一张50元的。 推导过程如下: 所以最后的公式为:( C(m+n,n) - C(m+n,m+1) ) * m! * n! 化简即为:(m+n)!*(m-n+1)/(m+1) 如果没看懂:可以参考我的前面一篇卡特兰数的分析: import java.math.BigInteger; import java.util.Scanner; /** * @author 陈浩翔 * * 2016-5-19 */ public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t=0; while(sc.hasNext()){ int m =sc.nextInt(); int n =sc.nextInt(); if(n==0&&m==0){ return; } System.out.println("Test #"+(++t)+":"); if(n>m){ System.out.println(0); continue; } BigInteger a = new BigInteger("1"); for(int i=2;i<=m+n;i++){ a=a.multiply(new BigInteger(""+i)); } a=a.multiply(new BigInteger(""+(m-n+1))); a=a.divide(new BigInteger(""+(m+1))); System.out.println(a); } } } (编辑:开发网_开封站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |