QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#723923#4431. Interesting NumbersmaspyAC ✓674ms84156kbC++2324.6kb2024-11-08 03:40:052024-11-08 03:40:05

Judging History

你现在查看的是最新测评结果

  • [2024-11-08 03:40:05]
  • 评测
  • 测评结果:AC
  • 用时:674ms
  • 内存:84156kb
  • [2024-11-08 03:40:05]
  • 提交

answer

#line 1 "library/my_template.hpp"
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using pi = pair<ll, ll>;
using vi = vector<ll>;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;

template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#define vv(type, name, h, ...) \
  vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...)   \
  vector<vector<vector<type>>> name( \
      h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)       \
  vector<vector<vector<vector<type>>>> name( \
      a, vector<vector<vector<type>>>(       \
             b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

// https://trap.jp/post/1224/
#define FOR1(a) for (ll _ = 0; _ < ll(a); ++_)
#define FOR2(i, a) for (ll i = 0; i < ll(a); ++i)
#define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i)
#define FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c))
#define FOR1_R(a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR2_R(i, a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR3_R(i, a, b) for (ll i = (b)-1; i >= ll(a); --i)
#define FOR4_R(i, a, b, c) for (ll i = (b)-1; i >= ll(a); i -= (c))
#define overload4(a, b, c, d, e, ...) e
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FOR_R(...) \
  overload4(__VA_ARGS__, FOR4_R, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)

#define FOR_subset(t, s) for (ll t = s; t >= 0; t = (t == 0 ? -1 : (t - 1) & s))
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define elif else if

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define stoi stoll

template <typename T>
T SUM(vector<T> &A) {
  T sum = T(0);
  for (auto &&a: A) sum += a;
  return sum;
}

#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())

int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T, typename U>
T ceil(T x, U y) {
  return (x > 0 ? (x + y - 1) / y : x / y);
}

template <typename T, typename U>
T floor(T x, U y) {
  return (x > 0 ? x / y : (x - y + 1) / y);
}

template <typename T, typename U>
pair<T, T> divmod(T x, U y) {
  T q = floor(x, y);
  return {q, x - q * y};
}

ll binary_search(function<bool(ll)> check, ll ok, ll ng) {
  assert(check(ok));
  while (abs(ok - ng) > 1) {
    auto x = (ng + ok) / 2;
    if (check(x))
      ok = x;
    else
      ng = x;
  }
  return ok;
}

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

vi s_to_vi(const string &S, char first_char) {
  vi A(S.size());
  FOR(i, S.size()) { A[i] = S[i] - first_char; }
  return A;
}

template <typename T>
vector<T> cumsum(vector<T> &A, int off = 1) {
  int N = A.size();
  vector<T> B(N + 1);
  FOR(i, N) { B[i + 1] = B[i] + A[i]; }
  if (off == 0) B.erase(B.begin());
  return B;
}

template <typename CNT, typename T>
vc<CNT> bincount(const vc<T> &A, int size) {
  vc<CNT> C(size);
  for (auto &&x: A) { ++C[x]; }
  return C;
}

template <typename T>
vector<int> argsort(const vector<T> &A) {
  // stable
  vector<int> ids(A.size());
  iota(all(ids), 0);
  sort(all(ids),
       [&](int i, int j) { return A[i] < A[j] || (A[i] == A[j] && i < j); });
  return ids;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vc<int> &I) {
  int n = len(A);
  assert(len(I) == n);
  vc<T> B(n);
  FOR(i, n) B[i] = A[I[i]];
  return B;
}
#line 1 "library/other/io.hpp"
// based on yosupo's fastio
#include <unistd.h>

namespace detail {
template <typename T, decltype(&T::is_modint) = &T::is_modint>
std::true_type check_value(int);
template <typename T>
std::false_type check_value(long);
} // namespace detail

template <typename T>
struct is_modint : decltype(detail::check_value<T>(0)) {};
template <typename T>
using is_modint_t = enable_if_t<is_modint<T>::value>;
template <typename T>
using is_not_modint_t = enable_if_t<!is_modint<T>::value>;

struct Scanner {
  FILE *fp;
  char line[(1 << 15) + 1];
  size_t st = 0, ed = 0;
  void reread() {
    memmove(line, line + st, ed - st);
    ed -= st;
    st = 0;
    ed += fread(line + ed, 1, (1 << 15) - ed, fp);
    line[ed] = '\0';
  }
  bool succ() {
    while (true) {
      if (st == ed) {
        reread();
        if (st == ed) return false;
      }
      while (st != ed && isspace(line[st])) st++;
      if (st != ed) break;
    }
    if (ed - st <= 50) {
      bool sep = false;
      for (size_t i = st; i < ed; i++) {
        if (isspace(line[i])) {
          sep = true;
          break;
        }
      }
      if (!sep) reread();
    }
    return true;
  }
  template <class T, enable_if_t<is_same<T, string>::value, int> = 0>
  bool read_single(T &ref) {
    if (!succ()) return false;
    while (true) {
      size_t sz = 0;
      while (st + sz < ed && !isspace(line[st + sz])) sz++;
      ref.append(line + st, sz);
      st += sz;
      if (!sz || st != ed) break;
      reread();
    }
    return true;
  }
  template <class T, enable_if_t<is_integral<T>::value, int> = 0>
  bool read_single(T &ref) {
    if (!succ()) return false;
    bool neg = false;
    if (line[st] == '-') {
      neg = true;
      st++;
    }
    ref = T(0);
    while (isdigit(line[st])) { ref = 10 * ref + (line[st++] & 0xf); }
    if (neg) ref = -ref;
    return true;
  }
  template <class T, is_modint_t<T> * = nullptr>
  bool read_single(T &ref) {
    long long val = 0;
    bool f = read_single(val);
    ref = T(val);
    return f;
  }
  bool read_single(double &ref) {
    string s;
    if (!read_single(s)) return false;
    ref = std::stod(s);
    return true;
  }
  bool read_single(char &ref) {
    string s;
    if (!read_single(s) || s.size() != 1) return false;
    ref = s[0];
    return true;
  }
  template <class T>
  bool read_single(vector<T> &ref) {
    for (auto &d: ref) {
      if (!read_single(d)) return false;
    }
    return true;
  }
  template <class T, class U>
  bool read_single(pair<T, U> &p) {
    return (read_single(p.first) && read_single(p.second));
  }
  template <class A, class B, class C>
  bool read_single(tuple<A, B, C> &p) {
    return (read_single(get<0>(p)) && read_single(get<1>(p))
            && read_single(get<2>(p)));
  }
  template <class A, class B, class C, class D>
  bool read_single(tuple<A, B, C, D> &p) {
    return (read_single(get<0>(p)) && read_single(get<1>(p))
            && read_single(get<2>(p)) && read_single(get<3>(p)));
  }
  void read() {}
  template <class H, class... T>
  void read(H &h, T &... t) {
    bool f = read_single(h);
    assert(f);
    read(t...);
  }
  Scanner(FILE *fp) : fp(fp) {}
};

struct Printer {
  Printer(FILE *_fp) : fp(_fp) {}
  ~Printer() { flush(); }

  static constexpr size_t SIZE = 1 << 15;
  FILE *fp;
  char line[SIZE], small[50];
  size_t pos = 0;
  void flush() {
    fwrite(line, 1, pos, fp);
    pos = 0;
  }
  void write(const char &val) {
    if (pos == SIZE) flush();
    line[pos++] = val;
  }
  template <class T, enable_if_t<is_integral<T>::value, int> = 0>
  void write(T val) {
    if (pos > (1 << 15) - 50) flush();
    if (val == 0) {
      write('0');
      return;
    }
    if (val < 0) {
      write('-');
      val = -val; // todo min
    }
    size_t len = 0;
    while (val) {
      small[len++] = char(0x30 | (val % 10));
      val /= 10;
    }
    for (size_t i = 0; i < len; i++) { line[pos + i] = small[len - 1 - i]; }
    pos += len;
  }
  void write(const string &s) {
    for (char c: s) write(c);
  }
  void write(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++) write(s[i]);
  }
  void write(const double &x) {
    ostringstream oss;
    oss << setprecision(15) << x;
    string s = oss.str();
    write(s);
  }
  void write(const long double &x) {
    ostringstream oss;
    oss << setprecision(15) << x;
    string s = oss.str();
    write(s);
  }
  template <class T, is_modint_t<T> * = nullptr>
  void write(T &ref) {
    write(ref.val);
  }
  template <class T>
  void write(const vector<T> &val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
      if (i) write(' ');
      write(val[i]);
    }
  }
  template <class T, class U>
  void write(const pair<T, U> &val) {
    write(val.first);
    write(' ');
    write(val.second);
  }
  template <class A, class B, class C>
  void write(const tuple<A, B, C> &val) {
    auto &[a, b, c] = val;
    write(a), write(' '), write(b), write(' '), write(c);
  }
  template <class A, class B, class C, class D>
  void write(const tuple<A, B, C, D> &val) {
    auto &[a, b, c, d] = val;
    write(a), write(' '), write(b), write(' '), write(c), write(' '), write(d);
  }
  template <class A, class B, class C, class D, class E>
  void write(const tuple<A, B, C, D, E> &val) {
    auto &[a, b, c, d, e] = val;
    write(a), write(' '), write(b), write(' '), write(c), write(' '), write(d), write(' '), write(e);
  }
  template <class A, class B, class C, class D, class E, class F>
  void write(const tuple<A, B, C, D, E, F> &val) {
    auto &[a, b, c, d, e, f] = val;
    write(a), write(' '), write(b), write(' '), write(c), write(' '), write(d), write(' '), write(e), write(' '), write(f);
  }
  template <class T, size_t S>
  void write(const array<T, S> &val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
      if (i) write(' ');
      write(val[i]);
    }
  }
  void write(i128 val) {
    string s;
    bool negative = 0;
    if(val < 0){
      negative = 1;
      val = -val;
    }
    while (val) {
      s += '0' + int(val % 10);
      val /= 10;
    }
    if(negative) s += "-";
    reverse(all(s));
    if (len(s) == 0) s = "0";
    write(s);
  }
};

