BZOJ2729 [HNOI2012]排队 【高精 + 组合数学】
题目链接
题解
高考数学题。。。
我们先把老师看做男生,女生插空站
如果两个老师相邻,我们把他们看做一个男生,女生插空站
对于\(n\)个男生\(m\)个女生的方案数:
\[n!m!{n + 1 \choose m}
\]
还要特判一下没有男生女生的情况
然后高精即可
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 100005,B = 10000,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
struct NUM{
int s[4000],len;
NUM(){cls(s); len = 0;}
void out(){
printf("%d",s[len]);
for (int i = len - 1; i; i--)
printf("%04d",s[i]);
}
}A,C;
inline NUM operator *(const NUM& a,const int& b){
NUM c;
c.len = a.len;
LL carry = 0,tmp;
for (int i = 1; i <= a.len; i++){
tmp = a.s[i] * b + carry;
c.s[i] = tmp % B;
carry = tmp / B;
}
while (carry) c.s[++c.len] = carry % B,carry /= B;
return c;
}
inline NUM operator -(const NUM& a,const NUM& b){
NUM c;
c.len = a.len;
int carry = 0,tmp;
for (int i = 1; i <= a.len; i++){
tmp = a.s[i] - b.s[i] + carry;
if (tmp < 0) tmp += B,carry = -1;
else carry = 0;
c.s[i] = tmp;
}
while (c.len && !c.s[c.len]) c.len--;
return c;
}
int main(){
int n = read(),m = read();
if (n + 3 < m || (!n && !m)){
puts("0"); return 0;
}
n += 2;
A.s[A.len = 1] = 1;
for (int i = n; i > n - m + 1; i--)
A = A * i;
for (int i = n + 1; i; i--)
A = A * i;
n--;
if (n + 1 >= m){
C.s[C.len = 1] = 1;
for (int i = n; i > n - m + 1; i--)
C = C * i;
for (int i = n + 1; i; i--)
C = C * i;
C = C * 2;
}
A = A - C;
A.out();
return 0;
}