結果

問題 No.2409 Strange Werewolves
ユーザー い
提出日時 2023-08-27 10:24:57
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 10,936 bytes
コンパイル時間 3,527 ms
コンパイル使用メモリ 270,336 KB
実行使用メモリ 19,616 KB
最終ジャッジ日時 2024-06-08 06:02:11
合計ジャッジ時間 4,289 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 20 ms
17,280 KB
testcase_04 AC 23 ms
19,488 KB
testcase_05 AC 23 ms
19,616 KB
testcase_06 AC 17 ms
15,232 KB
testcase_07 AC 14 ms
13,312 KB
testcase_08 AC 10 ms
10,112 KB
testcase_09 AC 8 ms
8,320 KB
testcase_10 AC 5 ms
6,400 KB
testcase_11 AC 4 ms
5,376 KB
testcase_12 AC 6 ms
6,016 KB
testcase_13 AC 9 ms
8,960 KB
testcase_14 AC 10 ms
10,880 KB
testcase_15 AC 11 ms
11,008 KB
testcase_16 AC 13 ms
11,824 KB
testcase_17 AC 4 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using i128 = __int128_t;

template <typename F>
using fn = function<F>;

#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()

template <class A>
int len(const A &a) {
  return a.size();
}

#define eb emplace_back
#define pb push_back
#define elif else if

template <typename T>
using vec = vector<T>;
template <typename T>
using vec2 = vec<vec<T>>;
template <typename T>
using vec3 = vec<vec2<T>>;
template <typename T>
using vec4 = vec<vec3<T>>;
template <typename T>
using vec5 = vec<vec4<T>>;

#define VEC(T, a, ...) \
  vec<T> a(__VA_ARGS__)

#define VEC2(T, a, n, ...) \
  vector a(n, vec<T>(__VA_ARGS__));

#define VEC3(T, a, n, m, ...) \
  vector a( \
    n, \
    vector(m, vec<T>(__VA_ARGS__)) \
  );

#define VEC4(T, a, n, m, l, ...) \
  vector a( \
    n, \
    vector( \
      m, \
      vector(l, vec<T>(__VA_ARGS__)) \
    ) \
  );

#define eval_4(a, b, c, d, e, ...) e

#define loop while (1)

#define rep(n) \
  for (int __ = 0; __ < n; __++)

#define range_1(i, n) \
  for (int i = 0; i < n; i++)
#define range_2(i, a, b) \
  for (ll i = a; i < b; i++)
#define range_3(i, a, b, c) \
  for (ll i = a; i < b; i += c)

#define range(...) \
  eval_4(__VA_ARGS__, range_3, range_2, range_1, rep)( \
    __VA_ARGS__ \
  )

#define ranger_1(i, n) \
  for (int i = n; i-- > 0;)
#define ranger_2(i, a, b) \
  for (ll i = b; i-- > a;)
#define ranger_3(i, a, b, c) \
  for (ll i = b - 1; i >= a; i -= c)

#define range_rev(...) \
  eval_4(__VA_ARGS__, ranger_3, ranger_2, ranger_1)( \
    __VA_ARGS__ \
  )

#define iter(x, a) \
  for (const auto &x : a)
#define iter_mut(x, a) \
  for (auto &&x : a)

template <typename T, typename U>
istream &
operator>>(istream &in, pair<T, U> &p) {
  return in >> p.first >> p.second;
}

template <typename T, typename U>
ostream &operator<<(
  ostream &out,
  pair<T, U> &p
) {
  out << p.first << ' ' << p.second;
  return out;
}

template <int k = 0, class T>
void read_tup(istream &in, T &x) {
  if constexpr (tuple_size<T>::value > k) {
    in >> get<k>(x);
    read_tup<k + 1>(in, x);
  }
}

template <class... T>
istream &operator>>(
  istream &in,
  tuple<T...> &x
) {
  read_tup(in, x);
  return in;
}

template <int k = 0, class T>
void out_tup(ostream &out, T &x) {
  if constexpr (tuple_size<T>::value > k) {
    if constexpr (k > 0) {
      out << ' ';
    }
    out << get<k>(x);
    out_tup<k + 1>(out, x);
  }
}

template <class... T>
ostream &operator<<(
  ostream &out,
  tuple<T...> &x
) {
  out_tup(out, x);
  return out;
}

template <typename T>
auto operator<<(ostream &out, vec<T> a)
  -> ostream & {
  range(i, len(a)) {
    if (i) {
      out << ' ';
    }
    out << a[i];
  }
  return out;
}

template <typename T>
auto operator<<(ostream &out, vec2<T> a)
  -> ostream & {
  iter_mut(x, a) out << x << '\n';
  return out;
}

template <typename T>
auto operator>>(istream &in, vec<T> &a)
  -> istream & {
  iter_mut(x, a) in >> x;
  return in;
}

