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

#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 102;
const int m = 18;

int n, k;
int a[N];
int s, t;

ll C[N][N];

int ast[N];

ll p[N];

void solv() {
  for (int i = 0; i < N; ++i) {
    C[i][0] = 1;
    for (int j = 1; j <= i; ++j)
      C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
  }
  ast[0] = 1;
  for (int i = 1; i < N; ++i)
    ast[i] = ast[i - 1] * 3;
  scanf("%d%d", &n, &k);
  scanf("%d%d", &s, &t);
  for (int i = 0; i < n; ++i)
    scanf("%d", &a[i]);
  for (int i = 0; i < m; ++i) {
    if ((s & (1 << i)) && !(t & (1 << i))) {
      printf("0\\n");
      return;
    }
  }
  vector<int> v;
  for (int i = 0; i < n; ++i) {
    bool z = true;
    for (int j = 0; j < m; ++j) {
      if ((s & (1 << j)) && (t & (1 << j))) {
        if (!(a[i] & (1 << j))) {
          z = false;
          break;
        }
      }
      if (!(s & (1 << j)) && !(t & (1 << j))) {
        if ((a[i] & (1 << j))) {
          z = false;
          break;
        }
      }
    }
    if (z)
      v.push_back(a[i]);
  }
  n = v.size();
  for (int i = 0; i < n; ++i)
    a[i] = v[i];
  v.clear();
  for (int i = 0; i < m; ++i) {
    if (!(s & (1 << i)) && (t & (1 << i)))
      v.push_back(i);
  }
  vector<pair<int, int>> mp;
  for (int i = 0; i < n; ++i) {
    for (int x = 0; x < (1 << v.size()); ++x) {
      pair<int, int> u = m_p(0, 0);
      for (int j = 0; j < v.size(); ++j) {
        if ((x & (1 << j))) {
          ++u.se;
          if ((a[i] & (1 << v[j])))
            u.fi += (ast[v[j]] * 1);
          else
            u.fi += (ast[v[j]] * 2);
        }
      }
      mp.push_back(u);
    }
  }
  for (int u = 1; u <= 50; ++u) {
    for (int i = 1; i <= k; ++i)
      p[u] += C[u][i];
  }
  ll ans = 0;
  sort(all(mp));
  int q = 1;
  for (int i = 1; i < mp.size(); ++i) {
    if (mp[i] == mp[i - 1]) {
      ++q;
    } else {
      if (mp[i - 1].se % 2 == 0)
        ans += p[q];
      else
        ans -= p[q];
      q = 1;
    }
  }
  if (mp.back().se % 2 == 0)
    ans += p[q];
  else
    ans -= p[q];
  /*for (auto it = mp.begin(); it != mp.end(); ++it)
  {
      if (it->fi.se % 2 == 0)
      {
          ans += p[it->se];
      }
      else
      {
          ans -= p[it->se];
      }
  }*/
  printf("%lld\\n", ans);
}

int main() {
#ifdef SOMETHING
  freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // SOMETHING
  // ios_base::sync_with_stdio(false), cin.tie(0);
  solv();
  return 0;
}

// while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 1003;
const int m = 18;

int n, k;
int a[N];
int s, t;

ll C[N][N];

int ast[N];

ll p[N];

void solv() {
  for (int i = 0; i < N; ++i) {
    C[i][0] = 1;
    for (int j = 1; j <= i; ++j)
      C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
  }
  ast[0] = 1;
  for (int i = 1; i < N; ++i)
    ast[i] = ast[i - 1] * 3;
  scanf("%d%d", &n, &k);
  scanf("%d%d", &s, &t);
  for (int i = 0; i < n; ++i)
    scanf("%d", &a[i]);
  for (int i = 0; i < m; ++i) {
    if ((s & (1 << i)) && !(t & (1 << i))) {
      printf("0\\n");
      return;
    }
  }
  vector<int> v;
  for (int i = 0; i < n; ++i) {
    bool z = true;
    for (int j = 0; j < m; ++j) {
      if ((s & (1 << j)) && (t & (1 << j))) {
        if (!(a[i] & (1 << j))) {
          z = false;
          break;
        }
      }
      if (!(s & (1 << j)) && !(t & (1 << j))) {
        if ((a[i] & (1 << j))) {
          z = false;
          break;
        }
      }
    }
    if (z)
      v.push_back(a[i]);
  }
  n = v.size();
  for (int i = 0; i < n; ++i)
    a[i] = v[i];
  v.clear();
  for (int i = 0; i < m; ++i) {
    if (!(s & (1 << i)) && (t & (1 << i)))
      v.push_back(i);
  }
  map<pair<int, int>, int> mp;
  for (int i = 0; i < n; ++i) {
    for (int x = 0; x < (1 << v.size()); ++x) {
      pair<int, int> u = m_p(0, 0);
      for (int j = 0; j < v.size(); ++j) {
        if ((x & (1 << j))) {
          ++u.se;
          if ((a[i] & (1 << v[j])))
            u.fi += (ast[v[j]] * 1);
          else
            u.fi += (ast[v[j]] * 2);
        }
      }
      mp[u]++;
    }
  }
  ll ans = 0;
  for (int u = 1; u <= 50; ++u) {
    for (int i = 1; i <= k; ++i)
      p[u] += C[u][i];
  }
  for (auto it = mp.begin(); it != mp.end(); ++it) {
    if (it->fi.se % 2 == 0) {
      ans += p[it->se];
    } else {
      ans -= p[it->se];
    }
  }
  printf("%lld\\n", ans);
}

int main() {
#ifdef SOMETHING
  freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // SOMETHING
  // ios_base::sync_with_stdio(false), cin.tie(0);
  solv();
  return 0;
}

// while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}