Scanner scanner = Scanner(stdin);
Printer printer = Printer(stdout);

void flush() { printer.flush(); }
void print() { printer.write('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
  printer.write(head);
  if (sizeof...(Tail)) printer.write(' ');
  print(forward<Tail>(tail)...);
}

void read() {}
template <class Head, class... Tail>
void read(Head &head, Tail &... tail) {
  scanner.read(head);
  read(tail...);
}

#define INT(...)   \
  int __VA_ARGS__; \
  read(__VA_ARGS__)
#define LL(...)   \
  ll __VA_ARGS__; \
  read(__VA_ARGS__)
#define STR(...)      \
  string __VA_ARGS__; \
  read(__VA_ARGS__)
#define CHAR(...)      \
  char __VA_ARGS__; \
  read(__VA_ARGS__)
#define DBL(...)      \
  double __VA_ARGS__; \
  read(__VA_ARGS__)

#define VEC(type, name, size) \
  vector<type> name(size);    \
  read(name)
#define VV(type, name, h, w)                     \
  vector<vector<type>> name(h, vector<type>(w)); \
  read(name)

void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
#line 1 "library/ds/mo.hpp"
struct Mo {
  vector<pair<int, int> > lr;

  void add(int l, int r) { /* [l, r) */
    lr.emplace_back(l, r);
  }

  template <typename AL, typename AR, typename EL, typename ER, typename O>
  void calc(const AL &add_left, const AR &add_right, const EL &erase_left,
            const ER &erase_right, const O &query) {
    int n = 1;
    for (auto &&[l, r]: lr) chmax(n, r);
    int q = (int)lr.size();
    int bs = n / min<int>(n, sqrt(q));
    vector<int> ord(q);
    iota(begin(ord), end(ord), 0);
    sort(begin(ord), end(ord), [&](int a, int b) {
      int ablock = lr[a].first / bs, bblock = lr[b].first / bs;
      if (ablock != bblock) return ablock < bblock;
      return (ablock & 1) ? lr[a].second > lr[b].second
                          : lr[a].second < lr[b].second;
    });
    int l = 0, r = 0;
    for (auto idx: ord) {
      while (l > lr[idx].first) add_left(--l);
      while (r < lr[idx].second) add_right(r++);
      while (l < lr[idx].first) erase_left(l++);
      while (r > lr[idx].second) erase_right(--r);
      query(idx);
    }
  }

  template <typename A, typename E, typename O>
  void calc(const A &add, const E &erase, const O &query) {
    calc(add, add, erase, erase, query);
  }
};
#line 1 "library/ds/fastset.hpp"
/* 64分木。
insert, erase
[]での存在判定
next, prev
*/
struct FastSet {
  using uint = unsigned;
  using ull = unsigned long long;

  int bsr(ull x) { return 63 - __builtin_clzll(x); }
  int bsf(ull x) { return __builtin_ctzll(x); }

  static constexpr uint B = 64;
  int n, lg;
  vc<vc<ull>> seg;
  FastSet(int _n) : n(_n) {
    do {
      seg.push_back(vc<ull>((_n + B - 1) / B));
      _n = (_n + B - 1) / B;
    } while (_n > 1);
    lg = int(seg.size());
  }
  bool operator[](int i) const { return (seg[0][i / B] >> (i % B) & 1) != 0; }
  void insert(int i) {
    for (int h = 0; h < lg; h++) {
      seg[h][i / B] |= 1ULL << (i % B);
      i /= B;
    }
  }
  void erase(int i) {
    for (int h = 0; h < lg; h++) {
      seg[h][i / B] &= ~(1ULL << (i % B));
      if (seg[h][i / B]) break;
      i /= B;
    }
  }

  // x以上最小の要素を返す。存在しなければ n。
  int next(int i) {
    for (int h = 0; h < lg; h++) {
      if (i / B == seg[h].size()) break;
      ull d = seg[h][i / B] >> (i % B);
      if (!d) {
        i = i / B + 1;
        continue;
      }
      // find
      i += bsf(d);
      for (int g = h - 1; g >= 0; g--) {
        i *= B;
        i += bsf(seg[g][i / B]);
      }
      return i;
    }
    return n;
  }

  // x以下最大の要素を返す。存在しなければ -1。
  int prev(int i) {
    if (i < 0) return -1;
    chmin(i, n - 1);
    for (int h = 0; h < lg; h++) {
      if (i == -1) break;
      ull d = seg[h][i / B] << (63 - i % 64);
      if (!d) {
        i = i / B - 1;
        continue;
      }
      // find
      i += bsr(d) - (B - 1);
      for (int g = h - 1; g >= 0; g--) {
        i *= B;
        i += bsr(seg[g][i / B]);
      }
      return i;
    }
    return -1;
  }

  // [l, r) 内の要素を全部集める
  vc<int> collect(int l, int r) {
    vc<int> res;
    int x = l - 1;
    while (1) {
      x = next(x + 1);
      if (x >= r) break;
      res.eb(x);
    }
    return res;
  }

  void debug() {
    string s;
    FOR(i, n) s += ((*this)[i] ? '1' : '0');
    print(s);
  }
};

// for mistype
using FaseSet = FastSet;
#line 2 "library/graph/base.hpp"

template <typename T>
struct Edge {
  int frm, to;
  T cost;
  int id;
};

template <typename T = int, bool directed = false>
struct Graph {
  int N, M;
  using cost_type = T;
  using edge_type = Edge<T>;
  vector<edge_type> edges;
  vector<int> indptr;
  vector<edge_type> csr_edges;
  bool prepared;

  class OutgoingEdges {
  public:
    OutgoingEdges(const Graph* G, int l, int r) : G(G), l(l), r(r) {}

    const edge_type* begin() const {
      if (l == r) { return 0; }
      return &G->csr_edges[l];
    }

    const edge_type* end() const {
      if (l == r) { return 0; }
      return &G->csr_edges[r];
    }

  private:
    int l, r;
    const Graph* G;
  };

  bool is_prepared() { return prepared; }
  constexpr bool is_directed() { return directed; }

  Graph() : N(0), M(0), prepared(0) {}
  Graph(int N) : N(N), M(0), prepared(0) {}

  void add(int frm, int to, T cost = 1, int i = -1) {
    assert(!prepared);
    assert(0 <= frm && 0 <= to && to < N);
    if (i == -1) i = M;
    auto e = edge_type({frm, to, cost, i});
    edges.eb(e);
    ++M;
  }

  // wt, off
  void read_tree(bool wt = false, int off = 1) { read_graph(N - 1, wt, off); }

  void read_graph(int M, bool wt = false, int off = 1) {
    FOR(M) {
      INT(a, b);
      a -= off, b -= off;
      if (!wt) {
        add(a, b);
      } else {
        T c;
        read(c);
        add(a, b, c);
      }
    }
    build();
  }

  void read_parent(int off = 1) {
    FOR3(v, 1, N) {
      INT(p);
      p -= off;
      add(p, v);
    }
    build();
  }

  void build() {
    assert(!prepared);
    prepared = true;
    indptr.assign(N + 1, 0);
    for (auto&& e: edges) {
      indptr[e.frm + 1]++;
      if (!directed) indptr[e.to + 1]++;
    }
    FOR(v, N) indptr[v + 1] += indptr[v];
    auto counter = indptr;
    csr_edges.resize(indptr.back() + 1);
    for (auto&& e: edges) {
      csr_edges[counter[e.frm]++] = e;
      if (!directed)
        csr_edges[counter[e.to]++] = edge_type({e.to, e.frm, e.cost, e.id});
    }
  }

  OutgoingEdges operator[](int v) const {
    assert(prepared);
    return {this, indptr[v], indptr[v + 1]};
  }

  void debug() {
    print("Graph");
    if (!prepared) {
      print("frm to cost id");
      for (auto&& e: edges) print(e.frm, e.to, e.cost, e.id);
    } else {
      print("indptr", indptr);
      print("frm to cost id");
      FOR(v, N) for (auto&& e: (*this)[v]) print(e.frm, e.to, e.cost, e.id);
    }
  }
};
#line 3 "library/graph/range_to_range_graph.hpp"

template <typename T>
struct Range_to_Range_Graph {
  int n;
  int n_node;
  vc<tuple<int, int, T>> edges;

  Range_to_Range_Graph(int n) : n(n), n_node(n * 3) {
    FOR3(i, 2, n + n) { edges.eb(to_upper_idx(i / 2), to_upper_idx(i), 0); }
    FOR3(i, 2, n + n) { edges.eb(to_lower_idx(i), to_lower_idx(i / 2), 0); }
  }

  inline int to_upper_idx(const int& i) {
    if (i >= n) return i - n;
    return n + i;
  }

  inline int to_lower_idx(const int& i) {
    if (i >= n) return i - n;
    return n + n + i;
  }

  void add(int frm, int to, T wt) { edges.eb(frm, to, wt); }

  void add_frm_range(int frm_l, int frm_r, int to, T wt) {
    int l = frm_l + n, r = frm_r + n;
    while (l < r) {
      if (l & 1) add(to_lower_idx(l++), to, wt);
      if (r & 1) add(to_lower_idx(--r), to, wt);
      l >>= 1, r >>= 1;
    }
  }

  void add_to_range(int frm, int to_l, int to_r, T wt) {
    int l = to_l + n, r = to_r + n;
    while (l < r) {
      if (l & 1) add(frm, to_upper_idx(l++), wt);
      if (r & 1) add(frm, to_upper_idx(--r), wt);
      l >>= 1, r >>= 1;
    }
  }

  void add_range_to_range(int frm_l, int frm_r, int to_l, int to_r, T wt) {
    int new_node = n_node++;
    add_frm_range(frm_l, frm_r, new_node, wt);
    add_to_range(new_node, to_l, to_r, T(0));
  }

  Graph<T, 1> build() {
    Graph<T, 1> G(n_node);
    for (auto&& [a, b, c]: edges) G.add(a, b, c);
    G.build();
    return G;
  }
};
#line 1 "library/flow/maxflow.hpp"
template <typename Cap>
struct MaxFlowGraph {
  struct Edge {
    int to, rev;
    Cap cap;
  };

  const Cap INF;
  int N;
  vvc<Edge> G;
  vc<int> prog, level;
  Cap flow_ans;
  bool calculated;

  MaxFlowGraph(int N, Cap INF) : INF(INF), N(N), calculated(0) {}

  void add(int frm, int to, Cap cap) {
    assert(0 <= frm && frm < N);
    assert(0 <= to && to < N);
    assert(Cap(0) <= cap);
    if (len(G) < N) G.resize(N);
    G[frm].eb(Edge{to, (int)G[to].size(), cap});
    G[to].eb(Edge{frm, (int)G[frm].size() - 1, 0});
  }

  Cap flow(int source, int sink) {
    if (calculated) return flow_ans;
    calculated = true;
    chmax(N, source + 1);
    chmax(N, sink + 1);
    G.resize(N);
    flow_ans = 0;
    while (set_level(source, sink)) {
      fill(all(prog), 0);
      prog.assign(N, 0);
      while (1) {
        Cap x = flow_dfs(source, sink, INF);
        if (x == 0) break;
        flow_ans += x;
        chmin(flow_ans, INF);
        if (flow_ans == INF) return flow_ans;
      }
    }
    return flow_ans;
  }

  // 最小カットの値および、カットを表す 01 列を返す
  pair<Cap, vc<int>> cut(int source, int sink) {
    Cap f = flow(source, sink);
    vc<int> res(N);
    FOR(v, N) res[v] = (level[v] >= 0 ? 0 : 1);
    return {f, res};
  }

  // 残余グラフの辺
  vc<tuple<int, int, Cap>> get_edges() {
    vc<tuple<int, int, Cap>> edges;
    FOR(v, N) for (auto&& e: G[v]) { edges.eb(v, e.to, e.cap); }
    return edges;
  }

private:
  bool set_level(int source, int sink) {
    level.assign(N, -1);
    level[source] = 0;
    queue<int> que;
    que.push(source);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (auto&& e: G[v]) {
        if (e.cap > 0 && level[e.to] == -1) {
          level[e.to] = level[v] + 1;
          if (e.to == sink) return true;
          que.push(e.to);
        }
      }
    }
    return false;
  }

  Cap flow_dfs(int v, int sink, Cap lim) {
    if (v == sink) return lim;
    Cap res = 0;
    for (int& i = prog[v]; i < (int)G[v].size(); ++i) {
      auto& e = G[v][i];
      if (e.cap > 0 && level[e.to] == level[v] + 1) {
        Cap a = flow_dfs(e.to, sink, min(lim, e.cap));
        if (a > 0) {
          e.cap -= a;
          G[e.to][e.rev].cap += a;
          res += a;
          lim -= a;
          if (lim == 0) break;
        }
      }
    }
    return res;
  }
};
#line 1 "library/other/xor_range.hpp"
// lo <= a ^ x < hi となる x の区間 [li, ri) たちを返す
vc<pi> xor_range(ll a, ll lo, ll hi) {
  vc<pi> res;
  FOR(k, 64) {
    if (lo == hi) break;
    ll b = 1LL << k;
    if (lo & b) {
      res.eb((lo ^ a), (lo ^ a) + b);
      lo += b;
    }
    if (hi & b) {
      res.eb((hi - b) ^ a, ((hi - b) ^ a) + b);
      hi -= b;
    }
    if (a & b) a ^= b;
  }
  return res;
}
#line 8 "main.cpp"

