# BZOJ2729 [HNOI2012]排队 【高精 + 组合数学】

BZOJ2729

## 题解

$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;
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(){
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;
}


posted @ 2018-05-22 14:58  Mychael  阅读(158)  评论(0编辑  收藏  举报