# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
101067 | ecasdqina | Tents (JOI18_tents) | C++14 | 323 ms | 71032 KiB |
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::literals::string_literals;
using i64 = long long;
using std::cout;
using std::endl;
using std::cin;
const int MOD = 1e9 + 7;
i64 mod_pow(i64 x, i64 n = MOD - 2) {
i64 ret = 1;
while(n) {
if(n & 1) (ret *= x) %= MOD;
(x *= x) %= MOD;
n >>= 1;
}
return ret;
}
std::vector<i64> fact, inv;
i64 comb(int n, int r) {
return fact[n] * inv[n - r] % MOD * inv[r] % MOD;
}
int main() {
int h, w; scanf("%d%d", &h, &w);
// assert(h <= 300 and w <= 300);
fact.assign(std::max(h, w) + 1, 1); inv.assign(std::max(h, w) + 1, 1);
for(int i = 0; i < std::max(h, w); i++) fact[i + 1] = fact[i] * (i64)(i + 1) % MOD;
for(int i = 0; i <= std::max(h, w); i++) inv[i] = mod_pow(fact[i]);
std::vector<std::vector<i64>> dp(h + 1, std::vector<i64>(w + 1, 0));
dp[h][w] = 1;
for(i64 i = h; i >= 0; i--) {
for(i64 j = w; j >= 0; j--) {
if(i >= 1 and j >= 1) {
dp[i - 1][j - 1] += dp[i][j] * j % MOD * 4;
dp[i - 1][j - 1] %= MOD;
}
if(i >= 1 and j >= 2) {
dp[i - 1][j - 2] += dp[i][j] * comb(j, 2) % MOD;
dp[i - 1][j - 2] %= MOD;
}
if(i >= 2 and j >= 1) {
dp[i - 2][j - 1] += dp[i][j] * j % MOD * (i - 1) % MOD;
dp[i - 2][j - 1] %= MOD;
}
if(i >= 1) {
dp[i - 1][j] += dp[i][j];
dp[i - 1][j] %= MOD;
}
}
}
i64 ans = 0;
for(int j = 0; j <= w; j++) (ans += dp[0][j]) %= MOD;
printf("%lld\n", (ans - 1 + MOD) % MOD);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |