Hdoj/hdu 1133 Buy The Ticket (number theory ~ Cattleya ~ large number ~)

Source: Internet
Author: User

Problem Description
The "Harry Potter and the Goblet of Fire" would be on show in the next few days. As a crazy fan of Harry Potter, you'll go to the cinema and has the first sight, won ' t you?

Suppose the cinema only have one ticket-office and the price for per-ticket are dollars. The queue for buying the tickets are consisted of M + N persons (m persons each have the 50-dollar bill and N persons E Ach only has the 100-dollar bill).

Now, the problem for are to calculate the number of different ways of the "the" and 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 is stopped on the occasion that the Ticket-office have no 50-dollar bill but the first person of th E queue only had the 100-dollar bill.

Input
The input file contains several test cases. Each test case was made up of 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) and then output the number of different ways in Another line.

Sample Input
3 0
3 1
3 3
0 0

Sample Output
Test #1:
6
Test #2:
18
Test #3:
180

Test instructions

is m+n individual to buy tickets, the price of 50 yuan, M personal only with 50 yuan a note, n individuals with only 100 yuan a note, the conductor began to have no money in hand.
Ask, how to let the conductor in the case can be all the change, arrange these people buy tickets order ~

In other words: to 100 yuan people before the ticket, the conductor must have a 50 yuan in hand.

The derivation process is as follows:
M personal take 50,n 1001,
If n > M, then the number of sorting methods is 0, it is easy to think clearly
2, now we assume that 50 of the people with ' 0 ', take 100 of the people with ' 1 '. If there is such a sequence 0101101001001111.
When the number of 1 in the K position is more than 0, it is an illegal sequence. Suppose that a sequence of m=4,n=3 is: 0110100.
This means that any illegal sequence (m 0,n 1) can be obtained by another sequence (n-1 0 and m+1 1).
In addition, we know that a sequence is either legal or illegal.
Therefore, the number of legal sequences = total number of sequences-the total amount of an illegal sequence.
The total number of sequences can be calculated like this, m+n position, select n position to fill 1, so is C (m+n,n).
The number of illegal sequences is: m+n a position, select m+1 position to fill 1, so is C (m+n,m+1). And then everyone is not the same, so you need to arrange the whole m! * n!.

So the final formula is: (C (M+n,n)-C (m+n,m+1)) * m! * n! Simplification is: (m+n)!* (m-n+1)/(m+1)

If you do not understand: You can refer to my previous analysis of Cattleya number:
http://blog.csdn.net/qq_26525215/article/details/51453493

ImportJava.math.BigInteger;ImportJava.util.Scanner;/** * @author Chen Haoxiang * * 2016-5-19 * * Public  class Main{     Public Static void Main(string[] args) {Scanner SC =NewScanner (system.in);intt=0; while(Sc.hasnext ()) {intM =sc.nextint ();intn =sc.nextint ();if(n==0&&m==0){return; } System.out.println ("Test #"+ (++T) +":");if(n>m) {System.out.println (0);Continue; } BigInteger A =NewBigInteger ("1"); for(intI=2; i<=m+n;i++) {a=a.multiply (NewBigInteger (""+i)); } a=a.multiply (NewBigInteger (""+ (m-n+1))); A=a.divide (NewBigInteger (""+ (m+1)));        System.out.println (a); }    }}

Hdoj/hdu 1133 Buy The Ticket (number theory ~ Cattleya ~ large number ~)

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.