This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#define LL long long
using namespace std;
const int ST = 1;
const int ED = 2;
const int MAX_N = 2005;
const int MOD = 1e9 + 7;
void add(LL &x, LL v) {
x = (x + v) % MOD;
}
LL dp[MAX_N][MAX_N][4];
int get(int x, int bit) {
return (x & bit) != 0;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int N, cs, cf;
cin >> N >> cs >> cf;
dp[0][0][0] = 1;
for(int i = 1; i <= N; i++) {
for(int j = 0; j < i; j++) {
for(int k = 0; k < 4; k++) {
if(i == cs) {
add(dp[i][j + 1][k | ST], dp[i - 1][j][k]);
add(dp[i][j][k | ST], dp[i - 1][j][k] * (j - (get(k, ED) && i < N)));
} else if(i == cf) {
add(dp[i][j + 1][k | ED], dp[i - 1][j][k]);
add(dp[i][j][k | ED], dp[i - 1][j][k] * (j - (get(k, ST) && i < N)));
} else {
add(dp[i][j + 1][k], dp[i - 1][j][k]);
if(j) {
if(i < N) {
if(k == 0) {
add(dp[i][j - 1][k], dp[i - 1][j][k] * j * (j - 1));
} else if(k < 3) {
add(dp[i][j - 1][k], dp[i - 1][j][k] * (j - 1) * (j - 1));
} else {
add(dp[i][j - 1][k], dp[i - 1][j][k] * (j - 1) * (j - 2));
}
} else {
add(dp[i][j - 1][k], dp[i - 1][j][k]);
}
}
}
}
}
}
cout << dp[N][1][ST | ED] << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |