Problem p02649 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
using ld = long double;
template <typename T, int n> using arr = T (&)[n];
template <typename T, int n> using c_arr = const T (&)[n];
template <typename T> using max_heap = std::priority_queue<T>;
template <typename T>
using min_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
constexpr int popcount(const ull v) { return v ? __builtin_popcountll(v) : 0; }
constexpr int log2p1(const ull v) { return v ? 64 - __builtin_clzll(v) : 0; }
constexpr int lsbp1(const ull v) { return __builtin_ffsll(v); }
constexpr int clog(const ull v) { return v ? log2p1(v - 1) : 0; }
constexpr ull ceil2(const ull v) { return 1ULL << clog(v); }
constexpr ull floor2(const ull v) {
  return v ? (1ULL << (log2p1(v) - 1)) : 0ULL;
}
constexpr bool btest(const ull mask, const int ind) {
  return (mask >> ind) & 1ULL;
}
template <typename T> void bset(T &mask, const int ind) {
  mask |= ((T)1 << ind);
}
template <typename T> void breset(T &mask, const int ind) {
  mask &= ~((T)1 << ind);
}
template <typename T> void bflip(T &mask, const int ind) {
  mask ^= ((T)1 << ind);
}
template <typename T> void bset(T &mask, const int ind, const bool b) {
  (b ? bset(mask, ind) : breset(mask, ind));
}
template <typename T> bool chmin(T &a, const T &b) {
  return (a > b ? a = b, true : false);
}
template <typename T> bool chmax(T &a, const T &b) {
  return (a < b ? a = b, true : false);
}

template <typename T> constexpr T inf_v = std::numeric_limits<T>::max() / 4;
template <typename Real>
constexpr Real pi_v = Real{3.141592653589793238462643383279502884};
constexpr ull TEN(const int n) { return n == 0 ? 1ULL : TEN(n - 1) * 10ULL; }
template <typename F> struct fix : F {
  fix(F &&f) : F{std::forward<F>(f)} {}
  template <typename... Args> auto operator()(Args &&...args) const {
    return F::operator()(*this, std::forward<Args>(args)...);
  }
};
class printer {
public:
  printer(std::ostream &os_ = std::cout) : os{os_} {}
  template <typename T> int operator()(const T &v) { return os << v, 0; }
  template <typename T> int operator()(const std::vector<T> &vs) {
    for (int i = 0; i < (int)vs.size(); i++) {
      os << (i ? " " : ""), this->operator()(vs[i]);
    }
    return 0;
  }
  template <typename T> int operator()(const std::vector<std::vector<T>> &vss) {
    for (int i = 0; i < (int)vss.size(); i++) {
      os << (0 <= i or i + 1 < (int)vss.size() ? "\\n" : ""),
          this->operator()(vss[i]);
    }
    return 0;
  }
  template <typename T, typename... Args>
  int operator()(const T &v, const Args &...args) {
    return this->operator()(v), os << ' ', this->operator()(args...), 0;
  }
  template <typename... Args> int ln(const Args &...args) {
    return this->operator()(args...), os << '\\n', 0;
  }
  template <typename... Args> int el(const Args &...args) {
    return this->operator()(args...), os << std::endl, 0;
  }
  template <typename... Args>
  int fmt(const std::string &s, const Args &...args) {
    return rec(s, 0, args...);
  }

private:
  int rec(const std::string &s, int index) {
    return os << s.substr(index, s.size()), 0;
  }
  template <typename T, typename... Args>
  int rec(const std::string &s, int index, const T &v, const Args &...args) {
    return index == s.size() ? 0
           : s[index] == '%' ? (this->operator()(v), rec(s, index + 1, args...))
                             : (os << s[index], rec(s, index + 1, v, args...));
  }
  std::ostream &os;
};
printer out;

