# CF285E Positions in Permutations

代码：

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
using namespace std;

typedef long long ll;
const int MAX_N = 1005;
const ll mod = 1000000007ll;

int n, k;
int pi[MAX_N], C[MAX_N][MAX_N];
int f[MAX_N][MAX_N][2][2], at_least[MAX_N];

inline void add(int &x, int y) {
x = (x + y) % mod;
return;
}

int main() {
scanf("%d%d", &n, &k);
pi[0] = 1;
for (int i = 1; i <= n; ++i) pi[i] = int(1ll * pi[i - 1] * i % mod);
f[0][0][1][0] = 1;
for (int i = 1; i <= n; ++i) {
register int maxj = min(i, n);
for (int j = 0; j <= maxj; ++j) {
add(f[i][j][0][0], int(1ll * (f[i - 1][j][0][0] + f[i - 1][j][1][0]) % mod));
add(f[i][j][1][0], (f[i - 1][j][0][1] + f[i - 1][j][1][1]) % mod);
add(f[i][j + 1][0][0], f[i - 1][j][0][0]);
add(f[i][j + 1][1][0], f[i - 1][j][0][1]);
add(f[i][j + 1][0][1], int(1ll * (f[i - 1][j][0][0] + f[i - 1][j][1][0]) % mod));
add(f[i][j + 1][1][1], int(1ll * (f[i - 1][j][0][1] + f[i - 1][j][1][1]) % mod));
}
}
for (int i = 0; i <= n; ++i)
at_least[i] = 1ll * (f[n][i][0][0] + f[n][i][1][0]) % mod * pi[n - i] % mod;
for (int i = 0; i <= n; ++i) {
C[i][0] = C[i][i] = 1;
for (int j = 1; j < i; ++j)
C[i][j] = int(1ll * (C[i - 1][j - 1] + C[i - 1][j]) % mod);
}
int ans = at_least[k];
for (int i = k + 1; i <= n; ++i) {
ans = (1ll * (ans + (((i - k) & 1) ? -1ll : 1ll) * C[i][k] * at_least[i]) % mod + mod) % mod;
}
printf("%d\n", ans);
return 0;
}

posted @ 2018-11-02 21:43  AWordThatWeDefine  阅读(309)  评论(0编辑  收藏  举报