void solve() {
  LL(N, LIM);
  VEC(ll, A, N);

  auto f = [&](auto& dfs, vi A, int k) -> ll {
    if (len(A) == 0) return 0;
    // A[i] < 2^{k+1} が分かっている
    // xor <= k となるようになるべくたくさん選ぶ
    if ((1 << (k + 1)) - 1 <= LIM) return len(A);
    if ((1 << k) > LIM) {
      // それぞれ解く
      vi B, C;
      for (auto&& x: A) {
        if (x & 1 << k)
          B.eb(x ^ (1 << k));
        else
          C.eb(x);
      }
      return max(dfs(dfs, B, k - 1), dfs(dfs, C, k - 1));
    }
    // 補グラフで最大独立集合。補グラフは二部グラフなので、最大マッチングで解ける。
    // a^b > LIM に辺を張って、最大マッチング。
    sort(all(A));
    Range_to_Range_Graph<int> RGG(len(A) + 2);
    int source = len(A), sink = len(A) + 1;
    FOR(i, len(A)) {
      ll a = A[i];
      if (a >= (1 << k)) break;
      for (auto&& [l, r]: xor_range(a, LIM + 1, 1 << (k + 1))) {
        l = LB(A, l);
        r = LB(A, r);
        RGG.add_to_range(i, l, r, 1);
      }
    }
    FOR(i, len(A)) {
      ll a = A[i];
      if (a < (1 << k))
        RGG.add(source, i, 1);
      else
        RGG.add(i, sink, 1);
    }
    auto tmp = RGG.build();
    int INF = 1 << 30;
    MaxFlowGraph<int> G(tmp.N, INF);
    for (auto&& e: tmp.edges) {
      if (e.cost == 0) {
        G.add(e.frm, e.to, INF);
      } else {
        G.add(e.frm, e.to, 1);
      }
    }
    ll match = G.flow(source, sink);
    return len(A) - match;
  };
  ll ANS = f(f, A, 20);
  print(ANS);
}

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << setprecision(15);

  LL(T);
  // ll T = 1;
  FOR(T) solve();

  return 0;
}