template <typename... T>
void in(T &...a) {
  (cin >> ... >> a);
}

template <class T, class... U>
void out(T a, const U... b) {
  cout << a;
  ((cout << ' ' << b), ...);
  cout << '\n';
}

template <typename T = int>
vec<T> iota(int n, T v = 0) {
  vec<int> a(n);
  std::iota(all(a), v);
  return a;
}

template <class T>
using max_queue = priority_queue<T>;

template <class T>
using min_queue =
  priority_queue<T, vec<T>, greater<T>>;

template <typename T>
T pop(queue<T> &q) {
  T v = q.front();
  q.pop();
  return v;
}

template <typename T>
T pop(deque<T> &q) {
  T v = q.front();
  q.pop_front();
  return v;
}

template <typename T>
T pop(vec<T> &q) {
  T v = q.back();
  q.pop_back();
  return v;
}

template <typename T>
T pop(max_queue<T> &q) {
  T v = q.top();
  q.pop();
  return v;
}

template <typename T>
T pop(min_queue<T> &q) {
  T v = q.top();
  q.pop();
  return v;
}

template <typename T>
T max(const vec<T> &a) {
  return *max_element(all(a));
}

template <typename T>
T min(const vec<T> &a) {
  return *min_element(all(a));
}

int topbit(int x) {
  return 31 - __builtin_clz(x);
}

template <class T>
bool operator==(
  const vec<T> &a,
  const vec<T> &b
) {
  int n = len(a);
  if (len(b) != n) {
    return false;
  }
  range(i, n) {
    if (a[i] != b[i]) {
      return false;
    }
  }
  return true;
}

template <class T, class U>
bool chmin(T &a, const U &b) {
  return b < a ? a = b, 1 : 0;
}

template <class T, class U>
bool chmax(T &a, const U &b) {
  return b > a ? a = b, 1 : 0;
}

int popcnt(int x) {
  return __builtin_popcount(x);
}

template <class T, class U>
T sum(const vec<U> &a) {
  return accumulate(all(a), 0ll);
}

template <class T>
void unique(vec<T> &a) {
  sort(all(a));
  a.erase(std::unique(all(a)), a.end());
}

template <class T, class A>
int lb(const A &a, const T &x) {
  auto p = lower_bound(all(a), x);
  return distance(a.begin(), p);
}

template <class T, class A>
int ub(const A &a, const T &x) {
  auto p = upper_bound(all(a), x);
  return distance(a.begin(), p);
}

template <class A>
vec<int> argsort(const A &a) {
  int n = len(a);
  auto b = iota(n);
  sort(all(b), [&](int i, int j) {
    return a[i] < a[j];
  });
  return b;
}

template <class T>
int ctz(T n) {
  return __builtin_ctzll(n);
}

template <typename T>
auto divmod(T a, T b) -> pair<T, T> {
  T q = a / b;
  return {q, a - q * b};
}

#ifdef DEBUG
#define dbg(...) out(__VA_ARGS__);
#else
#define dbg(...) ;
#endif

#ifdef DEBUG
#define dbg_assert(...) \
  assert(__VA_ARGS__);
#else
#define dbg_assert(...) ;
#endif

