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 <bits/stdc++.h>
using namespace std;
const int kN = 2e3;
const int mod = 1e9 + 7;
int dp[2][1 + kN];
void addSelf(int &x, int y) {
x += y;
if (x >= mod) {
x -= mod;
}
}
int add(int x, int y) {
addSelf(x, y);
return x;
}
void multSelf(int &x, int y) {
x = (int64_t)x * y % mod;
}
int mult(int x, int y) {
multSelf(x, y);
return x;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, first, last;
cin >> n >> first >> last;
dp[1][1] = 1;
for (int i = 2, t = 0; i <= n; ++i, t ^= 1) {
for (int j = 1; j <= i; ++j) {
if (i == first || i == last) {
dp[t][j] = add(dp[t ^ 1][j - 1], dp[t ^ 1][j]);
} else {
dp[t][j] = add(mult(dp[t ^ 1][j + 1], j), mult(dp[t ^ 1][j - 1], j - (i > first) - (i > last)));
}
}
}
cout << dp[n & 1][1] << '\n';
return 0;
}
# | 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... |