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