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

#include <bits/stdc++.h>
#include <tr1/unordered_map>
#define ri register int
#define ptf printf
using namespace std;
typedef double db;
typedef long double ld;
typedef long long ll;
typedef vector<int> poly;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef unsigned long long ulll;
typedef unsigned int uii;
typedef string strr;
#define fi first
#define se second
#define pb push_back
#define ppp pop_back
#define rez resize
const ll Inf = 2e18;
const int rlen = 1 << 20, inf = 0x3f3f3f3f;
char buf[rlen], *ib = buf, *ob = buf;
#define gc()                                                                   \\
  getchar() //(((ib == ob) && (ob = (ib =  buf) + fread(buf, 1, rlen, stdin)),
            //ib == ob) ? -1 : *ib++)
inline int read() {
  static int ans, f;
  static char ch;
  ans = 0, ch = gc(), f = 1;
  while (!isdigit(ch))
    f ^= ch == '-', ch = gc();
  while (isdigit(ch))
    ans = (ans << 3) + (ans << 1) + (ch ^ 48), ch = gc();
  return f ? ans : -ans;
}
inline ll readl() {
  static ll ans;
  static char ch;
  for (ans = 0, ch = gc(); !isdigit(ch); ch = gc())
    ;
  while (isdigit(ch))
    ans = ((ans << 2) + ans << 1) + (ch ^ 48), ch = gc();
  return ans;
}
inline int Read(char *s) {
  static int top;
  static char ch;
  top = 0, ch = gc();
  while (!isalpha(ch) && !isdigit(ch))
    ch = gc();
  while (isalpha(ch) || isdigit(ch))
    s[++top] = ch, ch = gc();
  return top;
}
namespace modular {
const int mod = 998244353; //= 1e9 + 7;
// int mod;
int ret;
inline int add(int a, int b) { return a + b < mod ? a + b : a + b - mod; }
inline int dec(int a, int b) { return a < b ? a - b + mod : a - b; }
inline int mul(int a, int b) { return (ll)a * b % mod; }
inline void Add(int &a, int b) { a = a + b < mod ? a + b : a + b - mod; }
inline void Dec(int &a, int b) { a = a < b ? a - b + mod : a - b; }
inline void Mul(int &a, int b) { a = (ll)a * b % mod; }
inline int ksm(int a, int p) {
  for (ret = 1; p; p >>= 1, Mul(a, a))
    (p & 1) && (Mul(ret, a), 1);
  return ret;
}
inline int Inv(int a) { return ksm(a, mod - 2); }
inline int sqr(int a) { return (ll)a * a % mod; }
inline int cub(int a) { return (ll)a * a % mod * a % mod; }
} // namespace modular
using namespace modular;
template <typename T> inline void ckmax(T &a, T b) { a < b ? a = b : 0; }
template <typename T> inline void ckmin(T &a, T b) { a > b ? a = b : 0; }
template <typename T> inline T Abs(T a) { return a < 0 ? -a : a; }
template <typename T> inline T gcd(T a, T b) {
  T t;
  while (b)
    t = a, a = b, b = t - t / a * a;
  return a;
}
template <typename T> inline void exgcd(T a, T b, T &x, T &y) {
  if (!b) {
    x = 1, y = 0;
    return;
  }
  exgcd(b, a - a / b * b, y, x);
  y -= a / b * x;
}
const int N = 55;
int n, K, a[N], A, B, cnt[1 << 25 | 5];
ll C[N][N], sx[1 << 20 | 5], sy[1 << 20 | 5];
inline int getcnt(ll x) { return cnt[x >> 25] + cnt[x & ((1ll << 25) - 1)]; }
int main() {
#ifdef ldxcaicai
  freopen("lx.in", "r", stdin);
#endif
  n = read(), K = read(), A = read(), B = read();
  int tot = 0;
  for (ri i = 1; i <= n; ++i) {
    int x = read();
    if (((x & A) != A) || ((x | B) != B))
      continue;
    a[++tot] = x;
  }
  if (!tot)
    return cout << 0, 0;
  for (ri i = 1, up = 1 << 25; i < up; ++i)
    cnt[i] = cnt[i >> 1] + (i & 1);
  n = tot;
  for (ri i = 0; i <= n; ++i)
    for (ri j = C[i][0] = 1; j <= i; ++j)
      C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
  for (ri i = 2; i <= n; ++i)
    for (ri j = 2; j <= i; ++j)
      C[i][j] += C[i][j - 1];
  int lm = 1 << 18;
  for (ri i = 0; i < lm; ++i) {
    for (ri j = 1; j <= n; ++j) {
      if ((a[j] & i) == i)
        sx[i] |= 1ll << (j - 1);
      if ((a[j] | i) == i)
        sy[i] |= 1ll << (j - 1);
    }
  }
  ll res = 0;
  for (ri i = A; i < lm; i = (i + 1) | A) {
    if ((B & i) ^ i)
      continue;
    for (ri j = B ^ i;; j = (j - 1) & (B ^ i)) {
      int ct = getcnt(sx[i] & sy[j | i]);
      int coe = (cnt[A] ^ cnt[i] ^ cnt[j | i] ^ cnt[B]) & 1;
      ll tmp = C[ct][max(1, min(ct, K))];
      if (coe)
        tmp *= -1;
      res += tmp;
      if (!j)
        break;
    }
  }
  cout << res;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <tr1/unordered_map>
#define ri register int
#define ptf printf
using namespace std;
typedef double db;
typedef long double ld;
typedef long long ll;
typedef vector<int> poly;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef unsigned long long ulll;
typedef unsigned int uii;
typedef string strr;
#define fi first
#define se second
#define pb push_back
#define ppp pop_back
#define rez resize
const ll Inf = 2e18;
const int rlen = 1 << 20, inf = 0x3f3f3f3f;
char buf[rlen], *ib = buf, *ob = buf;
#define gc()                                                                   \\
  getchar() //(((ib == ob) && (ob = (ib =  buf) + fread(buf, 1, rlen, stdin)),
            //ib == ob) ? -1 : *ib++)
inline int read() {
  static int ans, f;
  static char ch;
  ans = 0, ch = gc(), f = 1;
  while (!isdigit(ch))
    f ^= ch == '-', ch = gc();
  while (isdigit(ch))
    ans = (ans << 3) + (ans << 1) + (ch ^ 48), ch = gc();
  return f ? ans : -ans;
}
inline ll readl() {
  static ll ans;
  static char ch;
  for (ans = 0, ch = gc(); !isdigit(ch); ch = gc())
    ;
  while (isdigit(ch))
    ans = ((ans << 2) + ans << 1) + (ch ^ 48), ch = gc();
  return ans;
}
inline int Read(char *s) {
  static int top;
  static char ch;
  top = 0, ch = gc();
  while (!isalpha(ch) && !isdigit(ch))
    ch = gc();
  while (isalpha(ch) || isdigit(ch))
    s[++top] = ch, ch = gc();
  return top;
}
namespace modular {
const int mod = 998244353; //= 1e9 + 7;
// int mod;
int ret;
inline int add(int a, int b) { return a + b < mod ? a + b : a + b - mod; }
inline int dec(int a, int b) { return a < b ? a - b + mod : a - b; }
inline int mul(int a, int b) { return (ll)a * b % mod; }
inline void Add(int &a, int b) { a = a + b < mod ? a + b : a + b - mod; }
inline void Dec(int &a, int b) { a = a < b ? a - b + mod : a - b; }
inline void Mul(int &a, int b) { a = (ll)a * b % mod; }
inline int ksm(int a, int p) {
  for (ret = 1; p; p >>= 1, Mul(a, a))
    (p & 1) && (Mul(ret, a), 1);
  return ret;
}
inline int Inv(int a) { return ksm(a, mod - 2); }
inline int sqr(int a) { return (ll)a * a % mod; }
inline int cub(int a) { return (ll)a * a % mod * a % mod; }
} // namespace modular
using namespace modular;
template <typename T> inline void ckmax(T &a, T b) { a < b ? a = b : 0; }
template <typename T> inline void ckmin(T &a, T b) { a > b ? a = b : 0; }
template <typename T> inline T Abs(T a) { return a < 0 ? -a : a; }
template <typename T> inline T gcd(T a, T b) {
  T t;
  while (b)
    t = a, a = b, b = t - t / a * a;
  return a;
}
template <typename T> inline void exgcd(T a, T b, T &x, T &y) {
  if (!b) {
    x = 1, y = 0;
    return;
  }
  exgcd(b, a - a / b * b, y, x);
  y -= a / b * x;
}
const int N = 55;
int n, K, a[N], A, B, cnt[1 << 18 | 5];
ll C[N][N];
tr1::unordered_map<ll, int> mp;
int main() {
#ifdef ldxcaicai
  freopen("lx.in", "r", stdin);
#endif
  n = read(), K = read(), A = read(), B = read();
  int tot = 0;
  for (ri i = 1; i <= n; ++i) {
    int x = read();
    if (((x & A) != A) || ((x | B) != B))
      continue;
    a[++tot] = x;
  }
  if (!tot)
    return cout << 0, 0;
  for (ri i = 1, up = 1 << 18; i < 1 << 18; ++i)
    cnt[i] = cnt[i >> 1] + (i & 1);
  n = tot;
  for (ri i = 0; i <= n; ++i)
    for (ri j = C[i][0] = 1; j <= i; ++j)
      C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
  for (ri i = 0; i <= n; ++i)
    for (ri j = 2; j <= i; ++j)
      C[i][j] += C[i][j - 1];
  ll bas = 1 << 18;
  for (ri i = 1; i <= n; ++i) {
    int s1 = 0, s2 = 0;
    for (ri b = 0; b < 18; ++b) {
      if (!(A >> b & 1)) {
        if ((a[i] >> b & 1))
          s1 |= 1 << b;
      }
      if (B >> b & 1) {
        if (!(a[i] >> b & 1))
          s2 |= 1 << b;
      }
    }
    for (ll t1 = s1;; t1 = (t1 - 1) & s1) {
      for (ll t2 = s2;; t2 = (t2 - 1) & s2) {
        ++mp[t1 * bas + t2];
        if (!t2)
          break;
      }
      if (!t1)
        break;
    }
  }
  ll res = 0;
  for (tr1::unordered_map<ll, int>::iterator it = mp.begin(); it != mp.end();
       ++it) {
    ll coe = C[it->se][min(K, it->se)];
    ll sta = it->fi;
    ll X = sta / bas;
    ll Y = sta % bas;
    if ((cnt[X] + cnt[Y]) & 1)
      coe *= -1;
    res += coe;
  }
  cout << res;
  return 0;
}