Submission #768342

#TimeUsernameProblemLanguageResultExecution timeMemory
768342raysh07Tents (JOI18_tents)C++17
100 / 100
157 ms72396 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define INF (int)1e18 #define f first #define s second mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count()); const int facN = 1e5 + 1; const long long mod = 1e9 + 7; long long fac[facN], invfac[facN]; long long power(int x, int y){ if (y==0) return 1; long long v = power(x, y/2); v *= v; v%= mod; if (y&1) return v * x % mod; else return v; } void Factorials(bool inverse = true){ fac[0] = invfac[0] = 1; for (int i=1; i<facN; i++){ fac[i] = fac[i-1] * i % mod; } if (!inverse) return; invfac[facN - 1] = power(fac[facN - 1], mod - 2); for (int i=facN - 2; i>=0; i--){ invfac[i] = invfac[i+1] * (i + 1) % mod; } } long long C(int n, int r){ return fac[n] * invfac[r] % mod * invfac[n-r] % mod; } long long P(int n, int r){ return fac[n] * invfac[n-r] % mod; } long long Solutions(int n, int r){ //x1 + .... + xn = r non negative integer solutions return C(n + r - 1, n - 1); } long long inverse(int n){ return power(n, mod - 2); } //remember to update facN and mod to the value you want //remember to actually call the Factorials function void Solve() { Factorials(); int h, w; cin >> h >> w; vector<vector<int>> dp(h + 1, vector<int>(w + 1, 0)); for (int i = 0; i <= h; i++){ for (int j = 0; j <= w; j++){ if (i == 0 || j == 0){ dp[i][j] = 1; continue; } //either there is thing in last row dp[i][j] = 4 * j * dp[i - 1][j - 1] + dp[i - 1][j]; dp[i][j] %= mod; } } int ans = 0; vector <int> dp1(max(h, w) + 1, 1); for (int i = 1; i <= max(h, w); i++){ dp1[i] = dp1[i - 1] * C(2 * i, 2) % mod; } for (int i = 0; i <= h; i++){ for (int j = 0; j <= w; j++){ int l1 = h - i - 2 * j; int l2 = w - j - 2 * i; if (l1 < 0 || l2 < 0) continue; int ways = C(h, i) * C(w, j) % mod * C(h - i, 2 * j) % mod * C(w - j, 2 * i) % mod * dp1[i] % mod * dp1[j] % mod; ans += ways * dp[l1][l2] % mod; ans %= mod; } } if (ans == 0) ans += mod; cout << ans - 1 << "\n"; } int32_t main() { auto begin = std::chrono::high_resolution_clock::now(); ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; // cin >> t; for(int i = 1; i <= t; i++) { //cout << "Case #" << i << ": "; Solve(); } auto end = std::chrono::high_resolution_clock::now(); auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin); cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...