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 <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <utility>
#include <array>
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,tune=native")
using namespace std;
#define ALL(x) x.begin(), x.end()
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false);
using ll = long long;
#define N 2005
const ll M = 1000000007;
int n, cs, cf, dp[2][N][2][2];
int main()
{
ShinLena;
cin >> n >> cs >> cf;
if (cs > cf) swap(cs, cf);
dp[0][0][0][0] = 1;
for (int I = 1, i = 1; i <= n; ++i, I ^= 1)
{
memset(dp[I], 0, sizeof *dp);
if (i == cs)
{
for (int j = 1; j <= i; ++j)
{
dp[I][j][0][0] = dp[!I][j-1][0][0];
dp[I][j][1][0] = dp[!I][j][0][0];
dp[I][j][0][1] = dp[!I][j-1][0][1];
dp[I][j][1][1] = dp[!I][j][0][1];
}
}
else if (i == cf)
{
for (int j = 1; j <= i; ++j)
{
dp[I][j][0][0] = dp[!I][j-1][0][0];
dp[I][j][1][0] = dp[!I][j-1][1][0];
dp[I][j][0][1] = dp[!I][j][0][0];
dp[I][j][1][1] = dp[!I][j][1][0];
}
}
else if (i < cs)
{
for (int j = 1; j <= i; ++j)
{
dp[I][j][0][0] = ((1ll * dp[!I][j-1][0][0] * j % M)
+ (1ll * dp[!I][j+1][0][0] * j % M)) % M;
dp[I][j][1][0] = ((1ll * dp[!I][j][0][0])
+ (1ll * dp[!I][j-1][1][0] * (j-1) % M)
+ (1ll * dp[!I][j+1][1][0] * j % M)) % M;
dp[I][j][0][1] = ((1ll * dp[!I][j][0][0])
+ (1ll * dp[!I][j-1][0][1] * (j-1) % M)
+ (1ll * dp[!I][j+1][0][1] * j % M)) % M;
dp[I][j][1][1] = ((1ll * dp[!I][j][1][0])
+ (1ll * dp[!I][j][0][1])
+ (1ll * dp[!I][j-1][1][1] * (j-2) % M)
+ (1ll * dp[!I][j+1][1][1])) % M;
}
}
else if (i < cf)
{
for (int j = 1; j <= i; ++j)
{
dp[I][j][0][0] = ((1ll * dp[!I][j-1][0][0] * (j-1) % M)
+ (1ll * dp[!I][j+1][0][0] * j % M)) % M;
dp[I][j][1][0] = ((0)
+ (1ll * dp[!I][j-1][1][0] * (j-1) % M)
+ (1ll * dp[!I][j+1][1][0] * j % M)) % M;
dp[I][j][0][1] = ((1ll * dp[!I][j][0][0])
+ (1ll * dp[!I][j-1][0][1] * (j-2) % M)
+ (1ll * dp[!I][j+1][0][1] * j % M)) % M;
dp[I][j][1][1] = ((1ll * dp[!I][j][1][0])
+ (0)
+ (1ll * dp[!I][j-1][1][1] * (j-2) % M)
+ (1ll * dp[!I][j+1][1][1])) % M;
}
}
else
{
for (int j = 1; j <= i; ++j)
{
dp[I][j][0][0] = ((1ll * dp[!I][j-1][0][0] * (j-2) % M)
+ (1ll * dp[!I][j+1][0][0] * j % M)) % M;
dp[I][j][1][0] = ((0)
+ (1ll * dp[!I][j-1][1][0] * (j-2) % M)
+ (1ll * dp[!I][j+1][1][0] * j % M)) % M;
dp[I][j][0][1] = ((0)
+ (1ll * dp[!I][j-1][0][1] * (j-2) % M)
+ (1ll * dp[!I][j+1][0][1] * j % M)) % M;
dp[I][j][1][1] = ((0)
+ (0)
+ (1ll * dp[!I][j-1][1][1] * (j-2) % M)
+ (1ll * dp[!I][j+1][1][1])) % M;
}
}
}
cout << (1ll * dp[n&1][1][0][0] + dp[n&1][1][1][0] + dp[n&1][1][0][1] + dp[n&1][1][1][1]) % M;
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... |