Buy the Ticket
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6250 Accepted Submission(s): 2637
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
首先 简化一下问题 假设每个有50元的人都是无差别的 100元的人也是无差别的
那dp[i][j]表示 共有i+j人的队伍里 i个人有50 j个人有100 不会出现不能找钱的情况的方案数量
d[i][j]=d[i-1][j]+d[i][j-1]
表示:
在d[i-1][j]后面加一个50的人
在d[i][j-1]后面加一个100元的人
如果每个人都是有差别的话 方案数就是再乘以n,m的全排列
d[n][m]*n!*m!
发现d[i][i] i=0,1,2,…是卡特兰数 但是不知道怎么求d[i][j] i!=j 只能dp了
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
#include<bitset>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
struct BigNum{
deque<int>num;
//构造函数
BigNum(){
num.push_back(0);
}
BigNum(int x){
if(!x)
num.push_back(0);
while(x){
num.push_front(x%10);
x/=10;
}
}
//重载运算符
BigNum operator+(const BigNum&x){//大数加法
BigNum res;
const deque<int>&a=num,&b=x.num;
deque<int>&c=res.num;
int alen=a.size(),blen=b.size();
c.resize(max(alen,blen));
for(int i=1,len=c.size();i<=c.size();++i){
if(alen>=i)
c[len-i]+=a[alen-i];
if(blen>=i)
c[len-i]+=b[blen-i];
}
for(int i=c.size()-1;i>0;--i){
c[i-1]+=c[i]/10;
c[i]%=10;
}
if(c[0]>9){
c[0]-=10;
c.push_front(1);
}
return res;
}
BigNum operator*(const BigNum&x){//大数乘法
int zeros=0;
BigNum res,t;
const deque<int>&a=this->num,&b=x.num;
deque<int>&tem=t.num;
for(int i=b.size()-1;i>=0;--i){
tem.clear();
for(int j=0;j<a.size();++j)
tem.push_back(a[j]*b[i]);
for(int j=0;j<zeros;++j)
tem.push_back(0);
for(int j=tem.size()-1;j>0;--j){
tem[j-1]+=tem[j]/10;
tem[j]%=10;
}
if(tem[0]>9){
tem.push_front(tem[0]/10);
tem[1]%=10;
}
res=res+t;
++zeros;
}
while(res.num.size()>1&&res.num[0]==0)
res.num.pop_front();
return res;
}
};
ostream&operator<<(ostream&out,BigNum&x){
deque<int>&a=x.num;
for(int i=0;i<a.size();++i)
cout<<a[i];
return out;
}
BigNum d[101][101];
void dp(){
d[0][0]=BigNum(1);
for(int i=1;i<101;++i)
for(int j=0;j<=i;++j)
d[i][j]=d[i][j-1]+d[i-1][j];
}
BigNum jc[100];
void inijc(){//提前算出0-100的阶乘
jc[0]=jc[1]=BigNum(1);
for(int i=2;i<101;++i)
jc[i]=jc[i-1]*BigNum(i);
}
int main()
{
//freopen("/home/lu/文档/r.txt","r",stdin);
//freopen("/home/lu/文档/w.txt","w",stdout);
dp();
inijc();
int n,m;
for(int t=1;scanf("%d%d",&n,&m),n+m;++t){
BigNum res=d[n][m]*jc[n]*jc[m];
cout<<"Test #"<<t<<":"<<endl<<res<<endl;
}
return 0;
}