Buy the Ticket

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4726    Accepted Submission(s): 1993

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
 
Author
HUANG, Ninghai
 
Recommend
Eddy   |   We have carefully selected several similar problems for you:  1267 1297 2067 1023 1143 
 
当然这是一道卡特兰数的变式题
递推公式很容易得出
F[i][j]=F[i-1][j]+F[i][j-1];(当i<j时 F[i][j]=0)

最终结果是 F[m][n]*m!*n! 显然涉及高精度

高精度是这题的主角

一:不压位+F[m][n]+N!+高精乘高精
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
using namespace std;
int C[102][102][50];
int len[102][102];
int JC[102][200];
int LEN[102];
void getadd(int a,int b)
{
int a1=a,a2=a-1,b1=b-1,b2=b;
int temp=0;
len[a][b]=max(len[a1][b1],len[a2][b2])+1;
for(int i=0;i<len[a][b];i++)
{
temp+=C[a1][b1][i]+C[a2][b2][i];
C[a][b][i]=temp%10;
temp=temp/10;
}
if(C[a][b][len[a][b]-1]==0) len[a][b]--;
}
void getC()
{
C[1][1][0]=1;
len[1][1]=1;
for(int i=1;i<=100;i++)
{
C[i][0][0]=1;
len[i][0]=1;
}
for(int j=1;j<=100;j++)
for(int i=1;i<=100;i++)
{
len[i][j]=1;
if(j<=i)
getadd(i,j);
}
}
void getx(int X)
{
double k=(log((double)X)/log(10.0));
int K=(int)(k+1);
LEN[X]=LEN[X-1]+K;
int temp=0;
for(int i=0;i<LEN[X];i++)
{
temp+=JC[X-1][i]*X;
JC[X][i]=temp%10;
temp=temp/10;
}
if(JC[X][LEN[X]-1]==0) LEN[X]--;
}
void getJC()
{
JC[0][0]=1;LEN[0]=1;JC[1][0]=1;LEN[1]=1;
for(int i=1;i<=100;i++)
getx(i);
}
void highXhigh(int *c,int &lenc,int *a,int lena,int *b,int lenb)
{
lenc=lena+lenb;
int temp=0;
for(int i=0;i<lena;i++)
for(int j=0,temp=0;j<=lenb;j++)
{
temp+=a[i]*b[j]+c[i+j];
c[i+j]=temp%10;
temp=temp/10;
}
if(c[lenc-1]==0) lenc--;
}
int buffer1[500],buffer2[500],len1,len2;
void getans(int n,int m)
{
memset(buffer1,0,sizeof(buffer1));
memset(buffer2,0,sizeof(buffer2));
highXhigh(buffer1,len1,JC[n],LEN[n],C[n][m],len[n][m]);
highXhigh(buffer2,len2,buffer1,len1,JC[m],LEN[m]);
for(int i=len2-1;i>=0;i--)
{
printf("%d",buffer2[i]);
}
}
int main()
{
// freopen("a.in","r",stdin);
// freopen("a.out","w",stdout);
getC();
getJC();
int Case=0,n,m;
while(scanf("%d%d",&n,&m)!=EOF &&(n!=0||m!=0))
{
Case++;
printf("Test #%d:\n",Case);
if(n>=m)
getans(n,m);
else printf("0");
printf("\n");
}
return 0;
}

有几点要注意的


#71   j<=lenb 以及所有的估计长度都比实际

可能多一位的原因是保证 temp 最后为0;

二:压位+F[m][n]+N!+高精乘高精
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
#define B 10000
using namespace std;
int C[102][102][30];
int len[102][102];
int JC[102][50];
int LEN[102];
void getadd(int a,int b)
{
int a1=a,a2=a-1,b1=b-1,b2=b;
int temp=0;
len[a][b]=max(len[a1][b1],len[a2][b2])+1;
for(int i=0;i<len[a][b];i++)
{
temp+=C[a1][b1][i]+C[a2][b2][i];
C[a][b][i]=temp%B;
temp=temp/B;
}
if(C[a][b][len[a][b]-1]==0) len[a][b]--;
}
void getC()
{
C[1][1][0]=1;
len[1][1]=1;
for(int i=1;i<=100;i++)
{
C[i][0][0]=1;
len[i][0]=1;
}
for(int j=1;j<=100;j++)
for(int i=1;i<=100;i++)
{
len[i][j]=1;
if(j<=i)
getadd(i,j);
}
}
void getx(int X)
{
// double k=(log((double)X)/log(10.0));
// int K=(int)(k+1);
LEN[X]=LEN[X-1]+1;
int temp=0;
for(int i=0;i<LEN[X];i++)
{
temp+=JC[X-1][i]*X;
JC[X][i]=temp%B;
temp=temp/B;
}
if(JC[X][LEN[X]-1]==0) LEN[X]--;
}
void getJC()
{
JC[0][0]=1;LEN[0]=1;JC[1][0]=1;LEN[1]=1;
for(int i=1;i<=100;i++)
getx(i);
}
void highXhigh(int *c,int &lenc,int *a,int lena,int *b,int lenb)
{
lenc=lena+lenb;
int temp=0;
for(int i=0;i<lena;i++)
for(int j=0,temp=0;j<=lenb;j++)
{
temp+=a[i]*b[j]+c[i+j];
c[i+j]=temp%B;
temp=temp/B;
}
if(c[lenc-1]==0) lenc--;
}
int buffer1[120],buffer2[120],len1,len2;
void getans(int n,int m)
{
memset(buffer1,0,sizeof(buffer1));
memset(buffer2,0,sizeof(buffer2));
highXhigh(buffer1,len1,JC[n],LEN[n],C[n][m],len[n][m]);
highXhigh(buffer2,len2,buffer1,len1,JC[m],LEN[m]);
if(len2-1>=0) printf("%d",buffer2[len2-1]);
for(int i=len2-2;i>=0;i--)
{
printf("%04d",buffer2[i]);
}
}
int main()
{
freopen("a.in","r",stdin);
freopen("b.out","w",stdout);
getC();
getJC();
int Case=0,n,m;
while(scanf("%d%d",&n,&m)!=EOF &&(n!=0||m!=0))
{
Case++;
printf("Test #%d:\n",Case);
if(n>=m)
getans(n,m);
else printf("0");
printf("\n");
}
return 0;
}