详细

Test #1:

score: 100
Accepted
time: 674ms
memory: 84156kb

input:

6
30000 887788
295744 887428 33604 327716 100488 363104 299908 329892 100808 34952 557256 100996 821280 334560 68216 854944 297148 960 362788 68096 622628 625250 788548 360737 328264 524932 592672 361026 8780 164004 327808 262656 297024 624772 98376 395652 263052 524404 224 623244 344616 622944 2976...

output:

19915
18057
25682
14945
15025
3880

result:

ok 6 lines

Test #2:

score: 0
Accepted
time: 287ms
memory: 73356kb

input:

203
200 83
451 494 342 47 98 470 991 145 83 626 484 517 377 169 388 754 386 651 620 637 565 827 559 214 591 315 113 754 405 307 72 699 674 399 369 19 1009 42 932 349 737 286 834 391 982 647 927 515 840 110 239 454 350 775 457 728 290 78 610 890 51 127 410 316 957 571 180 803 383 308 335 738 281 921 ...

output:

24
21
12
54
105
66
33
19
112
36
12
13
12
56
63
101
20
19
100
19
136
30
24
11
62
59
23
16
18
36
59
17
17
18
33
120
34
15
18
17
32
14
56
14
32
103
55
123
57
11
11
37
30
19
20
60
30
110
64
14
11
36
12
109
20
55
106
24
44
56
55
33
19
36
120
102
34
20
64
112
19
11
21
12
134
39
10
11
65
34
100
110
12
52
1...

