This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// oooo
/*
har chi delet mikhad bebar ~
gitar o ba khodet nabar! ~
;Amoo_Hasan;
*/
#include<bits/stdc++.h>
#pragma GCC optimize("O3,no-stack-protector,unroll-loops")
#pragma GCC target("avx2,fma")
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long ll;
typedef long double ld;
#define Sz(x) int((x).size())
#define All(x) (x).begin(), (x).end()
#define wtf(x) cout<<#x <<" : " <<x <<endl
constexpr ll inf = 1e18, N = 2e3 + 10, mod = 1e9 + 7;
int dp[N][N][2][2]; // dp[i][j][t][k] -> left size = i, right size = j, direction of first step = t, destination = k;
// 0 -> left, 1 -> right;
int ps_l[N][N][2][2];
int ps_r[N][N][2][2];
int fac[N], rfac[N], c[N][N];
inline ll poww(ll x, int y) {
ll res = 1;
for(; y; y /= 2, x = x * x % mod)
if(y & 1)
res *= x, res %= mod;
return res;
}
inline void pre() {
fac[0] = 1;
for(int i = 1; i < N; i++) fac[i] = 1ll * fac[i - 1] * i % mod;
rfac[N - 1] = poww(fac[N - 1], mod - 2);
for(int i = N - 2; i >= 0; i--) rfac[i] = 1ll * rfac[i + 1] * (i + 1) % mod;
}
inline ll choose(ll x, ll y) {
if(x < y) return 0;
if(x == y) return 1;
return 1ll * fac[x] * rfac[y] % mod * rfac[x - y] % mod;
}
int main() {
ios :: sync_with_stdio(0), cin.tie(0);
pre();
dp[0][0][0][1] = 1;
dp[0][0][1][0] = 1;
ps_l[0][1][0][1] = 1;
ps_l[0][1][1][0] = 1;
ps_r[0][1][0][1] = 1;
ps_r[0][1][1][0] = 1;
for(int len = 2; len < N; len++) {
for(int l = 0; l < len; l++) {
int r = len - l - 1;
if(l > 0) {
dp[l][r][0][0] += ps_l[l - 1][len - 1][1][0];
dp[l][r][0][1] += ps_l[l - 1][len - 1][1][1];
}
if(r > 0) {
dp[l][r][1][0] += ps_r[r - 1][len - 1][0][0];
dp[l][r][1][1] += ps_r[r - 1][len - 1][0][1];
}
if(l == 0) dp[l][r][0][0] = dp[l][r][1][0] = 0;
if(r == 0) dp[l][r][0][1] = dp[l][r][1][1] = 0;
for(int t = 0; t < 2; t++)
for(int k = 0; k < 2; k++) {
ps_l[l][len][t][k] += dp[l][r][t][k];
if(l > 0) ps_l[l][len][t][k] += ps_l[l - 1][len][t][k];
ps_l[l][len][t][k] %= mod;
}
}
for(int r = 0; r < len; r++) {
int l = len - r - 1;
for(int t = 0; t < 2; t++)
for(int k = 0; k < 2; k++) {
ps_r[r][len][t][k] += dp[l][r][t][k];
if(r > 0) ps_r[r][len][t][k] += ps_r[r - 1][len][t][k];
ps_r[r][len][t][k] %= mod;
}
}
}
int n, s, e; cin >>n >>s >>e;
if(s > e) swap(s, e);
if(s == 1) {
ll ans = dp[e - 1][n - e][0][0] + dp[e - 1][n - e][1][0];
ans %= mod;
cout<<ans;
return 0;
}
if(s == n) {
ll ans = dp[e - 1][n - e][0][1] + dp[e - 1][n - e][1][1];
ans %= mod;
cout<<ans;
return 0;
}
if(e == 1) {
ll ans = dp[s - 1][n - s][0][0] + dp[s - 1][n - s][1][0];
ans %= mod;
cout<<ans;
return 0;
}
if(e == n) {
ll ans = dp[s - 1][n - s][0][1] + dp[s - 1][n - s][1][1];
ans %= mod;
cout<<ans;
return 0;
}
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
c[i][j] = choose(i, j);
ll ans = 0;
for(int len1 = 0; len1 <= s - 2; len1++) {
ll cnt1 = c[s - 2][len1];
for(int len2 = 0; len2 <= e - s - 1; len2++) {
ll cnt2 = c[e - s - 1][len2];
for(int len3 = 0; len3 <= n - e - 1; len3++) {
ll cnt3 = c[n - e - 1][len3];
ll total = cnt1 * cnt2 % mod * cnt3 % mod;
ll val = total;
val *= (dp[len1 + 1][len2 + len3 + 1][0][0] + dp[len1 + 1][len2 + len3 + 1][1][0]);
val %= mod;
val *= (dp[(s - 1 - len1) + (e - s - 1 - len2)][n - e - 1 - len3][0][0] + dp[(s - 1 - len1) + (e - s - 1 - len2)][n - e - 1 - len3][1][0]);
val %= mod;
ans += val;
if(ans > mod) ans -= mod;
val = total;
val *= (dp[len1 + 1][len2 + len3 + 1][0][1] + dp[len1 + 1][len2 + len3 + 1][1][1]);
val %= mod;
val *= (dp[(s - 2 - len1) + (e - s - 1 - len2)][n - e - len3][0][1] + dp[(s - 2 - len1) + (e - s - 1 - len2)][n - e - len3][1][1]);
val %= mod;
ans += val;
if(ans > mod) ans -= mod;
}
}
}
cout<<ans;
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... |