传送门
HNOI2012 排队
题意:某中学有 n 名男同学,m 名女同学和两名老师要排队参加体检。他们排成一条直线,并且任意两名女同学不能相邻,两名老师也不能相邻,那么一共有多少种排法呢?(任意两人不同)
I think
排列组合+高精度
考虑先放男生,后放老师,再放女生
先用男生把两个老师隔开,再用男生和老师把所有女生隔开
方案数为: A(n,n)*A(n+1,2)*A(n+3,m)
或者用女生隔开老师
方案数为:A(n,n)*A(m,1)*A(2,2)*A(n+1,1)*A(n+3,m-1)(此时两个老师视为整体)
注意排列公式: A(m,n) = m!/(m-n)!
Code
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
const int sm = 2e3 + 10,Mod = 1e8;
int n,m;
int Max(int x,int y) { return x>y?x:y; }
struct Bign {
int len;
LL val[sm];
Bign() { len = 1; memset(val,0,sizeof(val)); val[0] = 1; }
void init() { val[0] = 1;}
Bign operator * (const int x) {
Bign ret; LL t = 0;
ret.val[0] = 0, ret.len = len + 1;
for(int i = 0; i < ret.len; ++i) {
ret.val[i] = (val[i] * x + t) % Mod;
t = (val[i] * x + t) / Mod;
}
while(ret.val[ret.len-1] == 0 && ret.len > 1) --ret.len;
return ret;
}
Bign operator + (const Bign & x) {
Bign ret; ret.len = Max(len,x.len) + 1;
LL t = 0; ret.val[0] = 0;
for(int i = 0; i < ret.len; ++i) {
ret.val[i] = (val[i] + x.val[i] + t) % Mod;
t = (val[i] + x.val[i] + t) / Mod;
}
while(ret.val[ret.len-1]==0 && ret.len>1)
--ret.len;
return ret;
}
void print() {
printf("%lld",val[len-1]);
for(int i = len-2; i >= 0; --i)
printf("%08lld",val[i]);
putchar(10);
}
}a,b;
int main() {
bool flaga = 0, flagb = 0;
scanf("%d%d",&n,&m);
if(n+3<m||n+m+1<2) return puts("0"),0;
else {
if(2<=n+1 && m<=n+3) {
flaga = 1;
for(int i = 1; i <= n+3; ++i) {
if(i <= n) a = a * i;
if(i > n-m+3) a = a * i;
}
a = a * (n*(n+1));
}
if(m!=0 && m-1<=n+2) {
flagb = 1;
for(int i = 1; i <= n+2; ++i) {
if(i <= n) b = b * i;
if(i > n-m+3) b = b * i;
}
b = b * (2*m*(n+1));
}
if(!flaga) a.val[0] = 0;
if(flagb) a = a + b;
a.print();
}
return 0;
}