이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
const int N = 2007;
const int MOD = 1e9 + 7;
void add(int &a, int b){
a += b;
if (a >= MOD) a -= MOD;
}
int n, s, f;
int dp[N][N];
namespace Odd{
void solve(){
int ans= 0;
s--; f--;
for(int iteration = 0; iteration <= 1; ++iteration){
if (s != n-1 && f != n-1){
memset(dp, 0, sizeof dp);
dp[0][0] = 1;
for(int i = 0; i<n-1; ++i) {
if (i == s || i == f){
for(int j = 0; j< n; ++j) add(dp[i+1][j], dp[i][j]);
continue;
}
int edge_cnt = (i >= s) + (i >= f);
for(int j = 0; j< n; ++j) if (dp[i][j]){
add(dp[i+1][j+1], dp[i][j]);
if (j > 0 && j + edge_cnt > 1){
int mul = j * (j-1) + edge_cnt * j;
add(dp[i+1][j-1], 1LL * mul * dp[i][j] % MOD);
}
}
}
add(ans, dp[n-1][0]);
}
s = n-1-s, f = n-1-f;
}
cout << ans << "\n";
exit(0);
}
}
namespace Even{
void solve(){
int ans = 0;
s--; f--;
for(int iteration = 0; iteration <= 1; ++iteration){
if (s != n-1 && f != 0){
memset(dp, 0, sizeof dp);
dp[0][0] = 1;
for(int i = 0; i<n; ++i) {
if (i == s){
for(int j = 0; j< n; ++j) add(dp[i+1][j], dp[i][j]);
continue;
}
if (i == f){
for(int j = 1; j<n; ++j) if (dp[i][j]){
add(dp[i+1][j-1], 1LL * dp[i][j] * j % MOD);
}
continue;
}
int edge_cnt = (i >= s) + (i >= f);
for(int j = 0; j< n; ++j) if (dp[i][j]){
add(dp[i+1][j+1], dp[i][j]);
if (j > 0 && j + edge_cnt > 1){
int mul = j * (j-1) + edge_cnt * j;
add(dp[i+1][j-1], 1LL * mul * dp[i][j] % MOD);
}
}
}
add(ans, dp[n-1][0]);
}
s = n-1-s, f = n-1-f;
}
cout << ans << "\n";
exit(0);
}
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> s >> f;
if (n % 2 == 1) Odd::solve();
else Even::solve();
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... |