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 long long
const int MOD = 1e9 + 7;
using pii=std::pair<int,int>;
using namespace std;
const int maxn = 2005;
int n, cs, cf, dp[maxn][maxn]; // dp[positions visited in first i steps][as j components] = number of ways of doing so
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> cs >> cf;
cs--; cf--;
dp[1][1] = 1;
for(int i = 1; i < n; i++)
for(int j = 1; j <= i; j++)
{
if(i == cs || i == cf)
{
// Add at start / end as expected, creating a new component if required
dp[i + 1][j] += dp[i][j];
dp[i + 1][j] %= MOD;
dp[i + 1][j + 1] += dp[i][j];
dp[i + 1][j + 1] %= MOD;
}
else
{
// Place as a seperate component
dp[i + 1][j + 1] += (j + 1 - (i > cs) - (i > cf)) * dp[i][j];
dp[i + 1][j + 1] %= MOD;
// Merge two components together
dp[i + 1][j - 1] += (j - 1) * dp[i][j];
dp[i + 1][j - 1] %= MOD;
}
}
cout << dp[n][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... |