// define yes/no
#define yesno(y, n) \
  void yes(bool f = 1) { \
    out(f ? #y : #n); \
  } \
  void no() { \
    out(#n); \
  }

yesno(yes, no);

// yesno(Yes, No);
// yesno(YES, NO);

// const/runtime
// id <= 0, call set_mod
template <int id = 0>
class mint {
  ll v;

  static int mod;

  static constexpr int m() {
    return id > 0 ? id : mod;
  }

public:
  static void set_mod(int m) {
    assert(m > 0);
    mod = m;
  }

  constexpr mint(): v() {
  }

  mint(ll v): v(v) {
    norm();
  }

  void norm() {
    if (v < -m() || m() <= v) {
      v %= m();
    }
    if (v < 0) {
      v += m();
    }
  }

  int operator()() const {
    return v;
  }

  template <class T>
  explicit operator T() const {
    return static_cast<T>(v);
  }

  mint operator-() const {
    return v ? m() - v : 0;
  }

  mint &operator+=(const mint &a) {
    if ((v += a.v) >= m()) {
      v -= m();
    }
    return *this;
  }

  mint &operator-=(const mint &a) {
    if ((v -= a.v) < 0) {
      v += m();
    }
    return *this;
  }

  mint &operator*=(const mint &a) {
    v *= a.v;
    norm();
    return *this;
  }

  mint inv() const {
    int a = m(), b = v, u = 0, v = 1, q;
    while (b) {
      q = a / b;
      swap(u -= q * v, v);
      swap(a -= q * b, b);
    }
    return u;
  }

  mint pow(ll t) const {
    if (t < 0) {
      return inv().pow(-t);
    }
    mint y = 1, x(v);
    while (t) {
      if (t & 1) {
        y *= x;
      }
      x *= x;
      t >>= 1;
    }
    return y;
  }

  mint &operator/=(const mint &a) {
    return *this *= a.inv();
  }

  auto operator++() -> mint & {
    return *this += 1;
  }

  auto operator--() -> mint & {
    return *this -= 1;
  }

  auto operator++(int) -> mint {
    mint a(*this);
    *this += 1;
    return a;
  }

  auto operator--(int) -> mint {
    mint a(*this);
    *this -= 1;
    return a;
  }

  friend mint operator+(
    const mint &a,
    const mint &b
  ) {
    return mint(a) += b;
  }

  friend mint operator-(
    const mint &a,
    const mint &b
  ) {
    return mint(a) -= b;
  }

  friend mint operator*(
    const mint &a,
    const mint &b
  ) {
    return mint(a) *= b;
  }

  friend mint operator/(
    const mint &a,
    const mint &b
  ) {
    return mint(a) /= b;
  }

  friend bool operator==(
    const mint &a,
    const mint &b
  ) {
    return a.v == b.v;
  }

  friend istream &
  operator>>(istream &i, mint &x) {
    i >> x.v;
    x.norm();
    return i;
  }

  friend ostream &operator<<(
    ostream &o,
    const mint &x
  ) {
    return o << x.v;
  }
};

template <int id>
int mint<id>::mod = 1;

using mint107 = mint<1'000'000'007>;
using mint998 = mint<998'244'353>;
using mint_runtime = mint<0>;

template <class T>
T norm(T m, T x) {
  assert(m > 0);
  if (x < -m || x >= m) {
    x %= m;
  }
  if (x < 0) {
    x += m;
  }
  return x;
}

template <class T>
tuple<T, T, T> egcd(T a, T b) {
  if (!b) {
    if (a < 0) {
      return {-a, -1, 0};
    }
    return {a, 1, 0};
  };
  T q = a / b;
  auto [g, x, y] = egcd(b, a - b * q);
  return {g, y, x - q * y};
}

template <class T>
pair<T, T> ginv(T m, T a) {
  assert(0 < a && a < m);
  auto [g, x, _] = egcd(a, m);
  m /= g;
  if (x < 0) {
    x += m;
  }
  dbg_assert(0 <= x && x < m);
  return {g, x};
}

int inv(int m, int a) {
  auto [g, x] = ginv(m, norm(m, a));
  assert(g == 1);
  return x;
}

template <class T>
class tables {
  vec<T> f, fi, iv;

public:
  tables(int n = 1)
    : f{1},
      fi{1},
      iv{0} {
    expand(n);
  }

  void expand(int n) {
    int k = len(f);
    if (k >= n) {
      return;
    }
    // at lease double size
    // because freq resizing is slow
    chmax(n, k << 1);
    f.resize(n);
    fi.resize(n);
    iv.resize(n);
    range(i, k, n) {
      f[i] = f[i - 1] * i;
    }
    fi.back() = 1 / f.back();
    range_rev(i, k + 1, n) {
      fi[i - 1] = (ll)fi[i] * i;
      iv[i] = (ll)fi[i] * f[i - 1];
    }
    iv[k] = (ll)fi[k] * f[k - 1];
  }

  T fac(int n) {
    expand(n + 1);
    return f[n];
  }

  T ifac(int n) {
    expand(n + 1);
    return fi[n];
  }

  T inv(int n) {
    expand(n + 1);
    return iv[n];
  }
};

template <class T>
class comb {
  tables<T> t;

public:
  comb(int n = 1): t(n) {
  }

  auto f(int n) -> T {
    return t.fac(n);
  }

  auto fi(int n) -> T {
    return t.ifac(n);
  }

  auto inv(int n) -> T {
    return t.inv(n);
  }

  auto p(int n, int k) -> T {
    if (k < 0 || n < k) {
      return 0;
    }
    return f(n) * fi(n - k);
  }

  auto c(int n, int k) -> T {
    if (k < 0 || n < k) {
      return 0;
    }
    return p(n, k) * fi(k);
  }

  auto h(int n, int k) -> T {
    return c(n - 1 + k, k);
  }

  auto ip(int n, int k) -> T {
    assert(0 <= k && k <= n);
    return fi(n) * f(n - k);
  }

  auto ic(int n, int k) -> T {
    return ip(n, k) * f(k);
  }
};

constexpr int mod = 998'244'353;

void solve() {
  int x, y, z, w;
  in(x, y, z, w);
  chmax(z, 1);
  chmax(w, 1);
  using mint = mint998;
  comb<mint> f;
  // auto f = comb(1 << 20);
  mint res = f.f(x + y - z - w);
  res *= f.c(x, z);
  res *= f.c(y, w);
  out(res);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  // cout << setprecision(16);
  int t = 1;
  // in(t);
  while (t--) {
    solve();
  }
}
0