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

#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x + modulo)) % modulo)
#define Inf 1000000000

long long get(long long num, long long K) {
  long long ret = 0;
  long long temp = 1;
  for (int i = 0; i < K; i++) {
    temp *= num - i;
    temp /= i + 1;
    ret += temp;
  }
  return ret;
}

int main() {

  int N, K, S, T;
  cin >> N >> K >> S >> T;

  for (int i = 0; i < 18; i++) {
    int a = (S >> i) & 1;
    int b = (T >> i) & 1;
    if (a == 1 && b == 0) {
      cout << 0 << endl;
      return 0;
    }
  }

  vector<int> A;
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    bool f = true;
    for (int j = 0; j < 18; j++) {
      int t = (x >> j) & 1;
      int a = (S >> j) & 1;
      int b = (T >> j) & 1;
      if (t == 1 && b == 0) {
        f = false;
        break;
      }
      if (t == 0 && a == 1) {
        f = false;
        break;
      }
    }
    if (f) {
      A.push_back(x);
    }
  }

  N = A.size();

  vector<int> valid;
  for (int i = 0; i < 18; i++) {
    int a = (S >> i) & 1;
    int b = (T >> i) & 1;
    if (a == 0 && b == 1) {
      valid.push_back(i);
    }
  }

  vector<long long> calc(N + 1);
  for (int i = 0; i <= N; i++)
    calc[i] = get(i, K);

  long long ans = 0;
  for (int i = 0; i < (1 << valid.size()); i++) {
    int cnt = 0;
    map<int, long long> mp;

    int X = 0;
    for (int j = 0; j < valid.size(); j++) {
      if ((i >> j) & 1) {
        X ^= (1 << valid[j]);
        cnt++;
      }
    }

    for (int j = 0; j < N; j++) {
      mp[A[j] & X]++;
    }
    long long temp = 0;
    for (auto a : mp) {
      temp += calc[a.second];
    }
    if (cnt % 2 == 0)
      ans += temp;
    else
      ans -= temp;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x + modulo)) % modulo)
#define Inf 1000000000

long long get(long long num, long long K) {
  long long ret = 0;
  long long temp = 1;
  for (int i = 0; i < K; i++) {
    temp *= num - i;
    temp /= i + 1;
    ret += temp;
  }
  return ret;
}

int main() {

  int N, K, S, T;
  cin >> N >> K >> S >> T;

  for (int i = 0; i < 18; i++) {
    int a = (S >> i) & 1;
    int b = (T >> i) & 1;
    if (a == 1 && b == 0) {
      cout << 0 << endl;
      return 0;
    }
  }

  vector<int> A;
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    bool f = true;
    for (int j = 0; j < 18; j++) {
      int t = (x >> j) & 1;
      int a = (S >> j) & 1;
      int b = (T >> j) & 1;
      if (t == 1 && b == 0) {
        f = false;
        break;
      }
      if (t == 0 && a == 1) {
        f = false;
        break;
      }
    }
    if (f) {
      A.push_back(x);
      // cout<<x<<endl;
    }
  }

  N = A.size();

  vector<int> valid;
  for (int i = 0; i < 18; i++) {
    int a = (S >> i) & 1;
    int b = (T >> i) & 1;
    if (a == 0 && b == 1) {
      valid.push_back(i);
    }
  }

  long long ans = 0;
  for (int i = 0; i < (1 << valid.size()); i++) {
    int cnt = 0;
    map<int, long long> mp;
    for (int j = 0; j < N; j++) {
      cnt = 0;
      int t = 0;
      for (int k = 0; k < valid.size(); k++) {
        if ((i >> k) & 1) {
          cnt++;
          if ((A[j] >> valid[k]) & 1)
            t ^= 1 << valid[k];
        }
      }
      mp[t]++;
    }
    long long temp = 0;
    for (auto a : mp) {
      temp += get(a.second, K);
    }
    if (cnt % 2 == 0)
      ans += temp;
    else
      ans -= temp;
  }

  cout << ans << endl;

  return 0;
}