template <typename T, typename F> std::vector<T> generated(const int n, F f) {
  std::vector<T> ans(n);
  return std::generate(ans.begin(), ans.end(), f), ans;
}
std::vector<int> ioted(const int n, const int offset = 0) {
  std::vector<int> ans(n);
  return std::iota(ans.begin(), ans.end(), offset), ans;
}
template <typename Vs> Vs reversed(const Vs &vs) {
  auto ans = vs;
  return std::reverse(ans.begin(), ans.end()), ans;
}
template <typename T, typename F = std::less<T>>
std::vector<T> sorted(const std::vector<T> &vs, F comp = F{}) {
  auto ans = vs;
  return std::sort(ans.begin(), ans.end(), comp), ans;
}
template <typename F>
std::vector<int> sorted_iota(const int n, F comp = F{}, const int offset = 0) {
  return sorted(ioted(n, offset), comp);
}
class scanner {
public:
  scanner(std::istream &is_ = std::cin) : is{is_} {
    is.tie(nullptr), std::ios::sync_with_stdio(false);
  }
  template <typename T> T val() {
    static T v;
    return is >> v, v;
  }
  template <typename T> T val(const T offset) { return val<T>() - offset; }
  template <typename T> std::vector<T> vec(const int n) {
    return generated<T>(n, [&] { return val<T>(); });
  }
  template <typename T> std::vector<T> vec(const int n, const T offset) {
    return generated<T>(n, [&] { return val<T>(offset); });
  }
  template <typename T>
  std::vector<std::vector<T>> vvec(const int n0, const int n1) {
    return generated<std::vector<T>>(n0, [&] { return vec<T>(n1); });
  }
  template <typename T>
  std::vector<std::vector<T>> vvec(const int n0, const int n1, const T offset) {
    return generated<std::vector<T>>(n0, [&] { return vec<T>(n1, offset); });
  }
  template <typename... Args> auto tup() {
    return std::tuple<std::decay_t<Args>...>{val<Args>()...};
  }
  template <typename... Args> auto tup(const Args &...offsets) {
    return std::tuple<std::decay_t<Args>...>{val<Args>(offsets)...};
  }

private:
  std::istream &is;
};
scanner in;
#define SHOW(...) static_cast<void>(0)

template <typename T, typename Int, int n, int i>
auto make_v(typename std::enable_if<(i == n), c_arr<Int, n>>::type,
            const T &v = T{}) {
  return v;
}
template <typename T, typename Int, int n, int i>
auto make_v(typename std::enable_if<(i < n), c_arr<Int, n>>::type szs,
            const T &v = T{}) {
  return std::vector<decltype(make_v<T, Int, n, i + 1>(szs, v))>(
      szs[i], make_v<T, Int, n, i + 1>(szs, v));
}
template <typename T, typename Int, int n>
auto make_v(c_arr<Int, n> szs, const T &t = T{}) {
  return make_v<T, Int, n, 0>(szs, t);
}

