This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/** I can do this all day **/
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
#define Mp make_pair
#define SZ(x) (int)x.size()
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
const int N = 2e2 + 10;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ll inf = 8e18;
const int LOG = 22;
ll pw(ll a , ll b, ll M) { return (!b ? 1 : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M : pw(a * a % M, b / 2, M))); }
ll F[N], I[N];
ll E(ll r, ll n)
{
if(r > n || r < 0) return 0;
return F[n] * I[r] % mod * I[n - r] % mod;
}
int n, s, f;
ll dp[N][4][N][N]; /// tedad halati ke toye i taye aval T ta masire ye samt baste vojod dare / a / b
int main()
{
F[0] = 1;
for(int i = 1; i < N; i ++) F[i] = F[i - 1] * i % mod;
I[N - 1] = pw(F[N - 1], mod - 2, mod);
for(int i = N - 2; ~i; i --)
{
I[i] = I[i + 1] * (i + 1) % mod;
}
scanf("%d%d%d", &n, &s, &f);
dp[0][0][0][0] = 1;
for(int i = 0; i < n; i ++)
{
for(int c = 0; c < 3; c ++)
{
for(int a = 0; a <= i; a ++)
{
for(int b = 0; b <= i - a; b ++)
{
ll cu = dp[i][c][a][b];
if(!cu) continue;
if(i + 1 == f || i + 1 == s)
{
/// match be ye a
if(a) dp[i + 1][c + 1][a - 1][b] = (dp[i + 1][c + 1][a - 1][b] + cu * a) % mod;
/// match be ye b
if(b > 1) dp[i + 1][c + 1][a][b - 2] = (dp[i + 1][c + 1][a][b - 2] + cu * b) % mod;
/// match be ye c
if(c) dp[i + 1][c - 1][a][b] = (dp[i + 1][c - 1][a][b] + cu) % mod;
dp[i + 1][c + 1][a][b] = (dp[i + 1][c + 1][a][b] + cu) % mod;
}
else
{
/// match be 2 ta a
if(a > 1) dp[i + 1][c][a - 2][b + 2] = (dp[i + 1][c][a - 2][b + 2] + cu * E(2, a)) % mod;
/// match be 2 ta b
if(b > 1)
{
dp[i + 1][c][a][b - 2] = (dp[i + 1][c][a][b - 2] + cu * b * (b - 2) / 2) % mod;
}
/// match be ye a ye b
if(a && b)
{
dp[i + 1][c][a - 1][b] = (dp[i + 1][c][a - 1][b] + cu * a * b) % mod;
}
/// match be 2 ta c
if(c > 1)
{
dp[i + 1][c - 2][a][b] = (dp[i + 1][c - 2][a][b] + cu) % mod;
}
///match be ye c va ye a
if(c && a)
{
dp[i + 1][c][a - 1][b] = (dp[i + 1][c][a - 1][b] + cu * a * c) % mod;
}
/// match be ye c va ye b
if(c > 0 && b > 1)
{
dp[i + 1][c][a][b - 2] = (dp[i + 1][c][a][b - 2] + cu * c * b) % mod;
}
dp[i + 1][c][a + 1][b] = (dp[i + 1][c][a + 1][b] + cu) % mod;
}
}
}
}
}
/*for(int i = 0; i <= n; i ++)
{
for(int c = 0; c < 3; c ++)
{
for(int a = 0; a <= i; a ++)
{
for(int b = 0; b <= i - a; b ++)
{
if(!dp[i][c][a][b]) continue;
printf("i = %d c = %d a = %d b = %d dp = %lld\n", i, c, a, b, dp[i][c][a][b]);
}
}
}
}
*/
printf("%lld", dp[n][0][0][0]);
return 0;
}
/** test corner cases(n = 1?) watch for overflow or minus indices **/
Compilation message (stderr)
kangaroo.cpp: In function 'int main()':
kangaroo.cpp:48:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | scanf("%d%d%d", &n, &s, &f);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |