結果

問題 No.2206 Popcount Sum 2
コンテスト
ユーザー emthrm
提出日時 2026-09-21 19:20:15
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 673 ms / 4,000 ms
+ 485µs
コード長 7,527 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,466 ms
コンパイル使用メモリ 206,816 KB
実行使用メモリ 10,496 KB
最終ジャッジ日時 2026-09-21 19:20:29
合計ジャッジ時間 13,334 ms
ジャッジサーバーID
(参考情報)
judge5_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <algorithm>
#include <cassert>
#include <cmath>
#include <compare>
#include <iostream>
#include <numeric>
#include <utility>
#include <vector>

template <unsigned int M>
struct MInt {
  unsigned int v;

  constexpr MInt() : v(0) {}
  constexpr MInt(const long long x) : v(x >= 0 ? x % M : x % M + M) {}
  static constexpr MInt raw(const int x) {
    MInt x_;
    x_.v = x;
    return x_;
  }

  static constexpr int get_mod() { return M; }
  static constexpr void set_mod(const int divisor) {
    assert(std::cmp_equal(divisor, M));
  }

  static void init(const int x) {
    inv<true>(x);
    fact(x);
    fact_inv(x);
  }

  template <bool MEMOIZES = false>
  static MInt inv(const int n) {
    // assert(0 <= n && n < M && std::gcd(n, M) == 1);
    static std::vector<MInt> inverse{0, 1};
    const int prev = inverse.size();
    if (n < prev) return inverse[n];
    if constexpr (MEMOIZES) {
      // "n!" and "M" must be disjoint.
      inverse.resize(n + 1);
      for (int i = prev; i <= n; ++i) {
        inverse[i] = -inverse[M % i] * raw(M / i);
      }
      return inverse[n];
    }
    int u = 1, v = 0;
    for (unsigned int a = n, b = M; b;) {
      const unsigned int q = a / b;
      std::swap(a -= q * b, b);
      std::swap(u -= q * v, v);
    }
    return u;
  }

  static MInt fact(const int n) {
    static std::vector<MInt> factorial{1};
    if (const int prev = factorial.size(); n >= prev) {
      factorial.resize(n + 1);
      for (int i = prev; i <= n; ++i) {
        factorial[i] = factorial[i - 1] * i;
      }
    }
    return factorial[n];
  }

  static MInt fact_inv(const int n) {
    static std::vector<MInt> f_inv{1};
    if (const int prev = f_inv.size(); n >= prev) {
      f_inv.resize(n + 1);
      f_inv[n] = inv(fact(n).v);
      for (int i = n; i > prev; --i) {
        f_inv[i - 1] = f_inv[i] * i;
      }
    }
    return f_inv[n];
  }

  static MInt nCk(const int n, const int k) {
    if (n < 0 || n < k || k < 0) [[unlikely]] return MInt();
    return fact(n) * (n - k < k ? fact_inv(k) * fact_inv(n - k) :
                                  fact_inv(n - k) * fact_inv(k));
  }
  static MInt nPk(const int n, const int k) {
    return n < 0 || n < k || k < 0 ? MInt() : fact(n) * fact_inv(n - k);
  }
  static MInt nHk(const int n, const int k) {
    return n < 0 || k < 0 ? MInt() : (k == 0 ? 1 : nCk(n + k - 1, k));
  }

  static MInt large_nCk(long long n, const int k) {
    if (n < 0 || n < k || k < 0) [[unlikely]] return MInt();
    inv<true>(k);
    MInt res = 1;
    for (int i = 1; i <= k; ++i) {
      res *= inv(i) * n--;
    }
    return res;
  }

  constexpr MInt pow(long long exponent) const {
    MInt res = 1, tmp = *this;
    for (; exponent > 0; exponent >>= 1) {
      if (exponent & 1) res *= tmp;
      tmp *= tmp;
    }
    return res;
  }

  constexpr MInt& operator+=(const MInt& x) {
    if ((v += x.v) >= M) v -= M;
    return *this;
  }
  constexpr MInt& operator-=(const MInt& x) {
    if ((v += M - x.v) >= M) v -= M;
    return *this;
  }
  constexpr MInt& operator*=(const MInt& x) {
    v = (unsigned long long){v} * x.v % M;
    return *this;
  }
  MInt& operator/=(const MInt& x) { return *this *= inv(x.v); }

  constexpr auto operator<=>(const MInt& x) const = default;

  constexpr MInt& operator++() {
    if (++v == M) [[unlikely]] v = 0;
    return *this;
  }
  constexpr MInt operator++(int) {
    const MInt res = *this;
    ++*this;
    return res;
  }
  constexpr MInt& operator--() {
    v = (v == 0 ? M - 1 : v - 1);
    return *this;
  }
  constexpr MInt operator--(int) {
    const MInt res = *this;
    --*this;
    return res;
  }

  constexpr MInt operator+() const { return *this; }
  constexpr MInt operator-() const { return raw(v ? M - v : 0); }

  constexpr MInt operator+(const MInt& x) const { return MInt(*this) += x; }
  constexpr MInt operator-(const MInt& x) const { return MInt(*this) -= x; }
  constexpr MInt operator*(const MInt& x) const { return MInt(*this) *= x; }
  MInt operator/(const MInt& x) const { return MInt(*this) /= x; }

  friend std::ostream& operator<<(std::ostream& os, const MInt& x) {
    return os << x.v;
  }
  friend std::istream& operator>>(std::istream& is, MInt& x) {
    long long v;
    is >> v;
    x = MInt(v);
    return is;
  }
};

template <typename AddLeft, typename AddRight,
          typename DelLeft, typename DelRight>
struct Mo {
  explicit Mo(const std::vector<int>& ls, const std::vector<int>& rs,
              const AddLeft& add_left, const AddRight& add_right,
              const DelLeft& del_left, const DelRight& del_right)
      : n(ls.size()), ptr(0), nl(0), nr(0), ls(ls), rs(rs),
        add_left(add_left), add_right(add_right),
        del_left(del_left), del_right(del_right) {
    const int width = (
        n == 0 ? 1
               : std::max(std::llround(std::ranges::max(rs) / std::sqrt(n)),
                          1LL));
    order.resize(n);
    std::iota(order.begin(), order.end(), 0);
    std::sort(order.begin(), order.end(),
              [&ls, &rs, width](const int a, const int b) -> bool {
                  if (ls[a] / width != ls[b] / width) return ls[a] < ls[b];
                  return (ls[a] / width) & 1 ? rs[a] < rs[b] : rs[a] > rs[b];
              });
  }

  int process() {
    if (ptr == n) [[unlikely]] return -1;
    const int id = order[ptr++];
    while (ls[id] < nl) {
      const int idx = --nl;
      add_left(idx, nl, nr);
    }
    while (nr < rs[id]) {
      const int idx = nr++;
      add_right(idx, nl, nr);
    }
    while (nl < ls[id]) {
      const int idx = nl++;
      del_left(idx, nl, nr);
    }
    while (rs[id] < nr) {
      const int idx = --nr;
      del_right(idx, nl, nr);
    }
    return id;
  }

 private:
  const int n;
  int ptr, nl, nr;
  std::vector<int> ls, rs, order;
  AddLeft add_left;
  AddRight add_right;
  DelLeft del_left;
  DelRight del_right;
};

template <unsigned int T>
std::vector<MInt<T>> multipoint_binomial_prefix_sum(
    const std::vector<int>& ns, const std::vector<int>& ms) {
  using ModInt = MInt<T>;
  assert(ns.size() == ms.size());
  if (ns.empty()) [[unlikely]] return {};
  assert(T % 2 == 1);
  const int q = ns.size();
  for (int i = 0; i < q; ++i) {
    assert(0 <= ms[i] && ms[i] <= ns[i]);
  }
  ModInt::init(std::ranges::max(ns));
  ModInt sum = 1;
  Mo mo(ms, ns,
        // S(n, m) -> S(n, m - 1)
        [&sum](const int, const int m, const int n) {
          sum -= ModInt::nCk(n, m + 1);
        },
        // S(n, m) -> S(n + 1, m)
        [&sum](const int, const int m, const int n) {
          sum += sum - ModInt::nCk(n - 1, m);
        },
        // S(n, m) -> S(n, m + 1)
        [&sum](const int, const int m, const int n) {
          sum += ModInt::nCk(n, m);
        },
        // S(n, m) -> S(n - 1, m)
        [&sum](const int, const int m, const int n) {
          static const ModInt inv2 = ModInt::inv(2);
          sum = (sum + ModInt::nCk(n, m)) * inv2;
        });
  std::vector<ModInt> ans(q);
  for (int i = 0; i < q; ++i) {
    const int idx = mo.process();
    ans[idx] = sum;
  }
  return ans;
}

int main() {
  constexpr int MOD = 998244353;
  using ModInt = MInt<MOD>;
  int t;
  std::cin >> t;
  std::vector<int> n(t), m(t);
  for (int i = 0; i < t; ++i) {
    std::cin >> n[i] >> m[i];
    --n[i];
    --m[i];
  }
  const std::vector<ModInt> sums =
      multipoint_binomial_prefix_sum<MOD>(n, m);
  for (int i = 0; i < t; ++i) {
    std::cout << (ModInt::raw(2).pow(n[i] + 1) - 1) * sums[i] << '\n';
  }
  return 0;
}
0