답안 #521509

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
521509 2022-02-02T09:47:30 Z cig32 Tents (JOI18_tents) C++17
100 / 100
145 ms 72388 KB
#pragma GCC optimize("Ofast")
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <codecvt>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
using namespace std;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 2e5 + 10;
const int MOD = 1e9 + 7;
#define int long long

int rnd(int x, int y) {
  int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int f[MAXN];
int bm(int b, int p) {
  if(p==0) return 1 % MOD;
  int r = bm(b, p >> 1);
  if(p & 1) return (((r * r) % MOD) * b) % MOD;
  return (r * r) % MOD;
}
int inv(int b) {
  return bm(b, MOD - 2);
}
int op[6001];
int nCr(int n, int r) {
  int ans = f[n] * op[r];
  ans %= MOD;
  return (ans * op[n-r]) % MOD;
}
void solve(int tc) {
  f[0] = 1;
  for(int i=1; i<MAXN; i++) f[i] = (f[i-1] * i) % MOD;
  for(int i=0; i<6001; i++) op[i] = inv(f[i]);
  int H, W, ans = 0;
  cin >> H >> W;
  int cnt[H+1][W+1];
  for(int i=0; i<=W; i++) cnt[0][i] = 1;
  for(int i=0; i<=H; i++) cnt[i][0] = 1;
  cnt[1][1] = 5;
  for(int i=2; i<=H; i++) cnt[i][1] = 4*i + 1;
  for(int i=2; i<=W; i++) cnt[1][i] = 4*i + 1;
  for(int i=2; i<=H; i++) {
    for(int j=2; j<=W; j++) {
      cnt[i][j] = (cnt[i-1][j-1] * 5 + cnt[i-1][j-2] * 4 * (j-1) + cnt[i-2][j-1] * 4 * (i-1)) % MOD;
      cnt[i][j] += (i-1) * (j-1) * 16 * cnt[i-2][j-2];
      cnt[i][j] %= MOD;
    }
  }
  int ono[6001];
  for(int i=0; i<=6000; i++) ono[i] = inv(bm(2, i));
  for(int x=0; x<=H; x++) {
    for(int y=0; x+2*y<=H && y+2*x<=W; y++) {
      int combs = nCr(H, x) * nCr(W, 2*x);
      combs %= MOD;
      combs *= f[2*x];
      combs %= MOD;
      combs *= ono[x];
      combs %= MOD;
      combs *= nCr(W-2*x, y);
      combs %= MOD;
      combs *= nCr(H-x, 2*y);
      combs %= MOD;
      combs *= f[2*y];
      combs %= MOD;
      combs *= ono[y];
      combs %= MOD;
      ans += combs * cnt[H - x - 2*y][W - y - 2*x];
      ans %= MOD;
    }
  }
  cout << (ans + MOD - 1) % MOD << "\n";
}
int32_t main(){
  ios::sync_with_stdio(0); cin.tie(0);
  int t = 1; //cin >> t;
  for(int i=1; i<=t; i++) solve(i);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1868 KB Output is correct
2 Correct 5 ms 1868 KB Output is correct
3 Correct 5 ms 1860 KB Output is correct
4 Correct 5 ms 1964 KB Output is correct
5 Correct 5 ms 1996 KB Output is correct
6 Correct 4 ms 2028 KB Output is correct
7 Correct 6 ms 2124 KB Output is correct
8 Correct 5 ms 2064 KB Output is correct
9 Correct 6 ms 1972 KB Output is correct
10 Correct 7 ms 2252 KB Output is correct
11 Correct 6 ms 1996 KB Output is correct
12 Correct 6 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1868 KB Output is correct
2 Correct 5 ms 1868 KB Output is correct
3 Correct 5 ms 1860 KB Output is correct
4 Correct 5 ms 1964 KB Output is correct
5 Correct 5 ms 1996 KB Output is correct
6 Correct 4 ms 2028 KB Output is correct
7 Correct 6 ms 2124 KB Output is correct
8 Correct 5 ms 2064 KB Output is correct
9 Correct 6 ms 1972 KB Output is correct
10 Correct 7 ms 2252 KB Output is correct
11 Correct 6 ms 1996 KB Output is correct
12 Correct 6 ms 2636 KB Output is correct
13 Correct 5 ms 1996 KB Output is correct
14 Correct 5 ms 1996 KB Output is correct
15 Correct 78 ms 45748 KB Output is correct
16 Correct 8 ms 4684 KB Output is correct
17 Correct 25 ms 11728 KB Output is correct
18 Correct 24 ms 13980 KB Output is correct
19 Correct 96 ms 52988 KB Output is correct
20 Correct 75 ms 42744 KB Output is correct
21 Correct 45 ms 28748 KB Output is correct
22 Correct 44 ms 28332 KB Output is correct
23 Correct 30 ms 16564 KB Output is correct
24 Correct 137 ms 72388 KB Output is correct
25 Correct 95 ms 54184 KB Output is correct
26 Correct 118 ms 61912 KB Output is correct
27 Correct 145 ms 69700 KB Output is correct