Buy the Ticket{HDU1133}
Buy the Ticket
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6517 Accepted Submission(s): 2720
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
f[i][j]表示有i个50j个100的方法数。当i<j时,f[i][j]=0。当i=j时,f[i][j]=f[i][j-1]。当i>j时,f[i][j]=f[i-1][j]+f[i][j-1]。
最后分别再乘上n和m的阶乘。
#include <iostream>
#include <string>
using namespace std;
#ifndef BIGNUM
#define BIGNUM
class BigNum {
#define MAXSIZEOFBIGNUM 500
#define BASE 10
#define DLEN 1
public:
int Len;
int d[MAXSIZEOFBIGNUM];
public:
BigNum(void);
BigNum(const int);
BigNum(const char *);
BigNum(const BigNum &);
BigNum & operator = (const BigNum &);
void clear(void);
friend istream& operator>>(istream&, BigNum&);
friend ostream& operator<<(ostream&, BigNum&);
bool operator == (const BigNum &) const;
bool operator > (const BigNum &) const;
bool operator < (const BigNum &) const;
bool operator >= (const BigNum &) const;
bool operator <= (const BigNum &) const;
BigNum operator + (const BigNum &) const;
BigNum operator - (const BigNum &) const;
BigNum operator * (const BigNum &) const;
BigNum operator / (const BigNum &) const;
BigNum operator % (const BigNum &) const;
void operator ++ (void);
void operator -- (void);
BigNum operator + (const int &) const;
BigNum operator - (const int &) const;
BigNum operator * (const int &) const;
BigNum operator / (const int &) const;
int operator % (const int &) const;
BigNum operator ^ (const int &) const;
~BigNum() {}
};
BigNum::BigNum() {
Len = ;
memset(d, , sizeof(d));
}
BigNum::BigNum(const int ops) {
int x = ops;
Len = ;
memset(d, , sizeof(d));
while(x) {
Len++;
d[Len] = x % BASE;
x /= BASE;
}
}
BigNum::BigNum(const char * ops) {
int L = strlen(ops) - , b = ;
memset(d, , sizeof(d));
while(ops[b] == '') {
b++;
}
Len = ;
while(L - b + >= DLEN) {
int x = ;
for(int i = L - DLEN + ; i <= L; i++) {
x = x * + ops[i] - '';
}
Len++;
d[Len] = x;
L -= DLEN;
}
int x = ;
for(int i = b; i <= L; i++) {
x = x * + ops[i] - '';
}
Len++;
d[Len] = x;
}
BigNum::BigNum(const BigNum &ops) : Len(ops.Len) {
memset(d, , sizeof(d));
for(int i = ; i <= Len; i++) {
d[i] = ops.d[i];
}
}
BigNum & BigNum::operator = (const BigNum &ops) {
memset(d, , sizeof(d));
Len = ops.Len;
for(int i = ; i <= Len; i++) {
d[i] = ops.d[i];
}
return *this;
}
void BigNum::clear(void) {
for(int i = ; i <= MAXSIZEOFBIGNUM - ; i++) {
if(d[i] < ) {
d[i] += BASE;
d[i + ]--;
}
if(d[i] >= BASE) {
d[i] -= BASE;
d[i + ]++;
}
}
for(int i = MAXSIZEOFBIGNUM - ; i >= ; i--)
if(d[i] > ) {
Len = i;
return;
}
Len = ;
}
istream& operator>>(istream &in, BigNum &ops) {
char str[MAXSIZEOFBIGNUM + ];
in >> str;
int L = strlen(str), b = ;
while(str[b] == '') {
b++;
}
ops.Len = ;
for(int i = L - ; i >= b; i--) {
ops.Len++;
ops.d[ops.Len] = str[i] - '';
}
return in;
}
ostream& operator<<(ostream& out, BigNum& ops) {
for(int i = ops.Len; i >= ; i--) {
out << ops.d[i];
}
if(ops.Len == ) {
out << "";
}
return out;
}
bool BigNum::operator == (const BigNum &ops) const {
if(Len != ops.Len) {
return false;
}
for(int i = Len; i >= ; i--)
if(d[i] != ops.d[i]) {
return false;
}
return true;
}
bool BigNum::operator > (const BigNum &ops) const {
if(Len < ops.Len) {
return false;
} else if(Len > ops.Len) {
return true;
} else {
for(int i = Len; i >= ; i--)
if(d[i] < ops.d[i]) {
return false;
} else if(d[i] > ops.d[i]) {
return true;
}
}
return false;
}
bool BigNum::operator < (const BigNum &ops) const {
if(Len < ops.Len) {
return true;
} else if(Len > ops.Len) {
return false;
} else {
for(int i = Len; i >= ; i--)
if(d[i] < ops.d[i]) {
return true;
} else if(d[i] > ops.d[i]) {
return false;
}
}
return false;
}
bool BigNum::operator >= (const BigNum &ops) const {
if(Len < ops.Len) {
return false;
} else if(Len > ops.Len) {
return true;
} else {
for(int i = Len; i >= ; i--)
if(d[i] < ops.d[i]) {
return false;
} else if(d[i] > ops.d[i]) {
return true;
}
}
return true;
}
bool BigNum::operator <= (const BigNum &ops) const {
if(Len < ops.Len) {
return true;
} else if(Len > ops.Len) {
return false;
} else {
for(int i = Len; i >= ; i--)
if(d[i] < ops.d[i]) {
return true;
} else if(d[i] > ops.d[i]) {
return false;
}
}
return true;
}
BigNum BigNum::operator + (const BigNum &ops) const {
BigNum ret(*this);
for(int i = ; i <= ops.Len; i++) {
ret.d[i] += ops.d[i];
}
ret.clear();
return ret;
}
BigNum BigNum::operator - (const BigNum &ops) const {
BigNum ret(*this);
for(int i = ops.Len; i >= ; i--) {
ret.d[i] -= ops.d[i];
}
ret.clear();
return ret;
}
BigNum BigNum::operator * (const BigNum &ops) const {
BigNum ret, now(*this);
for(int i = ; i <= now.Len; i++)
for(int j = ; j <= ops.Len; j++) {
ret.d[i + j - ] += now.d[i] * ops.d[j];
}
for(int i = ; i <= MAXSIZEOFBIGNUM - ; i++)
if(ret.d[i] >= BASE) {
ret.d[i + ] += ret.d[i] / BASE;
ret.d[i] %= BASE;
}
for(int i = MAXSIZEOFBIGNUM - ; i >= ; i--)
if(ret.d[i] > ) {
ret.Len = i;
break;
}
return ret;
}
BigNum BigNum::operator / (const BigNum &ops) const {
BigNum now = (*this), div, mod;
div.Len = now.Len;
mod.Len = ;
for(int j = now.Len; j >= ; j--) {
mod.Len++;
for(int p = mod.Len; p >= ; p--) {
mod.d[p] = mod.d[p - ];
}
mod.d[] = now.d[j];
while(mod >= ops) {
div.d[j]++;
mod = mod - ops;
}
if(mod.Len == && mod.d[] == ) {
mod.Len--;
}
}
div.clear();
mod.clear();
return div;
}
BigNum BigNum::operator % (const BigNum &ops) const {
BigNum now = (*this), div, mod;
div.Len = now.Len;
mod.Len = ;
for(int j = now.Len; j >= ; j--) {
mod.Len++;
for(int p = mod.Len; p >= ; p--) {
mod.d[p] = mod.d[p - ];
}
mod.d[] = now.d[j];
while(mod >= ops) {
div.d[j]++;
mod = mod - ops;
}
if(mod.Len == && mod.d[] == ) {
mod.Len--;
}
}
div.clear();
mod.clear();
return mod;
}
void BigNum::operator ++ (void) {
d[]++;
for(int i = ; i <= MAXSIZEOFBIGNUM - ; i++)
if(d[i] >= BASE) {
d[i] -= BASE;
d[i + ]++;
} else {
break;
}
if(d[Len + ] > ) {
Len++;
}
}
void BigNum::operator -- (void) {
d[]--;
for(int i = ; i <= MAXSIZEOFBIGNUM - ; i++)
if(d[i] < ) {
d[i] += BASE;
d[i + ]--;
} else {
break;
}
if(d[Len] == ) {
Len--;
}
}
BigNum BigNum::operator + (const int & ops) const {
BigNum ret = (*this);
ret.d[] += ops;
ret.clear();
return ret;
}
BigNum BigNum::operator - (const int & ops) const {
BigNum ret = (*this);
ret.d[] -= ops;
ret.clear();
return ret;
}
BigNum BigNum::operator * (const int & ops) const {
BigNum ret(*this);
for(int i = ; i <= ret.Len; i++) {
ret.d[i] *= ops;
}
for(int i = ; i <= MAXSIZEOFBIGNUM - ; i++)
if(ret.d[i] >= BASE) {
ret.d[i + ] += ret.d[i] / BASE;
ret.d[i] %= BASE;
}
for(int i = MAXSIZEOFBIGNUM - ; i >= ; i--)
if(ret.d[i] > ) {
ret.Len = i;
return ret;
}
ret.Len = ;
return ret;
}
BigNum BigNum::operator / (const int & ops) const {
BigNum ret;
int down = ;
for(int i = Len; i >= ; i--) {
ret.d[i] = (d[i] + down * BASE) / ops;
down = d[i] + down * BASE - ret.d[i] * ops;
}
ret.Len = Len;
while(ret.d[ret.Len] == && ret.Len > ) {
ret.Len--;
}
return ret;
}
int BigNum::operator % (const int &ops) const {
int mod = ;
for(int i = Len; i >= ; i--) {
mod = ((mod * BASE) % ops + d[i]) % ops;
}
return mod;
}
BigNum BigNum::operator ^ (const int &ops) const {
BigNum t, ret();
if(ops == ) {
return ret;
}
if(ops == ) {
return *this;
}
int m = ops, i;
while(m > ) {
t = *this;
for(i = ; (i << ) <= m; i <<= ) {
t = t * t;
}
m -= i;
ret = ret * t;
if(m == ) {
ret = ret * (*this);
}
}
return ret;
}
#endif
BigNum f[][];
int main() {
f[][] = ;
for(int i = ; i <= ; i++) {
for(int j = ; j <= i; j++) {
if(j == ) {
f[i][j] = ;
continue;
}
if(i == j) {
f[i][j] = f[i][j - ];
} else {
f[i][j] = f[i][j - ] + f[i - ][j];
}
}
}
int n, m,t=;
BigNum x;
while(scanf("%d%d", &n, &m) != EOF) {
t++;
if(n + m == ) {
break;
}
x = f[n][m];
for(int i = ; i <= n; i++) {
x = x * i;
}
for(int i = ; i <= m; i++) {
x = x * i;
}
cout << "Test #" << t << ":" << endl;
cout << x << endl;;
}
return ;
}
Buy the Ticket{HDU1133}的更多相关文章
- HDU1133 Buy the Ticket —— 卡特兰数
题目链接:https://vjudge.net/problem/HDU-1133 Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Me ...
- 【HDU 1133】 Buy the Ticket (卡特兰数)
Buy the Ticket Problem Description The "Harry Potter and the Goblet of Fire" will be on sh ...
- 【高精度练习+卡特兰数】【Uva1133】Buy the Ticket
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- 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(Catalan)
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- Codeforces 938 D. Buy a Ticket (dijkstra 求多元最短路)
题目链接:Buy a Ticket 题意: 给出n个点m条边,每个点每条边都有各自的权值,对于每个点i,求一个任意j,使得2×d[i][j] + a[j]最小. 题解: 这题其实就是要我们求任意两点的 ...
- HDUOJ---1133(卡特兰数扩展)Buy the Ticket
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- Codeforces 938.D Buy a Ticket
D. Buy a Ticket time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- hdu 1133 Buy the Ticket (大数+递推)
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
随机推荐
- iOS应用支持IPV6,就那点事儿
原文连接 果然是苹果打个哈欠,iOS行业内就得起一次风暴呀.自从5月初Apple明文规定所有开发者在6月1号以后提交新版本需要支持IPV6-Only的网络,大家便开始热火朝天的研究如何支持IPV6 ...
- Google 如何修复 TrustManager 实施方式不安全的应用
引用谷歌市场的帮助说明:https://support.google.com/faqs/answer/6346016 本文面向的是发布的应用中 X509TrustManager 接口实施方式不安全的开 ...
- iOS - UIButton设置图片文字上图下文排列
经查阅资料及尝试,最终解决了在图片和文字垂直排列的情况下,如果文字长度变化会导致图片位置变动的问题,最开始采用了网上比较多的做法,做法如下: @interface UIButton (UIButton ...
- python中统计列表各个元素的个数
- JVM内存区域与内存溢出异常
Java虚拟机在执行java程序时会把它所管理的内存会分为若干个不同的数据区域,不同的区域在内存不足时会抛出不同的异常. >>运行时数据区域的划分 (1)程序计数器程序计数器(Progra ...
- 【C#】 用Route进行URL重写
在.NET Framework 4中,微软推出了Route机制.这种机制不仅在MVC中大量运用,在WebForm中也可以使用. 和Contex.RewritePath()一样,Route功能也是写在G ...
- 在多台服务器上简单实现Redis的数据主从复制(3)(转载)
转载地址:http://www.cnblogs.com/liping13599168/archive/2011/04/14/2016226.html Redis的主从复制功能非常强大,一个master ...
- Delphi的字符串、PChar和字符数组之间的转换
参考:http://my.oschina.net/kavensu/blog/193719 以下的各种方法都是我在Delphi 6的环境下测试成功的,可能根据你的开发环境.不同的上下文语境……有一些可能 ...
- 【翻译二】java--并发之进程与线程
Processes and Threads In concurrent programming, there are two basic units of execution: processes a ...
- [SQL]oracle 的to_char、to_number、to_date用法
关键字: oracle 的to_char.to_number.to_date用法 TO_CHAR 是把日期或数字转换为字符串TO_DATE 是把字符串转换为数据库中得日期类型转换函数TO_NUMBER ...