改压位十分简单


把预处理的define B 改一下就好了

随便压几位都随便改

不过要注意的是

乘以非高精数的时候注意计算他的长度/4


【高精度练习+卡特兰数】【Uva1133】Buy the Ticket的更多相关文章

  1. Contset Hunter 1102 高精度求卡特兰数

    用递推的方式写的写挂了,而如果用组合数又不会高精度除法,偶然看到了别人的只用高精度乘低精度求组合数的方法,记录一下 #include<bits/stdc++.h> using namesp ...

  2. HDU 1023 Traning Problem (2) 高精度卡特兰数

    Train Problem II Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Sub ...

  3. Buy the Ticket(卡特兰数+递推高精度)

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

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

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

  5. HDUOJ---1133(卡特兰数扩展)Buy the Ticket

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

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

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

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

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

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

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

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

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

随机推荐

  1. [置顶] 正则表达式应用:匹配IP地址

    都知道iP地址有四个数值,三个点号组成.三个数值的具体范围为0到255,为了使用正则表达式匹配就必须分析IP地址的组成 1先分析数值,2再组合数值和点号 1先分析数值 IP地址的数字范围从0到255, ...

  2. 给一个int型整数,如何将这个整数的奇偶位互换

    题目: 假设一个8为整数是(10101100)b那么奇偶互换之后就是(01011100)b.假设机器是32位的 注意: 8位中最低位开始数,最低位是第0位,是偶数为,次低位时第1位,是偶数位. 做法: ...

  3. EffectiveC#6--区别值类型数据和引用类型数据

    1. 设计一个类型时,选择struct或者class是件简单的小事情,但是,一但你的类型发生了改变, 对所有使用了该类型的用户进行更新却要付出(比设计时)多得多的工作. 2.值类型:无多态但性能佳. ...

  4. HTML与CSS入门——第一章 理解Web的工作方式

    知识点: 1.万维网的简史 2."网页"的含义,以及该术语不能反映所涉及的所有内容的原因 3.如何从你的个人计算机进入别人的浏览器 4.选择Web托管提供商的方法 5.不同的Web ...

  5. Android——ExpandableListView事件拦截

    1.满足条件 如果使用ExpandableListView,需要子item响应一个事件,比如重新启动一个新的activity,需要满足下面的条件: (1).修改Adapter返回值 覆写BaseExp ...

  6. 混合高斯模型和EM算法

    这篇讨论使用期望最大化算法(Expectation-Maximization)来进行密度估计(density estimation). 与k-means一样,给定的训练样本是,我们将隐含类别标签用表示 ...

  7. gradle构建依赖

    本地依赖 gradle 作为构建工具,能够很方便的使用本地jar包,以下为使用的代码块. dependencies { //单文件依赖 compile files('libs/android-supp ...

  8. HTML5 canvas 在线画笔绘图工具(一)

    HTML5 canvas 在线画笔绘图工具(一) 功能介绍 这是我用Javascript写的第一个程序,在写的过程中走了很多弯路,所以写完之后想分享出来,给与我一样的初学者做为学习的参考,同时在编写这 ...

  9. html中的特殊符号表示法

    html中的特殊符号 符号 说明 编码   符号 说明 编码   符号 说明 编码 " 双引号 " × 乘号 × ← 向左箭头 ← & AND符号 & ÷ 除号 ÷ ...

  10. Clojure操作mysql

    在Eclipse中新建一个Clojure工程clj01 clojure 操作mysql需要依赖mysql-connector-java.clojure-contrib与java.jdbc三个jar包. ...