# bzoj4517: [Sdoi2016]排列计数

### 题目链接

bzoj4517: [Sdoi2016]排列计数

### 题解

$ans = C(n,m) * D(n-m)$ , $D(x)$表示元素为x个的序列的错排数

$D(x) = (x-1)* (D(x-1) + D(x-2))$

### 代码

// T2
// ans = C_n^m * (A(n,n) - A(n,n-m-1) + A(n,n-m-2).....)
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
int x = 0,f = 1;
char c = getchar ();
while(c < '0' || c > '9') { if(c == '-') f = -1;c = getchar();}
while(c <= '9' && c >= '0') x = x * 10 + c - '0' ,c = getchar();
return x * f;
}
const long long mod = 1e9 + 7;
const int maxn = 1000007;
#define LL long long
LL jc[maxn],f[maxn];
inline LL poow(LL a,int k = mod-2) {
LL ret = a;
for(k --;k;k >>= 1,a = a * a % mod)
if(k & 1) ret = ret * a % mod;
return ret;
}
inline LL inv(LL x) { return poow(x); }

LL C (int n,int m) {
return (jc[n] * inv(jc[m] * jc[n - m] % mod)  ) % mod;
}
/* LL get_num(int n) {
//	if(n == 1) return 1;
LL ret = 0;
for(int k,i = 2;i <= n;++ i) {
if(i&1) k = -1;else k = 1;
ret = ( ret +  (k * (jc[n]  *(inv(jc[i]) % mod) )%mod ) +mod ) %mod;
}
return ret;
} */
int main() {
//freopen("permutation.in","r",stdin); freopen("permutation.out","w",stdout);
jc[0]=1;
for(int i = 1;i <= 1000003;++ i)
jc[i] = jc[i-1] * i % mod;
//for(int i = 1;i <= 500003;++ i)
f[1]=f[0]=1;
for(int i = 1;i <= 1000003;++ i) f[i] = (i - 1) *( f[i-1] + f[i-2])%mod;
for( int n,m; T --;) {