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; }
  • 相关阅读:
    SQL 语句优化中间表的使用优化
    SQL 语句优化OR 语句优化案例
    浅谈系统优化设计复杂运算放在逻辑层还是在数据库层?
    linux命令综合
    MySQL常用命令
    PHP知识点积累
    [Git] 生成token解决github remote: Support for password authentication was removed on August 13, 2021.
    [uniapp] GOFLY在线客服系统 uniapp增加播放背景音效或者按钮音效
    [Golang]gorm更新数据update 解决值为0时被忽略
    光阴真的是贱(似箭),一不小心就又过一年了
  • 原文地址:https://www.cnblogs.com/syhandll/p/4453170.html
Copyright © 2011-2022 走看看