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
  1. 3 0
  2. 3 1
  3. 3 3
  4. 0 0
 
Sample Output
  1. Test #1:
  2. 6
  3. Test #2:
  4. 18
  5. Test #3:
  6. 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!+高精乘高精
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <ctime>
  6. #include <algorithm>
  7. #include <iostream>
  8. #include <sstream>
  9. #include <string>
  10. #define oo 0x13131313
  11. using namespace std;
  12. int C[102][102][50];
  13. int len[102][102];
  14. int JC[102][200];
  15. int LEN[102];
  16. void getadd(int a,int b)
  17. {
  18. int a1=a,a2=a-1,b1=b-1,b2=b;
  19. int temp=0;
  20. len[a][b]=max(len[a1][b1],len[a2][b2])+1;
  21. for(int i=0;i<len[a][b];i++)
  22. {
  23. temp+=C[a1][b1][i]+C[a2][b2][i];
  24. C[a][b][i]=temp%10;
  25. temp=temp/10;
  26. }
  27. if(C[a][b][len[a][b]-1]==0) len[a][b]--;
  28. }
  29. void getC()
  30. {
  31. C[1][1][0]=1;
  32. len[1][1]=1;
  33. for(int i=1;i<=100;i++)
  34. {
  35. C[i][0][0]=1;
  36. len[i][0]=1;
  37. }
  38. for(int j=1;j<=100;j++)
  39. for(int i=1;i<=100;i++)
  40. {
  41. len[i][j]=1;
  42. if(j<=i)
  43. getadd(i,j);
  44. }
  45. }
  46. void getx(int X)
  47. {
  48. double k=(log((double)X)/log(10.0));
  49. int K=(int)(k+1);
  50. LEN[X]=LEN[X-1]+K;
  51. int temp=0;
  52. for(int i=0;i<LEN[X];i++)
  53. {
  54. temp+=JC[X-1][i]*X;
  55. JC[X][i]=temp%10;
  56. temp=temp/10;
  57. }
  58. if(JC[X][LEN[X]-1]==0) LEN[X]--;
  59. }
  60. void getJC()
  61. {
  62. JC[0][0]=1;LEN[0]=1;JC[1][0]=1;LEN[1]=1;
  63. for(int i=1;i<=100;i++)
  64. getx(i);
  65. }
  66. void highXhigh(int *c,int &lenc,int *a,int lena,int *b,int lenb)
  67. {
  68. lenc=lena+lenb;
  69. int temp=0;
  70. for(int i=0;i<lena;i++)
  71. for(int j=0,temp=0;j<=lenb;j++)
  72. {
  73. temp+=a[i]*b[j]+c[i+j];
  74. c[i+j]=temp%10;
  75. temp=temp/10;
  76. }
  77. if(c[lenc-1]==0) lenc--;
  78. }
  79. int buffer1[500],buffer2[500],len1,len2;
  80. void getans(int n,int m)
  81. {
  82. memset(buffer1,0,sizeof(buffer1));
  83. memset(buffer2,0,sizeof(buffer2));
  84. highXhigh(buffer1,len1,JC[n],LEN[n],C[n][m],len[n][m]);
  85. highXhigh(buffer2,len2,buffer1,len1,JC[m],LEN[m]);
  86. for(int i=len2-1;i>=0;i--)
  87. {
  88. printf("%d",buffer2[i]);
  89. }
  90. }
  91. int main()
  92. {
  93. // freopen("a.in","r",stdin);
  94. // freopen("a.out","w",stdout);
  95. getC();
  96. getJC();
  97. int Case=0,n,m;
  98. while(scanf("%d%d",&n,&m)!=EOF &&(n!=0||m!=0))
  99. {
  100. Case++;
  101. printf("Test #%d:\n",Case);
  102. if(n>=m)
  103. getans(n,m);
  104. else printf("0");
  105. printf("\n");
  106. }
  107. return 0;
  108. }

有几点要注意的


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

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

