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;
}
}

HDU 1133 Buy the Ticket (数学、大数阶乘)的更多相关文章

  1. hdu 1133 Buy the Ticket (大数+递推)

    Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  2. hdu 1133 Buy the Ticket(Catalan)

    Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  3. HDU——1133 Buy the Ticket

    Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  4. HDOJ/HDU 1133 Buy the Ticket(数论~卡特兰数~大数~)

    Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...

  5. hdu 1133 Buy the Ticket

    首先,记50的为0,100的为1. 当m=4,n=3时,其中的非法序列有0110010; 从不合法的1后面开始,0->1,1->0,得到序列式0111101 也就是说,非法序列变为了n-1 ...

  6. HDU 1133 Buy the Ticket 卡特兰数

    设50元的人为+1 100元的人为-1 满足前随意k个人的和大于等于0 卡特兰数 C(n+m, m)-C(n+m, m+1)*n!*m! import java.math.*; import java ...

  7. Buy the Ticket HDU 1133 卡特兰数应用+Java大数

    Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...

  8. HDU 1060 Leftmost Digit (数学/大数)

    Leftmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  9. 【HDU 1133】 Buy the Ticket (卡特兰数)

    Buy the Ticket Problem Description The "Harry Potter and the Goblet of Fire" will be on sh ...

随机推荐

  1. Unity3D资源

    1.ShareSDK 地址:https://github.com/MobClub/New-Unity-For-ShareSDK 文档:产品集成步骤 2.Protobuf https://github. ...

  2. @Data注解使用后get set报错解决方法

    Maven项目中已经导入相关的lombok.jar包但是使用后仍提示无set/get方法 .在idea中安装如下插件,安装后重启idea可用不报错. 转载于:https://www.cnblogs.c ...

  3. [Linux系统] (8)Nginx

    一.高并发基础架构 简要流程: 1.客户端发请求. 2.又LVS等四层负载均衡系统将请求转发给不同的Nginx服务器. 3.Nginx与客户端建立TCP连接,拿到请求后分析URI,然后将其转发给对应的 ...

  4. tp5商城

    记录几个要点: 用户使用firbug伪造表单字段,比如伪造表单id字段,如何防止,tp5中好像没有. xss攻击:使用htmlspecialchars() 会把img.p.等等合法标签过滤掉,想要有选 ...

  5. linux中more命令如何使用

         more命令,功能类似 cat ,cat命令是整个文件的内容从上到下显示在屏幕上.兄弟连Linux培训教程() more会以一页一页的显示方便使用者逐页阅读,而最基本的指令就是按空白键(sp ...

  6. poj 3662 Telephone Lines dijkstra+二分搜索

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5696   Accepted: 2071 D ...

  7. 成功解决internal/modules/cjs/loader.js:596 throw err; ^ Error: Cannot find module 'minimatch'

    成功解决internal/modules/cjs/loader.js:596 throw err; ^ Error: Cannot find module 'minimatch' 解决办法 npm i ...

  8. Codeforces 798D Mike and distribution (构造)

    题目链接 http://codeforces.com/contest/798/problem/D 题解 前几天的模拟赛,居然出这种智商题..被打爆了QAQ 这个的话,考虑只有一个序列怎么做,把所有的排 ...

  9. [BZOJ5249][九省联考2018]IIIDX:线段树+贪心

    分析 GXZlegend orz 构造出一组合法的解并不是难事,但是我们需要输出的是字典序最大的解. 字典序最大有另一种理解方式,就是让越小的数尽量越靠后. 我们从树的根结点出发,从1开始填数,构造出 ...

  10. spring注解版

    第一.spring框架快速入门 1.1什么是spring 框架 Spring 框架是 Java 应用最广的框架,它的成功来源于理念,而不是技术本身,它的理念包括 IoC (Inversion of C ...