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

题目链接:http://acm.hdu.edu.cn/showproblem.php?

pid=1133

【题意】

电影票50块一张

有m个人手里正好有50块,n个人手里正好有100块,售票厅開始没有钱。问,有多少种排队的方式,能够让每一个人都买上票。

(假设售票厅没有50块零钱,则持有100块的人买不了票)

【分析】

显然。当m<n的时候,有0种排列方式。

当m>=n的时候:

用0。代表手里仅仅有50块的人,1,代表手里仅仅有100块的。

则0110100 这样的情况不能满足条件(到第三个人就买不了)

我们把包含第三个人及他之前的人 翻转 (1->0,  0->1)

1000100 出现了这个序列;

0110100   有 4个0,3个1   
1000100  有5个0 ,2个1

每个不能满足的情况都相应这样一个序列 所以 不能满足的条件的情况共同拥有

C(m+1,m+n);

我们计算公式就是:合法的排列方式=全部排列方式-非法排列方式

于是就有了F(N)=(-)*m!*n! 

然后再化简一下;

F(N) =(m+n)!

*(m-n+1)/(m+1)。

由于数据过大,所以用了JAVA大数来解决

【代码】

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);
            }
        }
}
时间: 2024-07-30 13:53:08

hdu1133 Buy the Ticket (卡兰特数应用+java大数)的相关文章

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

Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1886 Accepted Submission(s): 832   Problem Description The \\\\\\\"Harry Potter and the Goblet of Fire\\\\\\\" will be on show i

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

[分析] 当m<n,显然一定不合法. 所以我们考虑m>=n,类比n=m时的方案数推法,总方案为C(m,m+n),要减去不符合的情况. 我们扫描一个数,找到第一个不符合的位置,假设是有a个1,a+1个1,那么我们后面会填n-a-1个-1,m-a个1,我们把后面的1和-1交换,就得到了一个有m-a个-1,n-a-1个1的数,方案为C(m+1,m+n),把它减掉即可, 即ans=C(m,m+n)-C(m+1,m+n).因为每个人不相同最后1还要乘n!*m!. 为什么要用1和-1互换呢, 首先对于一个

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

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

Buy the Ticket{HDU1133}

Buy the TicketTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6517 Accepted Submission(s): 2720 Problem DescriptionThe "Harry Potter and the Goblet of Fire" will be on show in the next few day

Buy the Ticket DP +大数

Buy the Ticket 题目抽象:有m个手持50元的人,n个手持100元的人,售票处没有准备钱.问有多少种不同的方案使得购票不中断.每个人是不同的个体. 分析:简单DP题.画个格子,那么选取的点不越过对角线y = x.  dp[i][j]表示i个100元的人,j个手持50元的人的方案数.     i<=j dp[i][j] = dp[i-1][j]+dp[i][j-1];  i>j  dp[i][j] = 0;  如果对某些点有限制,那么在递推是加条件判断. ans = dp[n][m]

卡塔兰数简介

卡塔兰数(Catalan) 一.简介: 卡塔兰数是一个特殊的数列,在ACM程序设计.组合数学中会经常见到. 二.性质 (1)卡塔兰数的前几项 1,1,2,5,14,42,132,429,1430,4862,16796,58786,208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, 6564120420, 24466267020, 91482563640, 343059613650, 12899

hdu Buy the Ticket

1 import java.math.BigInteger; 2 import java.util.*; 3 public class Main { 4 public static void main(String []args) 5 { 6 Scanner cin=new Scanner(System.in); 7 int n,m,i; 8 int t1=0; 9 while(cin.hasNextBigInteger()) 10 { 11 t1++; 12 m=cin.nextInt();

catalan卡塔兰数

令h(0)=1,h(1)=1,卡塔兰数数满足递归式:h(n)= h(0)*h(n-1) + h(1)*h(n-2) + ... + h(n-1)h(0) (其中n>=2),这是n阶递推关系;还可以化简为1阶递推关系: 如h(n)=(4n-2)/(n+1)*h(n-1)(n>1) h(0)=1该递推关系的解为:h(n)=C(2n,n)/(n+1)=P(2n,n)/(n+1)!=(2n)!/(n!*(n+1)!) (n=1,2,3,...) 1 #include <iostream>

卡塔兰数(Catalan)

卡塔兰数(Catalan) 原理: 令h(0)=1,h(1)=1. 卡塔兰数满足递推式:h(n)=h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)h(0)(n>=2) 比如: h(2)=h(0)*h(1)+h(1)*h(0)=1*1+1*1=2 h(3)=h(0)*h(2)+h(1)*h(1)+h(2)*h(0)=1*2+1*1+2*1=5 另类递推式:h(n)=h(n-1)*(4*n-2)/(n+1); 递推关系的解为: h(n)=c(2n,n)/(n+1) (n=