result:

ok 203 lines

Test #3:

score: 0
Accepted
time: 19ms
memory: 3948kb

input:

1000
20 20
451 494 342 47 98 470 991 145 83 626 484 517 377 169 388 754 386 651 620 637
20 99
203 85 581 483 268 875 150 167 84 678 371 799 409 531 552 987 604 6 791 609
20 169
391 982 647 927 515 840 110 239 454 350 775 457 728 290 78 610 890 51 127 410
20 83
798 759 347 702 842 251 936 193 359 784...

output:

2
3
4
4
5
3
2
5
2
5
1
2
10
19
3
4
2
4
3
1
7
3
2
4
2
14
1
3
6
2
6
2
1
6
9
2
7
2
5
6
2
3
3
6
11
5
1
8
7
2
5
2
10
4
1
13
5
12
3
4
17
12
5
3
2
3
6
5
4
3
4
2
3
4
7
3
3
2
11
2
6
1
4
5
8
2
6
2
3
3
9
18
6
11
2
7
9
7
2
3
3
5
2
3
10
5
14
4
2
1
2
14
9
2
7
2
3
2
11
4
7
6
2
2
2
7
8
2
7
3
2
11
3
2
3
2
1
2
12
4
2
...

result:

ok 1000 lines

Extra Test:

score: 0
Extra Test Passed