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

#include <bits/stdc++.h>

using namespace std;
using int64 = long long;

constexpr int DEBUG = 0;

constexpr int NUM_BITS = 18;

template <typename T>
vector<vector<T>> Make2DVector(int d1, int d2, T default_value) {
  return vector<vector<T>>(d1, vector<T>(d2, default_value));
}

int GetBit(int x, int b) { return (x >> b) & 1; }

// Vector
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  int len = v.size();
  s << "[";
  for (int i = 0; i < len; i++) {
    if (i > 0)
      s << ", ";
    s << v[i];
  }
  s << "]";
  return s;
}

int64 Solve() {
  int n, k, s, t;
  cin >> n >> k >> s >> t;

  vector<int> xs(n);
  for (int i = 0; i < n; i++) {
    cin >> xs[i];
  }

  auto c_table = Make2DVector<int64>(n + 1, n + 1, 0LL);
  for (int i = 0; i <= n; i++) {
    c_table[i][0] = 1;
    for (int j = 1; j <= i; j++) {
      c_table[i][j] = c_table[i - 1][j - 1] + c_table[i - 1][j];
    }
  }

  vector<int64> d_table(n + 1);
  for (int i = 0; i <= n; i++) {
    for (int j = 1; j <= min(i, k - 1); j++) {
      d_table[i] += c_table[i][j];
    }
  }

  vector<int> invalids(n);
  vector<int> bits;
  for (int b = 0; b < NUM_BITS; b++) {
    if (GetBit(s, b) == 1) {
      for (int i = 0; i < n; i++) {
        if (GetBit(xs[i], b) == 0) {
          invalids[i] = true;
        }
      }
    }
    if (GetBit(t, b) == 0) {
      for (int i = 0; i < n; i++) {
        if (GetBit(xs[i], b) == 1) {
          invalids[i] = true;
        }
      }
    }

    if (GetBit(s, b) == 0 && GetBit(t, b) == 1) {
      bits.push_back(b);
    }
  }

  int valid_count = 0;
  for (int i = 0; i < n; i++) {
    if (!invalids[i])
      valid_count++;
  }
  if (valid_count == 0)
    return 0;

  if (bits.size() == 0) {
    int64 r = 0;
    for (int i = 1; i <= min(valid_count, k); i++) {
      r += c_table[valid_count][i];
    }
    return r;
  }

  if (k == 1) {
    return 0;
  }

  // k >= 2
  vector<int> ys;
  for (int i = 0; i < n; i++) {
    if (invalids[i])
      continue;
    int x = xs[i];
    int y = 0;
    for (int j = 0; j < bits.size(); j++) {
      if (GetBit(x, bits[j])) {
        y |= 1 << j;
      }
    }
    ys.push_back(y);
  }

  // cout << "ys: " << ys << endl;

  int64 r = 0;
  for (int h = 0; h < ys.size(); h++) {
    vector<int> zs;
    for (int i = h + 1; i < ys.size(); i++) {
      zs.push_back(ys[i] ^ ys[h]);
    }

    int64 v = 0;
    for (int p = 0; p < 1 << bits.size(); p++) {
      if (p == 0)
        continue;
      // vector<int> zero_bits;
      int mask = 0;
      int bit_count = 0;
      for (int b = 0; b < bits.size(); b++) {
        if (GetBit(p, b)) {
          // zero_bits.push_back(b);
          mask |= 1 << b;
          bit_count++;
        }
      }

      int count = 0;
      for (int z : zs) {
        if (((mask ^ z) & mask) == mask) {
          count++;
        }
      }

      if (bit_count % 2 == 1) {
        v += d_table[count];
      } else {
        v -= d_table[count];
      }
    }

    r += d_table[zs.size()] - v;
  }
  return r;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cout << Solve() << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using int64 = long long;

constexpr int DEBUG = 0;

constexpr int NUM_BITS = 18;

template <typename T>
vector<vector<T>> Make2DVector(int d1, int d2, T default_value) {
  return vector<vector<T>>(d1, vector<T>(d2, default_value));
}

int GetBit(int x, int b) { return (x >> b) & 1; }

// Vector
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  int len = v.size();
  s << "[";
  for (int i = 0; i < len; i++) {
    if (i > 0)
      s << ", ";
    s << v[i];
  }
  s << "]";
  return s;
}

int64 Solve() {
  int n, k, s, t;
  cin >> n >> k >> s >> t;

  vector<int> xs(n);
  for (int i = 0; i < n; i++) {
    cin >> xs[i];
  }

  auto c_table = Make2DVector<int64>(n + 1, n + 1, 0LL);
  for (int i = 0; i <= n; i++) {
    c_table[i][0] = 1;
    for (int j = 1; j <= i; j++) {
      c_table[i][j] = c_table[i - 1][j - 1] + c_table[i - 1][j];
    }
  }

  vector<int64> d_table(n + 1);
  for (int i = 0; i <= n; i++) {
    for (int j = 1; j <= min(i, k - 1); j++) {
      d_table[i] += c_table[i][j];
    }
  }

  vector<int> invalids(n);
  vector<int> bits;
  for (int b = 0; b < NUM_BITS; b++) {
    if (GetBit(s, b) == 1) {
      for (int i = 0; i < n; i++) {
        if (GetBit(xs[i], b) == 0) {
          invalids[i] = true;
        }
      }
    }
    if (GetBit(t, b) == 0) {
      for (int i = 0; i < n; i++) {
        if (GetBit(xs[i], b) == 1) {
          invalids[i] = true;
        }
      }
    }

    if (GetBit(s, b) == 0 && GetBit(t, b) == 1) {
      bits.push_back(b);
    }
  }

  int valid_count = 0;
  for (int i = 0; i < n; i++) {
    if (!invalids[i])
      valid_count++;
  }
  if (valid_count == 0)
    return 0;

  if (bits.size() == 0) {
    int64 r = 0;
    for (int i = 1; i <= min(valid_count, k); i++) {
      r += c_table[valid_count][i];
    }
    return r;
  }

  if (k == 1) {
    return 0;
  }

  // k >= 2
  vector<int> ys;
  for (int i = 0; i < n; i++) {
    if (invalids[i])
      continue;
    int x = xs[i];
    int y = 0;
    for (int j = 0; j < bits.size(); j++) {
      if (GetBit(x, bits[j])) {
        y |= 1 << j;
      }
    }
    ys.push_back(y);
  }

  // cout << "ys: " << ys << endl;

  int64 r = 0;
  for (int h = 0; h < ys.size(); h++) {
    vector<int> zs;
    for (int i = h + 1; i < ys.size(); i++) {
      zs.push_back(ys[i] ^ ys[h]);
    }

    int64 v = 0;
    for (int p = 0; p < 1 << bits.size(); p++) {
      if (p == 0)
        continue;
      vector<int> zero_bits;
      for (int b = 0; b < bits.size(); b++) {
        if (GetBit(p, b)) {
          zero_bits.push_back(b);
        }
      }

      int count = 0;
      for (int z : zs) {
        bool valid = true;
        for (int zero_bit : zero_bits) {
          if (GetBit(z, zero_bit)) {
            valid = false;
          }
        }
        if (valid) {
          count++;
        }
      }

      if (zero_bits.size() % 2 == 1) {
        v += d_table[count];
      } else {
        v -= d_table[count];
      }
    }

    r += d_table[zs.size()] - v;
  }
  return r;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cout << Solve() << endl;
}