Hdoj 1133 Buy the Ticket [catlan], hdoj1133

Source: Internet
Author: User

Hdoj 1133 Buy the Ticket [catlan], hdoj1133

Q: m people (50 yuan) and n people (100 yuan) are going to buy tickets. I didn't have a ticket office at the beginning. I asked the number of types that everyone can buy at the end.

This is also a classic question of the catlan number type.

We can regard them as trains in and out of the station, but because people are different, we have to ride m at last! * N!

The final mathematical expression is (C (m + n, n)-C (m + n, m + 1) * m! * N! = The result is (m! * N !) * (M + 1-n)/(m + 1)

Note: When m is less than n, 0 is output directly.

Code:

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);        }    }}
Question link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1133




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.