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

#include <bits/stdc++.h> // clang-format off
using Int = long long;
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef dump
#define dump(...)
#endif // clang-format on

/**
 *    author:  knshnb
 *    created: Sat Jun 13 21:41:36 JST 2020
 **/

template <class T, class S> T make_vec(S x) { return x; }
template <class T, class... Ts> auto make_vec(size_t n, Ts... ts) {
  return std::vector<decltype(make_vec<T>(ts...))>(n, make_vec<T>(ts...));
}

const Int B = 19;
signed main() {
  auto comb = make_vec<Int>(55, 55, 0);
  REP(i, 55 - 1) {
    comb[i][0] = 1;
    REP(j, i + 1) { comb[i + 1][j + 1] = comb[i][j] + comb[i][j + 1]; }
  }
  auto acc = make_vec<Int>(56, 56, 0);
  REP(i, 55) {
    REP(j, i + 1) { acc[i][j + 1] = acc[i][j] + comb[i][j]; }
  }
  auto calc = [&](Int n, Int r) {
    return std::max(0LL, acc[n][std::min(n, r) + 1] - 1);
  };
  Int n, K, s, t;
  std::cin >> n >> K >> s >> t;
  std::vector<Int> a(n);
  REP(i, n) std::cin >> a[i];
  std::vector<Int> bits;
  REP(k, B) {
    Int x = s >> k & 1, y = t >> k & 1;
    if (x == 1) {
      for (Int i = (Int)a.size() - 1; i >= 0; i--) {
        if ((a[i] >> k & 1) == 0)
          a.erase(a.begin() + i);
      }
    } else if (y == 0) {
      for (Int i = (Int)a.size() - 1; i >= 0; i--) {
        if ((a[i] >> k & 1) == 1)
          a.erase(a.begin() + i);
      }
    } else {
      bits.push_back(k);
    }
  }

  Int m = bits.size();
  std::vector<Int> b(a.size());
  REP(i, a.size()) {
    REP(j, m) { b[i] |= (a[i] >> bits[j] & 1) << j; }
  }

  __int128_t ans = 0;
  REP(S, 1 << m) {
    std::unordered_map<Int, Int> mp;
    for (Int x : b)
      mp[x & S]++;
    Int cnt = 0;
    for (auto &p : mp)
      cnt += calc(p.second, K);
    ans += cnt * (__builtin_popcount(S) % 2 ? -1 : 1);
  }
  std::cout << (Int)ans << std::endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h> // clang-format off
using Int = long long;
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef dump
#define dump(...)
#endif // clang-format on

/**
 *    author:  knshnb
 *    created: Sat Jun 13 21:41:36 JST 2020
 **/

template <class T, class S> T make_vec(S x) { return x; }
template <class T, class... Ts> auto make_vec(size_t n, Ts... ts) {
  return std::vector<decltype(make_vec<T>(ts...))>(n, make_vec<T>(ts...));
}

const Int B = 19;
signed main() {
  auto comb = make_vec<Int>(55, 55, 0);
  REP(i, 55 - 1) {
    comb[i][0] = 1;
    REP(j, i + 1) { comb[i + 1][j + 1] = comb[i][j] + comb[i][j + 1]; }
  }
  auto acc = make_vec<Int>(56, 56, 0);
  REP(i, 55) {
    REP(j, i + 1) { acc[i][j + 1] = acc[i][j] + comb[i][j]; }
  }
  auto calc = [&](Int n, Int r) {
    return acc[n][std::min(n, r) + 1] - 1;
    // Int ret = 0;
    // REP(i, 1, std::min(n + 1, r + 1)) ret += comb[n][i];
    // return ret;
  };
  // dump(comb);
  // dump(comb[1][1]);
  Int n, K, s, t;
  std::cin >> n >> K >> s >> t;
  std::vector<Int> a(n);
  REP(i, n) std::cin >> a[i];
  std::vector<Int> bits;
  REP(k, B) {
    Int x = s >> k & 1, y = t >> k & 1;
    if (x == 1 && y == 0) {
      std::cout << 0 << std::endl;
      return 0;
    }
    if (x == 1) {
      for (Int i = (Int)a.size() - 1; i >= 0; i--) {
        if ((a[i] >> k & 1) == 0)
          a.erase(a.begin() + i);
      }
    } else if (y == 0) {
      for (Int i = (Int)a.size() - 1; i >= 0; i--) {
        if ((a[i] >> k & 1) == 1)
          a.erase(a.begin() + i);
      }
    } else {
      bits.push_back(k);
    }
  }

  Int ans = calc(a.size(), K);
  dump(a, bits, ans);
  Int m = bits.size();
  std::vector<Int> dp(1 << m);
  REP(S, 1 << m) {
    for (int T = S;; T = (T - 1) & S) {
      Int cnt = 0;
      for (Int x : a) {
        bool ok = true;
        REP(i, m) {
          if ((S >> i & 1) == 0)
            continue;
          Int b = bits[i];
          if ((x >> b & 1) != (T >> i & 1))
            ok = false;
        }
        cnt += ok;
      }
      dump(S, T, cnt, calc(cnt, K));
      dp[S] += calc(cnt, K);
      if (T == 0)
        break;
    }
  }
  dump(dp);

  // REP(S, 1 << m) {
  //     ans -= dp[S] * (__builtin_popcount(S) % 2 ? -1 : 1);
  // }

  for (Int S = (1 << m) - 1; S >= 0; S--) {
    // Sの部分集合の列挙
    for (int T = S;; T = (T - 1) & S) {
      // 操作
      if (T != S)
        dp[T] -= dp[S];
      if (T == 0)
        break;
    }
  }
  dump(dp);

  ans -= std::accumulate(dp.begin() + 1, dp.end(), 0LL);
  std::cout << ans << std::endl;
}