結果

問題 No.2605 Pickup Parentheses
ユーザー risujirohrisujiroh
提出日時 2024-01-12 22:15:05
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 153 ms / 2,000 ms
コード長 8,088 bytes
コンパイル時間 4,860 ms
コンパイル使用メモリ 304,080 KB
実行使用メモリ 33,412 KB
最終ジャッジ日時 2024-09-30 06:25:30
合計ジャッジ時間 8,505 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
10,988 KB
testcase_01 AC 14 ms
10,944 KB
testcase_02 AC 13 ms
11,008 KB
testcase_03 AC 14 ms
11,136 KB
testcase_04 AC 13 ms
11,136 KB
testcase_05 AC 13 ms
11,116 KB
testcase_06 AC 14 ms
11,244 KB
testcase_07 AC 14 ms
11,140 KB
testcase_08 AC 15 ms
11,236 KB
testcase_09 AC 13 ms
11,052 KB
testcase_10 AC 14 ms
11,136 KB
testcase_11 AC 14 ms
11,136 KB
testcase_12 AC 14 ms
11,116 KB
testcase_13 AC 14 ms
10,876 KB
testcase_14 AC 14 ms
11,136 KB
testcase_15 AC 14 ms
11,264 KB
testcase_16 AC 14 ms
11,008 KB
testcase_17 AC 14 ms
11,088 KB
testcase_18 AC 71 ms
17,692 KB
testcase_19 AC 41 ms
14,336 KB
testcase_20 AC 19 ms
11,792 KB
testcase_21 AC 16 ms
11,528 KB
testcase_22 AC 14 ms
11,264 KB
testcase_23 AC 70 ms
17,484 KB
testcase_24 AC 18 ms
11,644 KB
testcase_25 AC 87 ms
19,428 KB
testcase_26 AC 88 ms
20,220 KB
testcase_27 AC 62 ms
16,804 KB
testcase_28 AC 17 ms
11,072 KB
testcase_29 AC 19 ms
11,840 KB
testcase_30 AC 30 ms
13,416 KB
testcase_31 AC 15 ms
11,120 KB
testcase_32 AC 13 ms
10,948 KB
testcase_33 AC 52 ms
15,912 KB
testcase_34 AC 17 ms
11,264 KB
testcase_35 AC 14 ms
11,008 KB
testcase_36 AC 16 ms
11,140 KB
testcase_37 AC 44 ms
14,776 KB
testcase_38 AC 25 ms
12,532 KB
testcase_39 AC 17 ms
11,552 KB
testcase_40 AC 45 ms
14,840 KB
testcase_41 AC 43 ms
14,604 KB
testcase_42 AC 56 ms
16,092 KB
testcase_43 AC 22 ms
12,132 KB
testcase_44 AC 20 ms
12,160 KB
testcase_45 AC 14 ms
11,008 KB
testcase_46 AC 15 ms
11,348 KB
testcase_47 AC 13 ms
11,064 KB
testcase_48 AC 14 ms
11,104 KB
testcase_49 AC 40 ms
13,920 KB
testcase_50 AC 23 ms
12,092 KB
testcase_51 AC 14 ms
11,064 KB
testcase_52 AC 27 ms
12,928 KB
testcase_53 AC 32 ms
13,456 KB
testcase_54 AC 23 ms
12,388 KB
testcase_55 AC 27 ms
12,928 KB
testcase_56 AC 16 ms
11,520 KB
testcase_57 AC 13 ms
10,948 KB
testcase_58 AC 153 ms
33,412 KB
testcase_59 AC 74 ms
18,184 KB
testcase_60 AC 86 ms
18,912 KB
testcase_61 AC 90 ms
19,308 KB
testcase_62 AC 98 ms
20,548 KB
testcase_63 AC 91 ms
19,752 KB
testcase_64 AC 101 ms
22,352 KB
testcase_65 AC 78 ms
18,532 KB
testcase_66 AC 135 ms
29,444 KB
testcase_67 AC 86 ms
19,236 KB
testcase_68 AC 13 ms
11,064 KB
testcase_69 AC 14 ms
11,008 KB
testcase_70 AC 13 ms
11,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#if __INCLUDE_LEVEL__ == 0

#include __BASE_FILE__

namespace {

using Fp = atcoder::modint998244353;

Comb<Fp> comb(1e6);

void solve() {
  int n, m;
  scan(n, m);
  std::vector<int> l(m), r(m);
  for (const int i : rep(m)) {
    scan(l[i], r[i]);
    --l[i];
  }
  if (n & 1) {
    print(0);
    return;
  }

  const auto catalan = [&](int n) { return comb.binom(n * 2, n) * comb.recip(n + 1); };

  std::vector<std::vector<Fp>> q;
  q.push_back({1});
  for (const int i : rep(m)) {
    if ((r[i] - l[i]) & 1) {
      continue;
    }
    const int t = (r[i] - l[i]) / 2;
    q.emplace_back(t + 1);
    q.back()[0] += 1;
    q.back()[t] -= catalan(t);
  }

  for (int qi = 0; qi + 2 <= len(q); qi += 2) {
    q.push_back(atcoder::convolution(q[qi], q[qi + 1]));
  }

  Fp ans = 0;
  for (const int i : rep(len(q.back()))) {
    ans += catalan(n / 2 - i) * q.back()[i];
  }

  print(ans);
}

}  // namespace

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  solve();
}

#else  // __INCLUDE_LEVEL__

#include <bits/stdc++.h>

#include <atcoder/convolution>

template <class T>
std::vector<T> make_vector_for_overwrite(int n) {
  static_assert(std::is_trivially_destructible_v<T>);
  std::vector<T> v;
  static_assert(sizeof(v) == 3 * sizeof(T*));
  v.reserve(n);
  T*(&a)[3] = reinterpret_cast<T*(&)[3]>(v);
  assert(a[0] == a[1] && a[1] + n == a[2]);
  a[1] = a[2];
  return v;
}

template <class T>
class Comb {
 public:
  Comb() = default;

  explicit Comb(int max_n)
      : fact_(make_vector_for_overwrite<T>(max_n + 1)),
        recip_fact_(make_vector_for_overwrite<T>(max_n + 1)) {
    fact_[0] = 1;
    for (const int n : std::views::iota(1, max_n + 1)) {
      fact_[n] = fact_[n - 1] * n;
    }
    recip_fact_[max_n] = 1 / fact_[max_n];
    for (const int n : std::views::iota(1, max_n + 1) | std::views::reverse) {
      recip_fact_[n - 1] = n * recip_fact_[n];
    }
  }

  T recip(int n) const {
    assert(n);
    return n < 0 ? -recip(-n) : recip_fact_[n] * fact_[n - 1];
  }

  T fact(int n) const {
    assert(0 <= n);
    return fact_[n];
  }

  T recip_fact(int n) const { return n < 0 ? 0 : recip_fact_[n]; }

  T falling_fact(int n, int k) const {
    assert(0 <= n || n < k);
    if (n < 0) {
      const T t = falling_fact(k - n - 1, k);
      return k & 1 ? -t : t;
    }
    return n < k ? 0 : recip_fact(n - k) * fact(n);
  }

  T recip_falling_fact(int n, int k) const {
    assert(n < 0 || k <= n);
    return falling_fact(n - k, -k);
  }

  T rising_fact(int n, int k) const {
    assert(n <= 0 || 0 < n + k);
    return falling_fact(n + k - 1, k);
  }

  T recip_rising_fact(int n, int k) const {
    assert(0 < n || n + k <= 0);
    return falling_fact(n - 1, -k);
  }

  T binom(int n, int k) const {
    if ((n < 0) ^ (k < 0) ^ (n < k)) {
      return 0;
    }
    if (n < 0 && k < 0) {
      k = n - k;
    }
    return recip_fact(k) * falling_fact(n, k);
  }

  T recip_binom(int n, int k) const {
    assert((0 <= n) ^ (0 <= k) ^ (k <= n));
    k = std::max(k, n - k);
    return recip_falling_fact(n, k) * fact(k);
  }

  T multiset(int n, int k) const { return binom(n + k - 1, k); }

  T recip_multiset(int n, int k) const {
    assert((0 < n) ^ (0 <= k) ^ (0 < n + k));
    return recip_binom(n + k - 1, k);
  }

 private:
  std::vector<T> fact_;
  std::vector<T> recip_fact_;
};

template <class T, class U = T>
bool chmin(T& x, U&& y) {
  return y < x && (x = std::forward<U>(y), true);
}

template <class T, class U = T>
bool chmax(T& x, U&& y) {
  return x < y && (x = std::forward<U>(y), true);
}

template <std::signed_integral T = int>
T inf() {
  T ret;
  std::memset(&ret, 0x3f, sizeof(ret));
  return ret;
}

template <std::floating_point T>
T inf() {
  return std::numeric_limits<T>::infinity();
}

template <class T>
concept Range = std::ranges::range<T> && !std::convertible_to<T, std::string_view>;

template <class T>
concept TupleLike = std::__is_tuple_like<T>::value && !Range<T>;

namespace std {

istream& operator>>(istream& is, Range auto&& r) {
  for (auto&& e : r) {
    is >> e;
  }
  return is;
}

istream& operator>>(istream& is, TupleLike auto&& t) {
  return apply([&](auto&... xs) -> istream& { return (is >> ... >> xs); }, t);
}

ostream& operator<<(ostream& os, Range auto&& r) {
  string_view sep = "";
  for (auto&& e : r) {
    os << exchange(sep, " ") << e;
  }
  return os;
}

ostream& operator<<(ostream& os, TupleLike auto&& t) {
  const auto f = [&](auto&... xs) -> ostream& {
    [[maybe_unused]] string_view sep = "";
    ((os << exchange(sep, " ") << xs), ...);
    return os;
  };
  return apply(f, t);
}

#define DEF_INC_OR_DEC(op) \
  auto& operator op(Range auto&& r) { \
    for (auto&& e : r) { \
      op e; \
    } \
    return r; \
  } \
  auto& operator op(TupleLike auto&& t) { \
    apply([](auto&... xs) { (op xs, ...); }, t); \
    return t; \
  }

DEF_INC_OR_DEC(++)
DEF_INC_OR_DEC(--)

#undef DEF_INC_OR_DEC

}  // namespace std

