Problem p02649 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\n"

using namespace std;
const long long MOD = (long long)1e9 + 7;

int n, k, s, t, a[55], sz, al;
long long ans, candi[2][18], combi[60][60], F[60][60];
vector<int> vec;

int cnt_bit(int x) {
  int cnt = 0;
  while (x) {
    if (x & 1)
      cnt++;
    x = x >> 1;
  }
  return cnt;
}

void fun(int cur, int sign, long long res, int v) {
  if (cur == 18) {
    int tmp = cnt_bit(res);
    // cout << sign * F[tmp][k - 1] << E;
    ans += sign * F[tmp][k - 1];
  } else {
    if (((t >> cur) & 1) ^ ((s >> cur) & 1)) {
      if ((v >> cur) & 1) {
        fun(cur + 1, sign, res, v);
        fun(cur + 1, -sign, res & candi[1][cur], v);
      } else {
        fun(cur + 1, sign, res, v);
        fun(cur + 1, -sign, res & candi[0][cur], v);
      }
    } else
      fun(cur + 1, sign, res, v);
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  for (int i = 1; i <= 50; i++)
    combi[i][0] = 1;
  combi[1][1] = 1;
  for (int i = 2; i <= 50; i++) {
    for (int j = 1; j <= i; j++) {
      combi[i][j] = combi[i - 1][j] + combi[i - 1][j - 1];
    }
  }
  for (int i = 1; i <= 50; i++) {
    F[i][0] = 0;
    for (int j = 1; j <= 50; j++) {
      F[i][j] = combi[i][j] + F[i][j - 1];
    }
  }
  cin >> n >> k;
  cin >> s >> t;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  if (((s & t) != s) || ((s | t) != t)) {
    cout << 0 << E;
  } else {
    for (int i = 0; i < n; i++) {
      if ((a[i] & s) != s)
        continue;
      if ((a[i] | t) != t)
        continue;
      vec.pb(a[i]);
    }
    sz = vec.size();
    for (int i = 0; i < sz; i++) {
      if (s == t && vec[i] == t) {
        ans++;
      }
      al = i;
      memset(candi, 0, sizeof(candi));
      for (int j = 0; j < i; j++) {
        for (int u = 0; u < 18; u++) {
          candi[(vec[j] >> u) & 1][u] += 1LL << j;
        }
      }
      fun(0, 1, (1LL << al) - 1, vec[i]);
    }
    cout << ans << E;
  }
  // system("pause");
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\n"

using namespace std;
const long long MOD = (long long)1e9 + 7;

int n, k, s, t, a[55], sz, al;
long long ans, candi[2][18], combi[60][60], F[60][60];
vector<int> vec;

int cnt_bit(long long x) {
  int cnt = 0;
  while (x) {
    if (x & 1)
      cnt++;
    x = x >> 1;
  }
  return cnt;
}

void fun(int cur, int sign, long long res, int v) {
  if (cur == 18) {
    int tmp = cnt_bit(res);
    // cout << sign * F[tmp][k - 1] << E;
    ans += sign * F[tmp][k - 1];
  } else {
    if (((t >> cur) & 1) ^ ((s >> cur) & 1)) {
      if ((v >> cur) & 1) {
        fun(cur + 1, sign, res, v);
        fun(cur + 1, -sign, res & candi[1][cur], v);
      } else {
        fun(cur + 1, sign, res, v);
        fun(cur + 1, -sign, res & candi[0][cur], v);
      }
    } else
      fun(cur + 1, sign, res, v);
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  for (int i = 1; i <= 50; i++)
    combi[i][0] = 1;
  combi[1][1] = 1;
  for (int i = 2; i <= 50; i++) {
    for (int j = 1; j <= i; j++) {
      combi[i][j] = combi[i - 1][j] + combi[i - 1][j - 1];
    }
  }
  for (int i = 1; i <= 50; i++) {
    F[i][0] = 0;
    for (int j = 1; j <= 50; j++) {
      F[i][j] = combi[i][j] + F[i][j - 1];
    }
  }
  cin >> n >> k;
  cin >> s >> t;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  if (((s & t) != s) || ((s | t) != t)) {
    cout << 0 << E;
  } else {
    for (int i = 0; i < n; i++) {
      if ((a[i] & s) != s)
        continue;
      if ((a[i] | t) != t)
        continue;
      vec.pb(a[i]);
    }
    sz = vec.size();
    for (int i = 0; i < sz; i++) {
      if (s == t && vec[i] == t) {
        ans++;
      }
      al = i;
      memset(candi, 0, sizeof(candi));
      for (int j = 0; j < i; j++) {
        for (int u = 0; u < 18; u++) {
          candi[(vec[j] >> u) & 1][u] += 1LL << j;
        }
      }
      fun(0, 1, (1LL << al) - 1, vec[i]);
    }
    cout << ans << E;
  }
  // system("pause");
  return 0;
}