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>
#define int int64_t
using namespace std;
const int maxn = 2010;
const int mod = 1e9 + 7;
int dp[maxn][maxn];
int n;
int ini, fim;
int solve(int i, int k) {
if (i == n+1) return (k==1);
if (dp[i][k] != -1) return dp[i][k];
int caso1, caso2;
if (i == ini || i == fim) {
caso1 = solve(i+1, k+1);
caso2 = k > 0 ? solve(i+1, k) : 0;
} else if (i < ini && i < fim) {
caso1 = (k+1) * solve(i+1, k+1);
caso2 = k > 1 ? (k-1) * solve(i+1, k-1) : 0;
} else if (i < ini || i < fim) {
caso1 = k * solve(i+1, k+1);
caso2 = k > 1 ? (k-1) * solve(i+1, k-1) : 0;
} else {
caso1 = (k-1) * solve(i+1, k+1);
caso2 = k > 1 ? (k-1) * solve(i+1, k-1) : 0;
}
return dp[i][k] = (caso1 + caso2) % mod;
}
int32_t main() {
memset(dp, -1, sizeof dp);
cin >> n;
cin >> ini >> fim;
cout << solve(1, 0) << '\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... |