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
 
 
【题意】
  
  有m个人为1,n个人为-1,他们各不同,求他们排成一列且从第一个人到任意人的权值和不能为负数的方案数(n,m<=100)
 
【分析】
  

  当m<n,显然一定不合法。所以我们考虑m>=n,类比n=m时的方案数推法,总方案为C(m,m+n),要减去不符合的情况。

  我们扫描一个数,找到第一个不符合的位置,假设是有a个1,a+1个1,那么我们后面会填n-a-1个-1,m-a个1,我们把后面的1和-1交换,就得到了一个有m-a个-1,n-a-1个1的数,方案为C(m+1,m+n),把它减掉即可,

  即ans=C(m,m+n)-C(m+1,m+n)。因为每个人不相同最后1还要乘n!*m!。

  为什么要用1和-1互换呢,首先对于一个不合法串是唯一对应一个转换串的(就按照上述方法转换),

  然后对于一个转换串是一定对应一个不合法串的,方法是找到其第一个不合法的地方,然后后面部分-1和1转换。

  还有就是转换串一定不合法,因为它有m+1个-1,n-1个1,而m>=n。这就是他比原串优越的地方,用他可以完全取代不合法串!

  - -高精乘法。

代码如下:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define Maxn 1100 struct node
{
int d[Maxn],ln;
};
node t[]; void mul(int x,int y)
{
int add=;
for(int i=;i<=t[x].ln;i++)
{
t[x].d[i]*=y;
t[x].d[i]+=add;
add=t[x].d[i]/;
t[x].d[i]%=;
// if(i==t[x].ln&&t[x].d[i+1]!=0) t[x].ln++;
}
t[x].d[t[x].ln+]=add;
while(t[x].d[t[x].ln+]!=)
{
t[x].ln++;
t[x].d[t[x].ln+]=t[x].d[t[x].ln]/;
t[x].d[t[x].ln]%=;
}
} void get_ans()
{
t[].ln=t[].ln;
for(int i=;i<=t[].ln;i++)
{
if(t[].d[i]<t[].d[i])
{
t[].d[i+]--;
t[].d[i]+=;
}
t[].d[i]=t[].d[i]-t[].d[i];
}
while(t[].d[t[].ln]==) t[].ln--;
if(t[].ln==) t[].ln++;
} int main()
{
int kase=;
while()
{
int m,n;
scanf("%d%d",&m,&n);
if(m==&&n==) break;
printf("Test #%d:\n",++kase);
if(m<n) {printf("0\n");continue;}
memset(t[].d,,sizeof(t[].d));
memset(t[].d,,sizeof(t[].d));
t[].d[]=; t[].ln=;
for(int i=;i<=m+n;i++)
{
mul(,i);
// for(int i=t[0].ln;i>=1;i--) printf("%d",t[0].d[i]);printf("\n");
}
t[].d[]=; t[].ln=;
for(int i=;i<=m;i++)
{
mul(,i);
}
for(int i=m+;i<=m+n;i++)
{
mul(,i);
}
mul(,n);
get_ans();
for(int i=t[].ln;i>=;i--) printf("%d",t[].d[i]);
printf("\n");
}
return ;
}

[HDU 1133]

2016-09-20 18:41:32

【HDU 1133】 Buy the Ticket (卡特兰数)的更多相关文章

  1. 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 ...

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

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

  3. 【hdoj_1133】Buy the Ticket(卡特兰数+大数)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1133 题目的意思是,m个人只有50元钱,n个人只有100元整钱,票价50元/人.现在售票厅没钱,只有50元 ...

  4. HDU 1133 Buy the Ticket (数学、大数阶乘)

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

  5. hdu 1133 Buy the Ticket(Catalan)

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

  6. HDU1133 Buy the Ticket —— 卡特兰数

    题目链接:https://vjudge.net/problem/HDU-1133 Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Me ...

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

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

  8. HDU——1133 Buy the Ticket

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

  9. hdu 1133 Buy the Ticket

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

  10. HDU 1023 Train Problem II (卡特兰数,经典)

    题意: 给出一个数字n,假设火车从1~n的顺序分别进站,求有多少种出站序列. 思路: 卡特兰数的经典例子.n<101,用递推式解决.需要使用到大数.n=100时大概有200位以下. #inclu ...

随机推荐

  1. display:none和visibility:hidden的区别[]

    display:none和visibility:hidden都是把网页上某个元素隐藏起来的功能,但两者有所区别,我发现使用 visibility:hidden属性会使对象不可见,但该对象在网页所占的空 ...

  2. 替换SQL Server数据库中所有表的所有字段的某些内容

    declare @t varchar(255),@c varchar(255)  declare table_cursor cursor for select a.name,b.name  from  ...

  3. mvc给html扩展方法:

    mvc给html扩展方法: 注意:扩展方法和所在的类都必须是 public static如果在页面直接使用新扩展的方法,需要web.config里把Web.Helper名称命名空间加上,页面才能访问到 ...

  4. DLL使用总结

    最近项目中使用到了DLL,因此就把最近一段时间的学习总结一下,以备不时之需. 一.相关概念 1.动态链接库 自从微软推出第一个版本的Windows操作系统以来,动态链接库(DLL)一直是Windows ...

  5. 500 OOPS: cannot change directory:/home/test

    问题:  以root   从远程客户端 登录 FTP  一直密码错误.  发现不能以root 登录, 需要创建其它的用户. 创建一个test 用户后(如下): useradd test; passwd ...

  6. [Twisted] Test

    由于Twisted程序采用事件驱动,并使用Deferred来处理事件,使用Python unittest的写测试并不容易.因此, Twisted拓展了unitest,并使用命令行工具来运行测试.这些组 ...

  7. 如何在Angular2中使用jquery

    首先在index.html中引入jquery文件 <script src="http://cdn.bootcss.com/jquery/2.1.3/jquery.js"> ...

  8. 关于Spring AOP和IOC的一些总结

    Spring官方网站:https://spring.io/ 最早对象的创建是有new关键字,但是如果产生的类比较繁多或者复杂,就用工厂代替new关键字,但是工厂的控制能力有限,譬如对产生对象的生命周期 ...

  9. QT5新手上路(1)安装

    这几天学了一下windows下的QT,也不算什么心得吧,就是谈一下我的做法.希望看到这篇随笔的菜鸟们略有所得,少走弯路. 闲话少说,先说安装.首先是选版本,我用的是qt-opensource-wind ...

  10. ASP.NET中的Request、Response、Server对象

    Request对象 Response.Write(Request.ApplicationPath) //应用根路径 Request.AppRelativeCurrentExecutionFilePat ...