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 N = 2004, MOD = 1e9 + 7;
int memo[N][N], n;
int add(int a, int b) {
a += b;
while(a >= MOD)
a -= MOD;
while(a < 0)
a += MOD;
return a;
}
int mul(int a, int b) {
return (1ll * (a % MOD) * (b % MOD)) % MOD;
}
int s, e;
int solve(int i, int j) {
if(i == n + 1)
return j == 1;
int &ret = memo[i][j];
if(~ret)
return ret;
bool putStart = (i > s);
bool putEnd = (i > e);
ret = 0;
// add to comp
if(i == s) {
if(!(j == 1 && putEnd && i < n))
ret = add(ret, solve(i + 1, j));
ret = add(ret, solve(i + 1, j + 1));
return ret;
}
if(i == e) {
if(!(j == 1 && putStart && i < n))
ret = add(ret, solve(i + 1, j));
ret = add(ret, solve(i + 1, j + 1));
return ret;
}
// new comp ?(x)?(y)?(z)?
ret = add(ret, mul(max(0, j + 1 - putStart - putEnd), solve(i + 1, j + 1)));
// merge two adjacent comps
if(j > 1)
ret = add(ret, mul(j - 1, solve(i + 1, j - 1)));
return ret;
}
void solve() {
cin >> n >> s >> e;
memset(memo , -1 ,sizeof memo);
cout << solve(1, 0) << "\n";
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#else
#endif
int tc = 1;
// cin >> tc;
while(tc--){
solve();
}
return 0;
}
Compilation message (stderr)
kangaroo.cpp: In function 'int main()':
kangaroo.cpp:61:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |