Buy the Ticket

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8838    Accepted Submission(s): 3684

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


Sample Output

Test #:

Test #:

Test #:

题目大意

n+m个人排队买票,票价为50元,其中m个人各手持一张50元钞票,n个人各手持一张100元钞票,除此之外大家身上没有任何其他的钱币,并且初始时候售票窗口没有钱,问有多少种排队的情况数能够让大家都买到票。

题目分析

题解都说是要用到卡特兰数,但是仔细想了想卡特兰数没啥用呀.....算是他的一个变种,但是公式还是要现推的。

如果m=n,那么就是一个标准的卡特兰数,但是本题m和n不相等,不能直接用卡特兰的公式。

在不考虑不同的人的情况下,假设正确的方法有Dm+n个,无法满足题意的方法有Wn+m个,那么Dm+n +Wn+m =

我们假设最早买不到票的人编号是k,他手持的是100元并且售票处没有钱,那么将前k个人的钱从50元变成100元,从100元变成50元,这时候就有m+1个人手持50元,n-1个手持100元的,也就是说Wn+m=

所以我们可以知道,Dm+n= -

所以最终的答案化简为:(m+n)! * (m+1-n)/(m+1)

需要注意的是:

  • 阶乘是个大数乘法
  • 如何处理除法

大数阶乘我之前的文章已经写过: 大数阶乘

至于如何处理除法,我们可以我们注意到,如果n!=0,那么m+1必然会在前面的阶乘中求到,跳过不乘即可,最后再乘上个(m+1-n)就行了,如果n=0,那么只需要算前面的阶乘就好了,后面的因数是1。

代码

#include<bits/stdc++.h>

using namespace std;

int anss[],m,n,cnt,temp,i,j,k,t=;

int main()
{
while(scanf("%d %d",&m,&n)&&(m!=||n!=))
{
t++;
if(m<n)
{
cout<<"Test #"<<t<<':'<<endl;
cout<<''<<endl;
continue;
}
memset(anss,,sizeof(anss));
anss[]=;
cnt=; //记录当前结果长度
for(i=;i<=n+m;i++)
{
if(i==m+)
continue;
k=; //记录进位
for(j=;j<=cnt;j++)
{
temp=(anss[j]*i+k)%;
k=(anss[j]*i+k)/;
anss[j]=temp;
}
while(k)
{
anss[++cnt]=k%;
k=k/;
}
}
if(n!=)
{
k=;
for(j=;j<=cnt;j++)
{
temp=(anss[j]*(m+-n)+k)%;
k=(anss[j]*(m+-n)+k)/;
anss[j]=temp;
}
while(k)
{
anss[++cnt]=k%;
k=k/;
}
}
cout<<"Test #"<<t<<':'<<endl;
for(i=cnt;i>=;i--)
printf("%d",anss[i]);
cout<<endl;
}
}

最新文章

  1. C++常用特性原理解析
  2. jsonp 跨域请求
  3. Executor框架(转载)
  4. Web开发中20个很有用的CSS库
  5. cocoapods安装及常用命令
  6. @proprety数组字典字符串用copy和strong区别(深浅拷贝)
  7. Android--扫描二维码
  8. jq 动态添加.active 实现导航效果
  9. canvas入门之时钟的实现
  10. Spark技术内幕:Shuffle Map Task运算结果的处理
  11. Git详解及github与gitlab使用
  12. [daily][emacs][go] 配置emacs go-mode的编辑环境以及环境变量问题
  13. typescript 与 js 开发 react 的区别
  14. GZip对字符串压缩和解压
  15. unix环境高级编程 读书笔记
  16. mongodb的学习-4-使用 MongoDB shell 来连接 Mongodb 服务
  17. [sz,rz]使用sz/rz在两台Linux设备之间传输数据
  18. STM32学习之路之入门篇
  19. bzoj2049: [Sdoi2008]Cave 洞穴勘测 lct裸题
  20. Spring第九弹—使用CGLIIB实现AOP功能与AOP概念解释

热门文章

  1. 【leetcode】1267. Count Servers that Communicate
  2. user-select 用户禁止选中
  3. mysql 查询奇偶数
  4. 报错:没有与参数列表匹配的构造函数 &quot;CFileDialog::CFileDialog&quot; 实例
  5. POJ 3061  Subsequence   尺取法   挑战146页
  6. Codeforces Round #325 (Div. 2) B. Laurenty and Shop 有规律的图 暴力枚举
  7. sh_05_列表遍历
  8. [CSP-S模拟测试]:真相(模拟)
  9. 安装OpenCV 3 on Raspbian Jessie
  10. C++入门经典-例6.5-连接字符串