二:压位+F[m][n]+N!+高精乘高精
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <ctime>
  6. #include <algorithm>
  7. #include <iostream>
  8. #include <sstream>
  9. #include <string>
  10. #define oo 0x13131313
  11. #define B 10000
  12. using namespace std;
  13. int C[102][102][30];
  14. int len[102][102];
  15. int JC[102][50];
  16. int LEN[102];
  17. void getadd(int a,int b)
  18. {
  19. int a1=a,a2=a-1,b1=b-1,b2=b;
  20. int temp=0;
  21. len[a][b]=max(len[a1][b1],len[a2][b2])+1;
  22. for(int i=0;i<len[a][b];i++)
  23. {
  24. temp+=C[a1][b1][i]+C[a2][b2][i];
  25. C[a][b][i]=temp%B;
  26. temp=temp/B;
  27. }
  28. if(C[a][b][len[a][b]-1]==0) len[a][b]--;
  29. }
  30. void getC()
  31. {
  32. C[1][1][0]=1;
  33. len[1][1]=1;
  34. for(int i=1;i<=100;i++)
  35. {
  36. C[i][0][0]=1;
  37. len[i][0]=1;
  38. }
  39. for(int j=1;j<=100;j++)
  40. for(int i=1;i<=100;i++)
  41. {
  42. len[i][j]=1;
  43. if(j<=i)
  44. getadd(i,j);
  45. }
  46. }
  47. void getx(int X)
  48. {
  49. // double k=(log((double)X)/log(10.0));
  50. // int K=(int)(k+1);
  51. LEN[X]=LEN[X-1]+1;
  52. int temp=0;
  53. for(int i=0;i<LEN[X];i++)
  54. {
  55. temp+=JC[X-1][i]*X;
  56. JC[X][i]=temp%B;
  57. temp=temp/B;
  58. }
  59. if(JC[X][LEN[X]-1]==0) LEN[X]--;
  60. }
  61. void getJC()
  62. {
  63. JC[0][0]=1;LEN[0]=1;JC[1][0]=1;LEN[1]=1;
  64. for(int i=1;i<=100;i++)
  65. getx(i);
  66. }
  67. void highXhigh(int *c,int &lenc,int *a,int lena,int *b,int lenb)
  68. {
  69. lenc=lena+lenb;
  70. int temp=0;
  71. for(int i=0;i<lena;i++)
  72. for(int j=0,temp=0;j<=lenb;j++)
  73. {
  74. temp+=a[i]*b[j]+c[i+j];
  75. c[i+j]=temp%B;
  76. temp=temp/B;
  77. }
  78. if(c[lenc-1]==0) lenc--;
  79. }
  80. int buffer1[120],buffer2[120],len1,len2;
  81. void getans(int n,int m)
  82. {
  83. memset(buffer1,0,sizeof(buffer1));
  84. memset(buffer2,0,sizeof(buffer2));
  85. highXhigh(buffer1,len1,JC[n],LEN[n],C[n][m],len[n][m]);
  86. highXhigh(buffer2,len2,buffer1,len1,JC[m],LEN[m]);
  87. if(len2-1>=0) printf("%d",buffer2[len2-1]);
  88. for(int i=len2-2;i>=0;i--)
  89. {
  90. printf("%04d",buffer2[i]);
  91. }
  92. }
  93. int main()
  94. {
  95. freopen("a.in","r",stdin);
  96. freopen("b.out","w",stdout);
  97. getC();
  98. getJC();
  99. int Case=0,n,m;
  100. while(scanf("%d%d",&n,&m)!=EOF &&(n!=0||m!=0))
  101. {
  102. Case++;
  103. printf("Test #%d:\n",Case);
  104. if(n>=m)
  105. getans(n,m);
  106. else printf("0");
  107. printf("\n");
  108. }
  109. return 0;
  110. }

改压位十分简单


把预处理的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. Cts框架解析(8)-IBuildProvider

    IBuildProvider接口中定义了三个方法 /* * Copyright (C) 2010 The Android Open Source Project * * Licensed under ...

  2. [Python学习笔记][第七章Python文件操作]

    2016/1/30学习内容 第七章 Python文件操作 文本文件 文本文件存储的是常规字符串,通常每行以换行符'\n'结尾. 二进制文件 二进制文件把对象内容以字节串(bytes)进行存储,无法用笔 ...

  3. Https协议简析及中间人攻击原理

    1.基础知识 1.1 对称加密算法 对称加密算法的特点是加密密钥和解密密钥是同一把密钥K,且加解密速度快,典型的对称加密算法有DES.AES等                              ...

  4. jsp页面获取服务器时间

    Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MON ...

  5. mysql事件调度器

    #查看mysql事件调度器是否开启 SHOW VARIABLES WHERE Variable_name = 'event_scheduler'; #开启mysql事件调度器功能 SET GLOBAL ...

  6. Codeforces Round #277.5 (Div. 2) A,B,C,D,E,F题解

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud A. SwapSort time limit per test    1 seco ...

  7. (原+转)Ubuntu下安装understand及在启动器中增加快捷方式

    参考网址: http://www.xuebuyuan.com/1353431.html http://www.2cto.com/os/201309/242543.html http://my.osch ...

  8. yii2 打印sql语句

    echo $temp_chat_query->createCommand()->getRawSql();

  9. VM11里安装ubuntukylin-16.04-desktop-amd64遇到问题

    一.ubuntu linux的地址 http://www.ubuntu-china.cn/ 这个地址是中国站,点击下载菜单后,有两个版本,一个是ubuntu,一个是kylin.后者是专门加了中文程序的 ...

  10. Array类型(二)

    1.concat()方法可以基于当前数组中的所有项创建一个新数组. 先创建当前数组的一个副本,然后将接收到的参数添加到这个副本的末尾,最后返回新构建的数组. var colors = ["r ...