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;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define maxn 303
int n, m, dp[maxn][maxn][maxn], pw[50], inv[maxn];
int mod = 1e9+7;
int modinv(int x) {
int ans = 1;
pw[0] = x%mod;
for(int i = 1; i <= 31; i++) {
pw[i] = pw[i-1]*pw[i-1];
pw[i]%= mod;
}
for(int i = 0; i <= 31; i++) {
if(((mod-2)&(1LL<<i)) != 0) {
ans*= pw[i];
ans%= mod;
}
}
return ans;
}
void solve() {
cin >> n >> m;
dp[0][0][0] = 1;
for(int i = 0; i <= min(n,m); i++) {
inv[i] = modinv(i);
}
int ans = 0;
for(int q1 = 0; q1 <= min(n,m); q1++) {
for(int q2 = 0; q2 <= min(n,m); q2++) {
for(int q3 = 0; q3 <= min(n,m); q3++) {
int i = n-q1-q2-2*q3;
int j = m-q1-2*q2-q3;
if(i < 0 || j < 0) continue;
// cout << q1 << " " << q2 << " " << q3 << " " << endl;
// cout << " " << i << " " << j << endl;
i++,j++;
if(q1!=0 && q2 == 0 && q3 == 0) dp[q1][q2][q3]+= ((4*dp[q1-1][q2][q3]*i*j)%mod * inv[q1])%mod;
// if(q1!=0) cout << " q1 " << 4*dp[q1-1][q2][q3]*i*j * inv[q1] << endl;
i--,j--;
i+=1,j+=2;
if(q2!=0 && q3 == 0) dp[q1][q2][q3]+= (((dp[q1][q2-1][q3]*(i*j*(j-1))/2)%mod) * inv[q2])%mod;
// if(q2!=0) cout << " q2 " << dp[q1][q2-1][q3]*(i*j*(j-1))/2 * inv[q2] << endl;
i-=1,j-=2;
i+=2,j+=1;
if(q3!=0) dp[q1][q2][q3]+= ((dp[q1][q2][q3-1]*(j*i*(i-1))/2)%mod * inv[q3])%mod;
// if(q3!=0) cout << " q3 " << dp[q1][q2][q3-1]*(j*i*(i-1))/2 * inv[q3] << endl;
i-=2,j-=1;
dp[q1][q2][q3]%= mod;
// cout << " " << dp[q1][q2][q3] << endl;
ans+= dp[q1][q2][q3];
ans%= mod;
}
}
}
cout << (ans-1+mod)%mod << endl;
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |