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 MOD = 1000000007;
int Add(int x, int y) {
return (x + y) % MOD;
}
void Increase(int& x, int y) {
x = Add(x, y);
}
int Multiply(int x, int y) {
return 1LL * x * y % MOD;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, startPosition, finishPosition;
cin >> n >> startPosition >> finishPosition;
vector<vector<int>> ways(n + 2, vector<int>(n + 1));
ways[2][1] = 1;
for (int i = 2; i <= n; ++i) {
for (int components = 1; components < i; ++components) {
if (ways[i][components] == 0) {
continue;
}
if (i == startPosition || i == finishPosition) {
Increase(ways[i + 1][components + 1], ways[i][components]); // open a new component on the side
Increase(ways[i + 1][components], ways[i][components]); // append to the side
}
else {
// create a new component
int validPosition = (components + 1) - (i > startPosition) - (i > finishPosition);
Increase(ways[i + 1][components + 1], Multiply(validPosition, ways[i][components]));
// merge two component
validPosition = components - 1;
Increase(ways[i + 1][components - 1], Multiply(validPosition, ways[i][components]));
}
}
}
cout << ways[n + 1][1];
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... |