#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
#define all(x) x.begin() , x.end()
#define sze(x) (ll)(x.size())
#define mp(x , y) make_pair(x , y)
#define wall cout<<"--------------------------------------\n";
typedef long long int ll;
typedef pair<ll , ll> pll;
typedef pair<int , int> pii;
typedef double db;
typedef pair<pll , ll> plll;
typedef pair<int , pii> piii;
typedef pair<pll , pll> pllll;
const ll maxn = 3e3 + 17 , md = 1e9 + 7 , inf = 2e16;
inline ll tav(ll n , ll k){
ll res = 1;
while(k > 0){
if(k & 1){
res *= n; res %= md;
}
n *= n; n %= md;
k >>= 1;
}
return res;
}
ll fact[maxn] , _fact[maxn];
void fact_build(){
fact[0] = 1;
for(ll i = 1 ; i < maxn ; i++){
fact[i] = fact[i - 1] * i % md;
}
_fact[maxn - 1] = tav(fact[maxn - 1] , md - 2);
for(ll i = maxn - 2 ; ~i ; i--){
_fact[i] = _fact[i + 1] * (i + 1) % md;
}
return;
}
ll chs(ll n , ll k){
if(k < 0 || k > n) return 0;
return fact[n] * _fact[k] % md * _fact[n - k] % md;
}
ll dp[maxn][maxn] , ps[maxn][maxn] , ps2[maxn][maxn];
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
fact_build();
ll n , m;
cin>>n>>m;
fill(dp[0] , dp[0] + m + 1 , 1);
iota(ps[0] , ps[0] + m + 1 , 1);
for(ll i = 0 ; i <= m ; i++){
ps2[0][i] = (i + 2) * (i + 1) / 2;
}
for(ll i = 1 ; i <= n ; i++){
ps2[i][0] = ps[i][0] = dp[i][0] = 1;
for(ll j = 1 ; j <= m ; j++){
ll h = 1;
if(j > 1){
h += ps2[i - 1][j - 2] * i % md;
}
if(i > 1){
h += chs(i , 2) * ps[i - 2][j - 1] % md;
}
dp[i][j] = h % md;
ps[i][j] = ps[i][j - 1] + dp[i][j]; ps[i][j] %= md;
ps2[i][j] = ps2[i][j - 1] + dp[i][j] * (j + 1); ps2[i][j] %= md;
}
}
ll ans = -1 , lm = min(n , m);
for(ll i = 0 ; i <= lm ; i++){
ll h = chs(n , i) * chs(m , i) % md * fact[i] % md * dp[n - i][m - i] % md * tav(4 , i) % md;
ans += h;
}
ans %= md;
cout<<ans<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
2 ms |
2900 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
2 ms |
3540 KB |
Output is correct |
7 |
Correct |
1 ms |
1908 KB |
Output is correct |
8 |
Correct |
2 ms |
3796 KB |
Output is correct |
9 |
Correct |
2 ms |
1876 KB |
Output is correct |
10 |
Correct |
2 ms |
4820 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
4 ms |
6228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
2 ms |
2900 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
2 ms |
3540 KB |
Output is correct |
7 |
Correct |
1 ms |
1908 KB |
Output is correct |
8 |
Correct |
2 ms |
3796 KB |
Output is correct |
9 |
Correct |
2 ms |
1876 KB |
Output is correct |
10 |
Correct |
2 ms |
4820 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
4 ms |
6228 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
10 ms |
28372 KB |
Output is correct |
15 |
Correct |
103 ms |
164948 KB |
Output is correct |
16 |
Correct |
8 ms |
11532 KB |
Output is correct |
17 |
Correct |
31 ms |
37964 KB |
Output is correct |
18 |
Correct |
33 ms |
50676 KB |
Output is correct |
19 |
Correct |
119 ms |
188464 KB |
Output is correct |
20 |
Correct |
103 ms |
152048 KB |
Output is correct |
21 |
Correct |
70 ms |
100852 KB |
Output is correct |
22 |
Correct |
69 ms |
105860 KB |
Output is correct |
23 |
Correct |
47 ms |
80592 KB |
Output is correct |
24 |
Correct |
166 ms |
212880 KB |
Output is correct |
25 |
Correct |
134 ms |
183920 KB |
Output is correct |
26 |
Correct |
140 ms |
200168 KB |
Output is correct |
27 |
Correct |
164 ms |
207212 KB |
Output is correct |