zoukankan      html  css  js  c++  java
  • 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 the price for per-ticket is 50 dollars. The queue for buying the tickets is consisted of m + n persons (m persons each only has the 50-dollar bill and n persons each only has the 100-dollar bill).

    Now the problem for you is to calculate the number of different ways of the queue that 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 will be stopped on the occasion that the ticket-office has no 50-dollar bill but the first person of the queue only has the 100-dollar bill.
     
    Input
    The input file contains several test cases. Each test case is made up of two integer 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) in one line, 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
    公式:(C(m+n, n)-C(m+n, m+1)) * m! * n! 化简即 (m+n)! * (m-n+1) / (m+1)
    由公式:C(m, n) = m! / (n!*(m-n)!)推导得来
    (C(m+n, n)-C(m+n, m+1)) * m! * n!:总共的排列减去违法的排列,又因为每个人不一样再乘以本身的全排列
    #include
    <stdio.h> #include<string.h> # define N 1010 int a[N], m, n; void Fact() { int i, j, s, r = 0; for (i = 2; i <= (m+n); i++) { for (j = 0; j < N; j++) { s = a[j] * i + r; r = s / 10; a[j] = s % 10; } } } //求出(m+n)! void Sum() { int i, j, c = m-n+1, d = m+1, b[N], k = 0, s, r = 0; for (i = 0; i < N; i++) a[i] *= c; for (i = 0; i < N-1; i++) { if (a[i] > 9) { a[i+1] += a[i] / 10; a[i] %= 10; } } //求出(m+n)! * (m-n+1) i = N-1; while (a[i] == 0) i--; for (j = i; j >= 0; j--) b[k++] = a[j]; for (i = 0; i < k; i++) { s = b[i] + 10*r; r = s % d; b[i] = s / d; } //求出(m+n)! * (m-n+1) / (m+1) i = 0; while (b[i] == 0) i++; for (j = i; j < k; j++) printf("%d", b[j]); printf(" "); } int main () { int k = 0; while (scanf("%d %d", &m, &n), m+n) { k++; printf("Test #%d: ", k); if (m < n) printf("0 "); //要是拿100的人比拿50的人还多的话,那么不存在这种排法 else { memset(a, 0, sizeof(a)); a[0] = 1; Fact(); Sum(); } } return 0; }
  • 相关阅读:
    软件工程第三次个人作业——最大连续子数组和
    LeetCode(258.各位相加)的思路及解决过程
    VS2017中对C++的单元测试
    对大学三年学习生活的总结与反思
    软件工程 实验四 代码评审
    软件工程 实验二
    2020软件工程最后一次作业
    软件工程第四次作业(第二次结对)
    软件工程第三次作业
    软件工程第二次作业:最大字段和
  • 原文地址:https://www.cnblogs.com/syhandll/p/4453170.html
Copyright © 2011-2022 走看看