template <typename T> struct bfft {
  bfft() = delete;
  static void fzt(std::vector<T> &a, const bool upper) {
    const int sz = a.size();
    for (int i = 1; i < sz; i <<= 1) {
      for (int j = 0; j < sz; j++) {
        if ((j & i) == 0) {
          (upper ? a[j] : a[j | i]) += (upper ? a[j | i] : a[j]);
        }
      }
    }
  }
  static void fmt(std::vector<T> &a, const bool upper) {
    const int sz = a.size();
    for (int i = 1; i < sz; i <<= 1) {
      for (int j = 0; j < sz; j++) {
        if ((j & i) == 0) {
          (upper ? a[j] : a[j | i]) -= (upper ? a[j | i] : a[j]);
        }
      }
    }
  }
  static void fwt(std::vector<T> &a, const bool rev) {
    const int sz = a.size();
    for (int i = 1; i < sz; i <<= 1) {
      for (int j = 0; j < sz; j++) {
        if ((j & i) == 0) {
          const T x = a[j], y = a[j | i];
          a[j] = (rev ? (x + y) / T(2) : x + y),
          a[j | i] = (rev ? (x - y) / T(2) : x - y);
        }
      }
    }
  }
  static std::vector<T> and_convolute(const std::vector<T> &a,
                                      const std::vector<T> &b) {
    const int sz = ceil2(std::max(a.size(), b.size()));
    std::vector<T> A(sz), B(sz);
    for (int i = 0; i < a.size(); i++) {
      A[i] = a[i];
    }
    for (int i = 0; i < b.size(); i++) {
      B[i] = b[i];
    }
    fzt(A, true), fzt(B, true);
    for (int i = 0; i < sz; i++) {
      A[i] *= B[i];
    }
    return fmt(A, true), A;
  }
  static std::vector<T> or_convolute(const std::vector<T> &a,
                                     const std::vector<T> &b) {
    const int sz = ceil2(std::max(a.size(), b.size()));
    std::vector<T> A(sz), B(sz);
    for (int i = 0; i < a.size(); i++) {
      A[i] = a[i];
    }
    for (int i = 0; i < b.size(); i++) {
      B[i] = b[i];
    }
    fzt(A, false), fzt(B, false);
    for (int i = 0; i < sz; i++) {
      A[i] *= B[i];
    }
    return fmt(A, false), A;
  }
  static std::vector<T> xor_convolute(const std::vector<T> &a,
                                      const std::vector<T> &b) {
    const int sz = ceil2(std::max(a.size(), b.size()));
    std::vector<T> A(sz), B(sz);
    for (int i = 0; i < a.size(); i++) {
      A[i] = a[i];
    }
    for (int i = 0; i < b.size(); i++) {
      B[i] = b[i];
    }
    fwt(A, false), fwt(B, false);
    for (int i = 0; i < sz; i++) {
      A[i] *= B[i];
    }
    return fwt(A, true), A;
  }
};
int main() {
  constexpr int B = 18;
  const auto [N, K, S, T] = in.tup<int, int, uint, uint>();
  const auto as = in.vec<uint>(N);
  if ((S & T) != S) {
    return out.ln(0);
  }
  std::vector<uint> bs;
  for (int i = 0; i < N; i++) {
    if (((as[i] & T) == as[i]) and ((as[i] & S) == S)) {
      bs.push_back(as[i]);
    }
  }
  SHOW(bs);
  const int M = bs.size();

  auto prec = make_v<ll>({M + 1, K + 1}, 0);
  prec[0][0] = 1;
  for (int i = 0; i < M; i++) {
    for (int k = 0; k < K; k++) {
      prec[i + 1][k + 1] += prec[i][k];
    }
    for (int k = 0; k <= K; k++) {
      prec[i + 1][k] += prec[i][k];
    }
  }
  for (int i = 0; i <= M; i++) {
    for (int k = 1; k <= K; k++) {
      prec[i][k] += prec[i][k - 1];
    }
  }
  auto xors = make_v<ll>({M, 1 << B}, 0);
  for (int i = 0; i < M; i++) {
    for (int j = i + 1; j < M; j++) {
      xors[i][bs[i] ^ bs[j]]++;
    }
    bfft<ll>::fzt(xors[i], false);
  }
  auto dp = std::vector<ll>(1 << B, 0);
  for (uint m = 0; m < (1 << B); m++) { // 違反集合
    bool ng = false;
    for (int b = 0; b < B; b++) {
      if (btest(m, b)) {
        if (btest(S, b) == 1 or btest(T, b) == 0) {
          ng = true;
        }
      }
    }
    if (ng) {
      continue;
    }
    const uint cm = ((1 << B) - 1) - m;
    for (int i = 0; i < M; i++) {
      const int num = xors[i][cm];
      dp[m] += prec[num][K - 1];
    }
  }
  //    SHOW(dp);
  ll ans = 0;
  for (int m = 0; m < (1 << B); m++) {
    ans += (popcount(m) % 2 == 0 ? 1LL : -1LL) * dp[m];
  }
  out.ln(ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
using ld = long double;
template <typename T, int n> using arr = T (&)[n];
template <typename T, int n> using c_arr = const T (&)[n];
template <typename T> using max_heap = std::priority_queue<T>;
template <typename T>
using min_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
constexpr int popcount(const ull v) { return v ? __builtin_popcountll(v) : 0; }
constexpr int log2p1(const ull v) { return v ? 64 - __builtin_clzll(v) : 0; }
constexpr int lsbp1(const ull v) { return __builtin_ffsll(v); }
constexpr int clog(const ull v) { return v ? log2p1(v - 1) : 0; }
constexpr ull ceil2(const ull v) { return 1ULL << clog(v); }
constexpr ull floor2(const ull v) {
  return v ? (1ULL << (log2p1(v) - 1)) : 0ULL;
}
constexpr bool btest(const ull mask, const int ind) {
  return (mask >> ind) & 1ULL;
}
template <typename T> void bset(T &mask, const int ind) {
  mask |= ((T)1 << ind);
}
template <typename T> void breset(T &mask, const int ind) {
  mask &= ~((T)1 << ind);
}
template <typename T> void bflip(T &mask, const int ind) {
  mask ^= ((T)1 << ind);
}
template <typename T> void bset(T &mask, const int ind, const bool b) {
  (b ? bset(mask, ind) : breset(mask, ind));
}
template <typename T> bool chmin(T &a, const T &b) {
  return (a > b ? a = b, true : false);
}
template <typename T> bool chmax(T &a, const T &b) {
  return (a < b ? a = b, true : false);
}

template <typename T> constexpr T inf_v = std::numeric_limits<T>::max() / 4;
template <typename Real>
constexpr Real pi_v = Real{3.141592653589793238462643383279502884};
constexpr ull TEN(const int n) { return n == 0 ? 1ULL : TEN(n - 1) * 10ULL; }
template <typename F> struct fix : F {
  fix(F &&f) : F{std::forward<F>(f)} {}
  template <typename... Args> auto operator()(Args &&...args) const {
    return F::operator()(*this, std::forward<Args>(args)...);
  }
};
class printer {
public:
  printer(std::ostream &os_ = std::cout) : os{os_} {}
  template <typename T> int operator()(const T &v) { return os << v, 0; }
  template <typename T> int operator()(const std::vector<T> &vs) {
    for (int i = 0; i < (int)vs.size(); i++) {
      os << (i ? " " : ""), this->operator()(vs[i]);
    }
    return 0;
  }
  template <typename T> int operator()(const std::vector<std::vector<T>> &vss) {
    for (int i = 0; i < (int)vss.size(); i++) {
      os << (0 <= i or i + 1 < (int)vss.size() ? "\\n" : ""),
          this->operator()(vss[i]);
    }
    return 0;
  }
  template <typename T, typename... Args>
  int operator()(const T &v, const Args &...args) {
    return this->operator()(v), os << ' ', this->operator()(args...), 0;
  }
  template <typename... Args> int ln(const Args &...args) {
    return this->operator()(args...), os << '\\n', 0;
  }
  template <typename... Args> int el(const Args &...args) {
    return this->operator()(args...), os << std::endl, 0;
  }
  template <typename... Args>
  int fmt(const std::string &s, const Args &...args) {
    return rec(s, 0, args...);
  }

private:
  int rec(const std::string &s, int index) {
    return os << s.substr(index, s.size()), 0;
  }
  template <typename T, typename... Args>
  int rec(const std::string &s, int index, const T &v, const Args &...args) {
    return index == s.size() ? 0
           : s[index] == '%' ? (this->operator()(v), rec(s, index + 1, args...))
                             : (os << s[index], rec(s, index + 1, v, args...));
  }
  std::ostream &os;
};
printer out;

template <typename T, typename F> std::vector<T> generated(const int n, F f) {
  std::vector<T> ans(n);
  return std::generate(ans.begin(), ans.end(), f), ans;
}
std::vector<int> ioted(const int n, const int offset = 0) {
  std::vector<int> ans(n);
  return std::iota(ans.begin(), ans.end(), offset), ans;
}
template <typename Vs> Vs reversed(const Vs &vs) {
  auto ans = vs;
  return std::reverse(ans.begin(), ans.end()), ans;
}
template <typename T, typename F = std::less<T>>
std::vector<T> sorted(const std::vector<T> &vs, F comp = F{}) {
  auto ans = vs;
  return std::sort(ans.begin(), ans.end(), comp), ans;
}
template <typename F>
std::vector<int> sorted_iota(const int n, F comp = F{}, const int offset = 0) {
  return sorted(ioted(n, offset), comp);
}
class scanner {
public:
  scanner(std::istream &is_ = std::cin) : is{is_} {
    is.tie(nullptr), std::ios::sync_with_stdio(false);
  }
  template <typename T> T val() {
    static T v;
    return is >> v, v;
  }
  template <typename T> T val(const T offset) { return val<T>() - offset; }
  template <typename T> std::vector<T> vec(const int n) {
    return generated<T>(n, [&] { return val<T>(); });
  }
  template <typename T> std::vector<T> vec(const int n, const T offset) {
    return generated<T>(n, [&] { return val<T>(offset); });
  }
  template <typename T>
  std::vector<std::vector<T>> vvec(const int n0, const int n1) {
    return generated<std::vector<T>>(n0, [&] { return vec<T>(n1); });
  }
  template <typename T>
  std::vector<std::vector<T>> vvec(const int n0, const int n1, const T offset) {
    return generated<std::vector<T>>(n0, [&] { return vec<T>(n1, offset); });
  }
  template <typename... Args> auto tup() {
    return std::tuple<std::decay_t<Args>...>{val<Args>()...};
  }
  template <typename... Args> auto tup(const Args &...offsets) {
    return std::tuple<std::decay_t<Args>...>{val<Args>(offsets)...};
  }

private:
  std::istream &is;
};
scanner in;
#define SHOW(...) static_cast<void>(0)

template <typename T, typename Int, int n, int i>
auto make_v(typename std::enable_if<(i == n), c_arr<Int, n>>::type,
            const T &v = T{}) {
  return v;
}
template <typename T, typename Int, int n, int i>
auto make_v(typename std::enable_if<(i < n), c_arr<Int, n>>::type szs,
            const T &v = T{}) {
  return std::vector<decltype(make_v<T, Int, n, i + 1>(szs, v))>(
      szs[i], make_v<T, Int, n, i + 1>(szs, v));
}
template <typename T, typename Int, int n>
auto make_v(c_arr<Int, n> szs, const T &t = T{}) {
  return make_v<T, Int, n, 0>(szs, t);
}
int main() {
  constexpr int B = 28;
  const auto [N, K, S, T] = in.tup<int, int, uint, uint>();
  const auto as = in.vec<uint>(N);
  if ((S & T) != S) {
    return out.ln(0);
  }
  std::vector<uint> bs;
  for (int i = 0; i < N; i++) {
    if (((as[i] & T) == as[i]) and ((as[i] & S) == S)) {
      bs.push_back(as[i]);
    }
  }
  SHOW(bs);
  const int M = bs.size();
  auto dp = std::vector<ll>(1 << B, 0);
  for (int m = 0; m < (1 << B); m++) { // 違反集合
    bool ng = false;
    for (int b = 0; b < B; b++) {
      if (btest(m, b)) {
        if (btest(S, b) == 1 or btest(T, b) == 0) {
          ng = true;
        }
      }
    }
    if (ng) {
      continue;
    }
    for (int i = 0; i < M; i++) {
      std::vector<ll> idp(K + 1, 0);
      idp[1] = 1;
      for (int j = i + 1; j < M; j++) {
        for (int k = K - 1; k >= 0; k--) {
          bool usable = true;
          for (int b = 0; b < B; b++) {
            if (btest(m, b)) {
              if (btest(as[i], b) != btest(as[j], b)) {
                usable = false;
              }
            }
          }
          if (usable) {
            idp[k + 1] += idp[k];
          }
        }
      }
      SHOW(m, i, idp);
      for (int k = 0; k <= K; k++) {
        dp[m] += idp[k];
      }
    }
  }
  SHOW(dp);
  ll ans = 0;
  for (int m = 0; m < (1 << B); m++) {
    ans += (popcount(m) % 2 == 0 ? 1LL : -1LL) * dp[m];
  }
  out.ln(ans);
  return 0;
}