namespace atcoder {

template <class T, internal::is_modint_t<T>* = nullptr>
std::istream& operator>>(std::istream& is, T& x) {
  int v;
  is >> v;
  x = T::raw(v);
  return is;
}

template <class T, internal::is_modint_t<T>* = nullptr>
std::ostream& operator<<(std::ostream& os, const T& x) {
  return os << x.val();
}

}  // namespace atcoder

void scan(auto&&... xs) { std::cin >> std::tie(xs...); }
void print(auto&&... xs) { std::cout << std::tie(xs...) << '\n'; }

#define FWD(...) static_cast<decltype(__VA_ARGS__)&&>(__VA_ARGS__)

template <class F>
class fix {
 public:
  explicit fix(F f) : f_(std::move(f)) {}

  decltype(auto) operator()(auto&&... xs) const { return f_(std::ref(*this), FWD(xs)...); }

 private:
  F f_;
};

template <class T>
concept LambdaExpr = std::is_placeholder_v<std::remove_cvref_t<T>> != 0 ||
                     std::is_bind_expression_v<std::remove_cvref_t<T>>;

auto operator++(LambdaExpr auto&& x, int) {
  return std::bind([](auto&& x) -> decltype(auto) { return FWD(x)++; }, FWD(x));
}

auto operator--(LambdaExpr auto&& x, int) {
  return std::bind([](auto&& x) -> decltype(auto) { return FWD(x)--; }, FWD(x));
}

#define DEF_UNARY_OP(op) \
  auto operator op(LambdaExpr auto&& x) { \
    return std::bind([](auto&& x) -> decltype(auto) { return op FWD(x); }, FWD(x)); \
  }

DEF_UNARY_OP(++)
DEF_UNARY_OP(--)
DEF_UNARY_OP(+)
DEF_UNARY_OP(-)
DEF_UNARY_OP(~)
DEF_UNARY_OP(!)
DEF_UNARY_OP(*)
DEF_UNARY_OP(&)

#undef DEF_UNARY_OP

#define DEF_BINARY_OP(op) \
  template <class T1, class T2> \
    requires LambdaExpr<T1> || LambdaExpr<T2> \
  auto operator op(T1&& x, T2&& y) { \
    return std::bind([](auto&& x, auto&& y) -> decltype(auto) { return FWD(x) op FWD(y); }, \
                     FWD(x), FWD(y)); \
  }

DEF_BINARY_OP(+=)
DEF_BINARY_OP(-=)
DEF_BINARY_OP(*=)
DEF_BINARY_OP(/=)
DEF_BINARY_OP(%=)
DEF_BINARY_OP(^=)
DEF_BINARY_OP(&=)
DEF_BINARY_OP(|=)
DEF_BINARY_OP(<<=)
DEF_BINARY_OP(>>=)
DEF_BINARY_OP(+)
DEF_BINARY_OP(-)
DEF_BINARY_OP(*)
DEF_BINARY_OP(/)
DEF_BINARY_OP(%)
DEF_BINARY_OP(^)
DEF_BINARY_OP(&)
DEF_BINARY_OP(|)
DEF_BINARY_OP(<<)
DEF_BINARY_OP(>>)
DEF_BINARY_OP(==)
DEF_BINARY_OP(!=)
DEF_BINARY_OP(<)
DEF_BINARY_OP(>)
DEF_BINARY_OP(<=)
DEF_BINARY_OP(>=)
DEF_BINARY_OP(&&)
DEF_BINARY_OP(||)

#undef DEF_BINARY_OP

template <class T1, class T2>
  requires LambdaExpr<T1> || LambdaExpr<T2>
auto at(T1&& x, T2&& y) {
  return std::bind([](auto&& x, auto&& y) -> decltype(auto) { return FWD(x)[FWD(y)]; }, FWD(x),
                   FWD(y));
}

template <int I>
auto get(LambdaExpr auto&& x) {
  return std::bind([](auto&& x) -> decltype(auto) { return std::get<I>(FWD(x)); }, FWD(x));
}

inline auto rep(int l, int r) { return std::views::iota(std::min(l, r), r); }
inline auto rep(int n) { return rep(0, n); }
inline auto rep1(int l, int r) { return rep(l, r + 1); }
inline auto rep1(int n) { return rep(1, n + 1); }

using namespace std::literals;
using namespace std::placeholders;

namespace ranges = std::ranges;
namespace views = std::views;

using i64 = std::int64_t;

#define len(...) static_cast<int>(ranges::size(__VA_ARGS__))

#endif  // __INCLUDE_LEVEL__
0