题解 P3223 【[HNOI2012]排队】
一个普通的高中排列组合问题。
考虑两种情况:
1:老师中间是男生
运用插空法,先排男生有 n!种排法;
在男生中插入两只老师:2 p (n+1)种排法,换句话说A( (n+1) , 2 );
最后在男生和老师中插入女生:m p (n+3)种排法,换句话说A( (n+3) , m );
2.老师中间是女生
同上,男生 n!;
将两位老师打包(n+1),老师之间排列方式 2 种;
挑一个女生在老师之间 m 种,剩下的女生(m-1 ) p (n+2)种排法,换句话说A( (n+2) , ( m-1 ) );
综合两种情况,整理得{ n ! ( n+1 )( n+2 ) ! ( n*n+3*n+2*m ) } / ( n-m+3 ) ! ;
代码如下。
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
long long fj,ans[10000];
int n,m,len(1);
const long long p=10000000000ll;
void mul(int x)
{
long long pre(0),tem;
for(int i=1;i<=len;i++)
{
tem=ans[i]*x;
ans[i]=tem%p+pre;
pre=tem/p;
}
if(pre){len++;ans[len]=pre;};
}
int main(){
scanf("%d%d",&n,&m);
ans[1]=1;
mul(n+1);mul(n*(n+3)+2*m);
for(int i=1;i<=n;i++)mul(i);
for(int i=n-m+4;i<=n+2;i++)mul(i);
printf("%lld",ans[len]);
while(--len)printf("%010lld",ans[len]);
//cout<<ans[len];
//while(--len)cout<<ans[len];
return 0;
}
————————————北镇中学OI队(犯罪团伙)