1133
Buy the Ticket
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5511 Accepted Submission(s): 2309
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.
6
18
180
这个题目是Catalan数的变形,不考虑人与人的差异,如果m=n的话那么就是我们初始的Catalan数问题,也就是将手持50元的人看成是+1,手持100元的人看成是-1,任前k个数值的和都非负的序列数。
这个题目区别就在于m>n的情况,此时我们仍然可以用原先的证明方法考虑,假设我们要的情况数是Dm+n,无法让每个人都买到的情况数是Um+n,那么就有Dm+n+Um+n= C(m+n, n),此时我们求Um+n,我们假设最早买不到票的人编号是k,他手持的是100元并且售票处没有钱,那么将前k个人的钱从50元变成100元,从100元变成50元,这时候就有n-1个人手持50元,m+1个手持100元的,所以就得到,于是我们的结果就因此得到了,表达式是C(m+n, m+1)
然后每个人都是不一样的,所以需要全排列 m! * n! 所以最后的公式就是(C(m+n, n)-C(m+n, m+1))*m!*n! 化简后为 (m+n)!*(m-n+1)/(m+1);
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std ;
const int MAXN=;
int f[MAXN],a[MAXN];
int main()
{
int i,j,t,k,n,m;
int x;
int c,s;
int sign;
k=;
while(cin>>m>>n&&!(m==&&n==))
{
printf("Test #%d:\n",++k);
memset(f,,sizeof(f));
memset(a,,sizeof(a));
f[]=;
for(i=; i<=m+n; i++)
{
c=;
for(j=; j<MAXN; j++)
{
s=f[j]*i+c;
f[j]=s%;
c=s/;
}
}
c=;
if(m>=n)
{
x=m-n+;
for(i=; i<MAXN; i++)
{
s=f[i]*x+c;
f[i]=s%;
c=s/;
}
t=;
for(j=MAXN-; j>=; j--) if(f[j]) break;
for(i=j; i>=; i--) a[t++]=f[i];
x=m+;
c=;
sign=;
for(i=; i<t; i++)
{
s=((c*)+a[i])/x;
if(s!=) sign=;
if(sign!=) cout<<s;
c=(c*+a[i])%x;
}
}
else cout<<"";
cout<<endl;
}
return ;
}