hdoj 1133 Buy the Ticket 【卡特兰】

题意:有m个人(拿50元)和n个人(拿100元)去买票,售票厅刚开始没有,问最后所有人都能够买到的方式的种类数。

这道题也是经典的卡特兰数类型题。

我们可以将他们看做是火车进出站,但是由于人是不同的,所以最后还要乘上m!*n!

最后的数学表达是就是(C(m+n,n)-C(m+n, m+1))*m!*n!=》 结果为 (m!*n!)*(m+1-n)/(m+1)

注:m<n时 直接输出0

代码:

import java.util.Scanner;
import java.math.*;

public class Main{

    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        BigInteger[] s = new BigInteger[220];
        s[1] = new BigInteger("1");
        int i;
        for(i = 2; i < 220; i ++){
            BigInteger c = new BigInteger(((Integer)i).toString());
            s[i] = new BigInteger("1");
            s[i] = s[i-1].multiply(c);
            //System.out.println(s[i]);
        }
        int n, m, v = 0;
        while(cin.hasNext()){
            m = cin.nextInt();
            n = cin.nextInt();
            if(n == 0&&m ==0) return;
            v++;
            System.out.println("Test #"+v+":");
            if(m < n){
                System.out.println("0"); continue;
            }
            BigInteger temp1 = new BigInteger(((Integer)(m+1-n)).toString());
            BigInteger temp2 = new BigInteger(((Integer)(m+1)).toString());
            BigInteger ans = s[n+m];
            ans = ans.multiply(temp1);
            ans = ans.divide(temp2);
            System.out.println(ans);
        }
    }
}

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

时间: 2024-12-09 04:03:32

hdoj 1133 Buy the Ticket 【卡特兰】的相关文章

HDU 1133 Buy the Ticket 卡特兰数

设50元的人为+1 100元的人为-1 满足前任意k个人的和大于等于0 卡特兰数 C(n+m, m)-C(n+m, m+1)*n!*m! import java.math.*; import java.util.*; public class Main { /** * @param args */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int cas = 1; while(tru

HDU——1133 Buy the Ticket

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

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

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(卡特兰数+递推高精度)

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

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]

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();

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

Codeforces 938D Buy a Ticket (转化建图 + 最短路)

题目链接  Buy a Ticket 题意   给定一个无向图.对于每个$i$ $\in$ $[1, n]$, 求$min\left\{2d(i,j) + a_{j}\right\}$ 建立超级源点$n+1$, 对于每一条无向边$(x, y, z)$,$x$向$y$连一条长度为$2z$的边,反之亦然. 对于每个$a_{i}$, 从$i$到$n+1$连一条长度为$a_{i}$的边,反之亦然. 然后跑一边最短路即可. #include <bits/stdc++.h> using namespace