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<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define fi first
#define se second
#define ALL(x) (x).begin(), (x).end()
#define MASK(x) ((1LL) << (x))
#define BIT(x, i) (((x) >> (i)) & (1LL))
#define eb emplace_back
#define pb push_back
const int N = 5000 + 5;
const int mod = 1e9 + 7;
const int INF = 1e9 + 7;
const int base = 31;
const int BL = 350;
template<class X, class Y> bool maximize(X &x, Y y) {if(x < y){x = y; return true;} return false;}
template<class X, class Y> bool minimize(X &x, Y y) {if(x > y){x = y; return true;} return false;}
void add(int &x, int y){x += y; if(x >= mod) x -= mod;}
void sub(int &x, int y){x -= y; if(x < 0) x += mod;}
int mul(int x, int y){return 1LL * x * y % mod;}
int calPw(int x, int y){
int ans = 1;
while(y){
if(y & 1) ans = 1LL * ans * x % mod;
x = 1LL * x * x % mod;
y >>= 1;
}
return ans;
}
///Deruck Phung - Luong The Vinh High School For The Gifted
int n, m;
int dp[N][N];
int fact[2 * N], finv[2 * N], pw[2 * N], val[2 * N];
///Code
int C(int k, int n){
if(k > n) return 0;
return mul(fact[n], mul(finv[k], finv[n - k]));
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
#define TASK "main"
if(fopen(TASK".inp", "r")){
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
cin >> n >> m;
if(n > m) swap(n, m);
int num = n + m;
fact[0] = 1;
for(int i = 1; i <= num; i++) fact[i] = mul(fact[i - 1], i);
finv[num] = calPw(fact[num], mod - 2);
for(int i = num - 1; i >= 0; i--) finv[i] = mul(finv[i + 1], i + 1);
pw[0] = 1;
for(int i = 1; i <= num; i++) pw[i] = mul(pw[i - 1], 4);
///dp(i, j): 1 -> i, đã nối được j cạnh
dp[0][0] = 1;
for(int i = 1; i <= m; i++)
for(int j = 0; j <= min(i, n); j++){
///Còn cả trên cả dưới
if(i <= n){
///Không nối cả trên dưới
add(dp[i][j], dp[i - 1][j]);
///Nối 1 trên hoặc 1 dưới
if(j >= 1) add(dp[i][j], mul(mul(i - j, 2), dp[i - 1][j - 1]));
///Nối cả trên và dưới
if(j >= 2) add(dp[i][j], mul(mul(i - j + 1, i - j + 1), dp[i - 1][j - 2]));
///Nối 2 thằng cho nhau
if(j >= 1) add(dp[i][j], dp[i - 1][j - 1]);
}
else{
///Chỉ còn trên
///Không nối
add(dp[i][j], dp[i - 1][j]);
///Nối
if(j >= 1) add(dp[i][j], mul(n - j + 1, dp[i - 1][j - 1]));
}
//cout << i << " " << j << " " << dp[i][j] << "\n";
}
//cout << C(2, 4) << "\n";
//cout << dp[4][2] << "\n";
int Ans = 0;
for(int i = 1; i <= n; i++){
for(int j = 0; j <= i; j++){
add(val[j], mul(mul(mul(fact[j], pw[i - j]), C(j, i)), mul(dp[m][i], C(j, n + m - 2 * i))));
}
}
for(int i = 0; i <= n; i++){
// if(i == 2)
// cout << val[i] << "\n";
add(Ans, mul(val[i], calPw(calPw(2, i), mod - 2)));
}
//Ans = mul(Ans, calPw(2, mod - 2));
cout << Ans;
}
Compilation message (stderr)
tents.cpp: In function 'int main()':
tents.cpp:52:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
52 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
tents.cpp:53:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
53 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |