This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*input
10 2 3
*/
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll modulo = 1000000007;
ll fast(int N, int A, int B)
{
ll L[N + 1][N + 1][N + 1];
ll R[N + 1][N + 1][N + 1];
for (int n = 2; n <= N; n++)
{
for (int a = 1; a <= n; a++)
{
for (int b = 1; b <= n; b++)
{
L[n][a][b] = 0;
R[n][a][b] = 0;
if (a == b)
continue;
if (n == 2)
{
if (a > b)
{
L[n][a][b] = 1;
}
if (a < b)
{
R[n][a][b] = 1;
}
}
else
{
for (int c = 1; c <= n; c++)
{
if (c > b)
{
L[n][a][b] += R[n - 1][a - (a > b)][c - (c > b)];
}
if (c < b)
{
R[n][a][b] += L[n - 1][a - (a > b)][c - (c > b)];
}
}
}
L[n][a][b] %= modulo;
R[n][a][b] %= modulo;
}
}
}
ll ret = L[N][A][B] + R[N][A][B];
ret %= modulo;
return ret;
}
ll faster(int N, int A, int B)
{
ll L[N + 1][N + 1][N + 1];
ll R[N + 1][N + 1][N + 1];
for (int n = 2; n <= N; n++)
{
for (int a = 1; a <= n; a++)
{
L[n][a][0] = 0;
R[n][a][0] = 0;
for (int b = 1; b <= n; b++)
{
L[n][a][b] = L[n][a][b - 1];
R[n][a][b] = R[n][a][b - 1];
if (a == b)
continue;
if (n == 2)
{
if (a > b)
{
L[n][a][b] = 1;
}
if (a < b)
{
R[n][a][b] = 1;
}
}
else
{
L[n][a][b] += R[n - 1][a - (a > b)][n - 1];
L[n][a][b] -= R[n - 1][a - (a > b)][b - 1];
R[n][a][b] += L[n - 1][a - (a > b)][b - 1];
}
L[n][a][b] %= modulo;
R[n][a][b] %= modulo;
}
}
}
ll ret = L[N][A][B] - L[N][A][B - 1] + R[N][A][B] - R[N][A][B - 1];
ret %= modulo;
ret += modulo;
ret %= modulo;
return ret;
}
int main()
{
int n, a, b;
cin >> n >> a >> b;
//cout << fast(n, a, b) << "\n";
cout << faster(n, a, b) << "\n";
}
# | 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... |