#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[N], finv[N], pw[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 && j <= i - 1) add(dp[i][j], mul(2, dp[i - 1][j - 1]));
///Nối cả trên và dưới
if(j >= 2) add(dp[i][j], 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], dp[i - 1][j - 1]);
}
//cout << i << " " << j << " " << dp[i][j] << "\n";
}
//cout << C(2, 4) << "\n";
int Ans = 0;
// cout << dp[4][1] << "\n";
for(int i = 1; i <= n; i++){
int Sum = 0;
for(int j = 0; j <= min(i, n + m - 2 * i); j++){
add(Sum, mul(mul(pw[i - j], C(j, i)),C(j, n + m - 2 * i)));
if(j == 0) Sum = mul(Sum, 2);
}
add(Ans, mul(Sum, dp[m][i]));
}
Ans = mul(Ans, calPw(2, mod - 2));
cout << Ans;
}
Compilation message
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 |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
2 ms |
1104 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
2 ms |
1104 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |