結果

問題 No.2990 Interval XOR
ユーザー 👑 hos.lyrichos.lyric
提出日時 2024-12-15 00:10:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 6,744 bytes
コンパイル時間 1,438 ms
コンパイル使用メモリ 117,744 KB
実行使用メモリ 17,260 KB
最終ジャッジ日時 2024-12-15 00:10:20
合計ジャッジ時間 14,432 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 2 ms
5,248 KB
testcase_10 AC 2 ms
5,248 KB
testcase_11 AC 2 ms
5,248 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")

////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0U) {}
  constexpr ModInt(unsigned x_) : x(x_ % M) {}
  constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
  constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    assert(a == 1U); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////

constexpr unsigned MO = 998244353;
using Mint = ModInt<MO>;


// \prod (a0 x^p0 + a1 x^p1)
struct XorConvolve2Terms {
  int n;
  int base;
  vector<Mint> fss[2];
  XorConvolve2Terms() {}
  XorConvolve2Terms(int n_) : n(n_), base(0) {
    for (int u = 0; u < 2; ++u) fss[u].assign(1 << n, 1);
  }
  void add(int p0, int p1, Mint a0, Mint a1) {
    base ^= p0;
    fss[0][p0 ^ p1] *= (a0 + a1);
    fss[1][p0 ^ p1] *= (a0 - a1);
  }
  vector<Mint> run() {
    // *= fss[u][p] when |p \cap (higher bits)| == u (mod 2)
    for (int e = 0; e < n; ++e) {
      for (int o = 0; o < 1 << n; o += (1 << (e + 1))) for (int p0 = o; p0 < o + (1 << e); ++p0) {
        const int p1 = p0 | 1 << e;
        const Mint t00 = fss[0][p0];
        const Mint t01 = fss[0][p1];
        const Mint t10 = fss[1][p0];
        const Mint t11 = fss[1][p1];
        fss[0][p0] = t00 * t01;
        fss[0][p1] = t00 * t11;
        fss[1][p0] = t10 * t11;
        fss[1][p1] = t10 * t01;
      }
    }
    auto &fs = fss[0];
    for (int e = 0; e < n; ++e) {
      for (int o = 0; o < 1 << n; o += (1 << (e + 1))) for (int p0 = o; p0 < o + (1 << e); ++p0) {
        const int p1 = p0 | 1 << e;
        const Mint t0 = fs[p0];
        const Mint t1 = fs[p1];
        fs[p0] = t0 + t1;
        fs[p1] = t0 - t1;
      }
    }
    const Mint invTwo = Mint(1 << n).inv();
    for (int p = 0; p < 1 << n; ++p) fs[p] *= invTwo;
    for (int p = 0; p < 1 << n; ++p) if (p < (base ^ p)) swap(fs[p], fs[base ^ p]);
    return fs;
  }
};

vector<Mint> xorConvolveIntervalsLR(int n, const vector<int> &ls, const vector<int> &rs) {
  const int E = n;
  const int N = ls.size();
  const auto &A = ls;
  const auto &B = rs;
  Mint ans = 0;
  for (int e = 0; e < E; ++e) {
    // free bits: [0, e)
    const int mask = (1 << e) - 1;
    const int m = E - 1 - e;
    Mint way = 0;
    for (int phase = 0; phase < 2; ++phase) {
      // phase = 1: do not choose interval to make free b bits
      // mod (z^2 - 1)
      for (const int z : {+1, -1}) {
        // (high, weight) 2-taku
        XorConvolve2Terms f(m);
        for (int i = 0; i < N; ++i) {
          const int hb = B[i] >> (e + 1);
          const int ha = A[i] >> (e + 1);
          Mint wb = +Mint(B[i] & mask) * ((B[i] >> e & 1) ? z : 1);
          Mint wa = -Mint(A[i] & mask) * ((A[i] >> e & 1) ? z : 1);
          if (phase == 0) {
            if (B[i] >> e & 1) wb += +Mint(1 << e);
            if (A[i] >> e & 1) wa += -Mint(1 << e);
          }
          f.add(hb, ha, wb, wa);
        }
        way += (phase ? -1 : +1) * f.run()[0];
      }
    }
    way /= 2;
    way /= (1 << e);
    ans += way;
  }
  return {ans};
}

int N, M;
vector<int> L, R;

int main() {
  for (; ~scanf("%d%d", &N, &M); ) {
    L.resize(M);
    R.resize(M);
    for (int i = 0; i < M; ++i) {
      scanf("%d%d", &L[i], &R[i]);
      ++R[i];
    }
    
    const auto ans = xorConvolveIntervalsLR(N + 1, L, R);
    for (int h = 0; h < (int)ans.size(); ++h) {
      printf("%u\n", ans[h].x);
    }
  }
  return 0;
}
// library kakikake...
0