【高精度练习+卡特兰数】【Uva1133】Buy the Ticket
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
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.
3 0
3 1
3 3
0 0
Test #1:
6
Test #2:
18
Test #3:
180
#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;
}
有几点要注意的
#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;
}
改压位十分简单
【高精度练习+卡特兰数】【Uva1133】Buy the Ticket的更多相关文章
- Contset Hunter 1102 高精度求卡特兰数
用递推的方式写的写挂了,而如果用组合数又不会高精度除法,偶然看到了别人的只用高精度乘低精度求组合数的方法,记录一下 #include<bits/stdc++.h> using namesp ...
- HDU 1023 Traning Problem (2) 高精度卡特兰数
Train Problem II Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Sub ...
- Buy the Ticket(卡特兰数+递推高精度)
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- 【HDU 1133】 Buy the Ticket (卡特兰数)
Buy the Ticket Problem Description The "Harry Potter and the Goblet of Fire" will be on sh ...
- HDUOJ---1133(卡特兰数扩展)Buy the Ticket
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU1133 Buy the Ticket —— 卡特兰数
题目链接:https://vjudge.net/problem/HDU-1133 Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Me ...
- HDOJ/HDU 1133 Buy the Ticket(数论~卡特兰数~大数~)
Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...
- Buy the Ticket HDU 1133 卡特兰数应用+Java大数
Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...
- 【hdoj_1133】Buy the Ticket(卡特兰数+大数)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1133 题目的意思是,m个人只有50元钱,n个人只有100元整钱,票价50元/人.现在售票厅没钱,只有50元 ...
随机推荐
- VC调试笔记
1.windows-32调试: ①使用map文件根据崩溃地址寻找对应的源代码文件和行号 勾选project->settings->link->General mapfile,对应的P ...
- phonegap环境配置与基本操作
一.开发环境配置: 1.工具环境安装: 安装java sdk 1.6以上版本号,Android Development Tools.ant,系统变量 Path后面加入 新增名稱 JAVA_HOME 值 ...
- Python 线程(threading) 进程(multiprocessing)
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- ajax局部刷新分页
//请求数据加载绑定页面 function DindAjax(pageIndex) {//获取参数 var colors = $("#colorsVal").val(); $.aj ...
- 不同浏览器对URL最大长度的限制(转)
1.今天碰到一个bug,window.open后面的页面,接收参数不全,导致后台报错.实验了一下.发现是使用get方法请求服务器时,URL过长所致 微软官方的说明: http://support.mi ...
- Ext Radio 取消选中
今天,做项目的时候遇到了要吧Ext Radio单选按钮取消选中状态,由于没有在formpanel中写, 导致不能用reset()方法,试了各种方法,最后这样写管用. radio1.setValue(f ...
- Cisco cmd 命令
1.enable 开启全局配置模式:disable 禁用配置模式 2.config进入配置模式 3.line 设置进入用户模式密码:分为 line aux 0;line console 0;line ...
- A Bit Of Knowledge
iOS推崇使用png格式的图片,说这样不会失帧 imageNamed 和 imageWithContentOfFile的区别 imageNamed会使用系统缓存,对重复加载的图片速度会快一些,效果好. ...
- 简单实用的HTML代码
简单实用的HTML代码 一.HTML各种命令的代码: 1.文本标签(命令) <pre></pre> 创建预格式化文本 <h1></h1> 创建最大的标题 ...
- js的原型继承小结
考虑:有一个想要复用的对象,并且想要创建的第二个对象需要从第一个对对象中获取其功能. 实现如下: //要继承的对象 var parent = { name:"Papa" }; // ...