#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <vector>
using namespace std;
#define N 1020
#define M 400030
#define mod 1000000007
#define B 234324
#define LL long long
int f[N][N][4];
int fac[N], nfac[N];
int F[N];
int n, k;
int qpow(int x, int k) {
int ret = 1;
while(k) {
if(k & 1) ret = 1LL * ret * x % mod;
k >>= 1;
x = 1LL * x * x % mod;
}
return ret;
}
void add(int &x, int y) {
x += y;
if(x >= mod) x -= mod;
}
int C(int x, int y) {
return 1LL * fac[x] * nfac[y] % mod * nfac[x-y] % mod;
}
void init() {
fac[0] = 1; for(int i = 1; i < N; ++i) fac[i] = 1LL * fac[i-1] * i % mod;
nfac[N-1] = qpow(fac[N-1], mod - 2);
for(int i = N - 2; i >= 0; --i) nfac[i] = 1LL * nfac[i+1] * (i + 1) % mod;
f[0][0][2] = 1;
for(int i = 1; i <= n; ++i) {
for(int j = 0; j < i; ++j) {
for(int z = 0; z < 4; ++z) {
if(z & 1) add(f[i][j+1][z/2+2], f[i-1][j][z]);
if(i < n) add(f[i][j+1][z/2], f[i-1][j][z]);
add(f[i][j][z/2+2], f[i-1][j][z]);
}
}
}
for(int i = 0; i <= n; ++i) {
for(int z = 0; z < 4; ++z) {
add(F[i], f[n][i][z]);
}
F[i] = 1LL * F[i] * fac[n-i] % mod;
}
for(int i = 0; i <= n; ++i) {
int mul = -1;
for(int j = i + 1; j <= n; ++j) {
int tmp = 1LL * F[j] * C(j, i) % mod;
tmp *= mul;
if(tmp < 0) tmp += mod;
add(F[i], tmp);
mul = -mul;
}
}
}
int main() {
cin >> n >> k;
init();
cout << F[k] << endl;
return 0;
}
Codeforces 285E Positions in Permutations (dp + 容斥)
最新推荐文章于 2020-03-20 21:43:56 发布