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

#include <bits/stdc++.h>
using namespace std;
//{
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define VAR(v, i) __typeof(i) v = (i)
#define all(v) (v).begin(), (v).end()
#define FORE(i, c) for (VAR(i, (c).begin()); i != (c).end(); ++i)

#define PII pair<int, int>
#define st first
#define nd second
#define pb push_back
#define lint long long int
#define VI vector<int>

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
}
;
//{
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define make(x)                                                                \\
  int(x);                                                                      \\
  scanf("%d", &(x));
#define make2(x, y)                                                            \\
  int(x), (y);                                                                 \\
  scanf("%d%d", &(x), &(y));
#define make3(x, y, z)                                                         \\
  int(x), (y), (z);                                                            \\
  scanf("%d%d%d", &(x), &(y), &(z));
#define make4(x, y, z, t)                                                      \\
  int(x), (y), (z), (t);                                                       \\
  scanf("%d%d%d%d", &(x), &(y), &(z), &(t));
#define makev(v, n)                                                            \\
  VI(v);                                                                       \\
  FOR(i, 0, (n)) {                                                             \\
    make(a);                                                                   \\
    (v).pb(a);                                                                 \\
  }
#define IOS ios_base::sync_with_stdio(0)
#define HEAP priority_queue

#define read(x) scanf("%d", &(x));
#define read2(x, y) scanf("%d%d", &(x), &(y));
#define read3(x, y, z) scanf("%d%d%d", &(x), &(y), &(z));
#define read4(x, y, z, t) scanf("%d%d%d%d", &(x), &(y), &(z), &(t));
#define readv(v, n)                                                            \\
  FOR(i, 0, (n)) {                                                             \\
    make(a);                                                                   \\
    (v).pb(a);                                                                 \\
  }
#define jeb() fflush(stdout);
//}

int n, k;
int s, t;
VI v;

int p3[30];
vector<PII> ile;

lint bin[55][55];
lint bin2[55];

int memo[300000];
int memo2[300000];

int logi[300000];

int main() {
  FOR(i, 0, 19) logi[1 << i] = i;
  read4(n, k, s, t);
  FOR(i, 0, n) {
    make(a);
    if (((a & s) == s) && ((a | t) == t)) {
      v.pb(a);
    }
  }
  n = v.size();
  if (((t & s) != s) || n == 0) {
    printf("0\\n");
    return 0;
  }
  k = min(k, n);
  FOR(i, 0, n + 1) bin[i][i] = bin[i][0] = 1;
  FOR(i, 2, n + 1) {
    FOR(j, 1, i) bin[i][j] = bin[i - 1][j - 1] + bin[i - 1][j];
  }
  FOR(i, 0, n + 1) {
    bin2[i] = 0;
    FOR(j, 0, min(i + 1, k + 1)) bin2[i] += bin[i][j];
  }

  VI cyfry;
  FOR(i, 0, 18) {
    if ((s & (1 << i)) != (t & (1 << i)))
      cyfry.pb(i);
  }
  int h = cyfry.size();
  // debug() << imie(cyfry) imie(h);

  int p2 = 1 << h;
  p3[0] = 1;
  FOR(i, 1, h + 1) p3[i] = p3[i - 1] * 3;

  FOR(i, 0, n) {
    int a = v[i];
    // debug() << imie(a);

    int mask0 = 0;
    FOR(u, 0, h) {
      int c = cyfry[u];
      if (a & (1 << c))
        mask0 += p3[u];
    }
    ile.pb({mask0, 0});

    memo[0] = 0;
    memo2[0] = 0;

    FOR(j, 1, p2) {
      int maxi = logi[j & (-j)];
      int reszta = j - (1 << maxi);
      int mask = memo[reszta];
      int dw = memo2[reszta] + 1;
      memo2[j] = dw;

      int c = cyfry[maxi];
      int d = 2;
      if (a & (1 << c)) {
        d = 1;
      }
      mask += p3[maxi] * d;
      memo[j] = mask;

      mask += mask0;
      ile.pb({mask, dw});
    }
    // debug() << imie(ile);
  }
  // debug() << ile ;
  FOR(i, 0, n + 1) bin2[i] -= 1;
  lint ans = bin2[n];
  sort(all(ile));
  int cnt = 1;
  PII kto = ile[0];
  FOR(i, 1, ile.size()) {
    if (ile[i] == kto) {
      cnt++;
      continue;
    } else {
      if (kto.st != p3[h] - 1) {
        int dw = kto.nd;
        int u = h - dw;
        if (u % 2 == 1) {
          ans -= bin2[cnt];
        } else {
          ans += bin2[cnt];
        }
      }
      kto = ile[i];
      cnt = 1;
    }
  }
  if (kto.st != p3[h] - 1) {
    int dw = kto.nd;
    int u = h - dw;
    if (u % 2 == 1) {
      ans -= bin2[cnt];
    } else {
      ans += bin2[cnt];
    }
  }

  printf("%lld\\n", ans);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
//{
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define VAR(v, i) __typeof(i) v = (i)
#define all(v) (v).begin(), (v).end()
#define FORE(i, c) for (VAR(i, (c).begin()); i != (c).end(); ++i)

#define PII pair<int, int>
#define st first
#define nd second
#define pb push_back
#define lint long long int
#define VI vector<int>

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
}
;
//{
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define make(x)                                                                \\
  int(x);                                                                      \\
  scanf("%d", &(x));
#define make2(x, y)                                                            \\
  int(x), (y);                                                                 \\
  scanf("%d%d", &(x), &(y));
#define make3(x, y, z)                                                         \\
  int(x), (y), (z);                                                            \\
  scanf("%d%d%d", &(x), &(y), &(z));
#define make4(x, y, z, t)                                                      \\
  int(x), (y), (z), (t);                                                       \\
  scanf("%d%d%d%d", &(x), &(y), &(z), &(t));
#define makev(v, n)                                                            \\
  VI(v);                                                                       \\
  FOR(i, 0, (n)) {                                                             \\
    make(a);                                                                   \\
    (v).pb(a);                                                                 \\
  }
#define IOS ios_base::sync_with_stdio(0)
#define HEAP priority_queue

#define read(x) scanf("%d", &(x));
#define read2(x, y) scanf("%d%d", &(x), &(y));
#define read3(x, y, z) scanf("%d%d%d", &(x), &(y), &(z));
#define read4(x, y, z, t) scanf("%d%d%d%d", &(x), &(y), &(z), &(t));
#define readv(v, n)                                                            \\
  FOR(i, 0, (n)) {                                                             \\
    make(a);                                                                   \\
    (v).pb(a);                                                                 \\
  }
#define jeb() fflush(stdout);
//}

int n, k;
int s, t;
VI v;

int p3[30];
unordered_map<int, int> ile;
unordered_map<int, int> dwojki;

lint bin[55][55];
lint bin2[55];

int memo[300000];
int memo2[300000];

int logi[300000];

int main() {
  FOR(i, 0, 19) logi[1 << i] = i;
  read4(n, k, s, t);
  FOR(i, 0, n) {
    make(a);
    if (((a & s) == s) && ((a | t) == t)) {
      v.pb(a);
    }
  }
  n = v.size();
  if (((t & s) != s) || n == 0) {
    printf("0\\n");
    return 0;
  }
  k = min(k, n);
  FOR(i, 0, n + 1) bin[i][i] = bin[i][0] = 1;
  FOR(i, 2, n + 1) {
    FOR(j, 1, i) bin[i][j] = bin[i - 1][j - 1] + bin[i - 1][j];
  }
  FOR(i, 0, n + 1) {
    bin2[i] = 0;
    FOR(j, 0, min(i + 1, k + 1)) bin2[i] += bin[i][j];
  }

  VI cyfry;
  FOR(i, 0, 18) {
    if ((s & (1 << i)) != (t & (1 << i)))
      cyfry.pb(i);
  }
  int h = cyfry.size();
  // debug() << imie(cyfry) imie(h);

  int p2 = 1 << h;
  p3[0] = 1;
  FOR(i, 1, h + 1) p3[i] = p3[i - 1] * 3;

  FOR(i, 0, n) {
    int a = v[i];
    // debug() << imie(a);

    int mask0 = 0;
    FOR(u, 0, h) {
      int c = cyfry[u];
      if (a & (1 << c))
        mask0 += p3[u];
    }
    if (ile.find(mask0) == ile.end())
      ile[mask0] = 1;
    else
      ile[mask0]++;
    dwojki[mask0] = 0;
    memo[0] = 0;
    memo2[0] = 0;

    FOR(j, 1, p2) {
      int maxi = logi[j & (-j)];
      int reszta = j - (1 << maxi);
      int mask = memo[reszta];
      int dw = memo2[reszta] + 1;
      memo2[j] = dw;

      int c = cyfry[maxi];
      int d = 2;
      if (a & (1 << c)) {
        d = 1;
      }
      mask += p3[maxi] * d;
      memo[j] = mask;

      mask += mask0;
      if (ile.find(mask) == ile.end())
        ile[mask] = 1;
      else
        ile[mask]++;
      dwojki[mask] = dw;
    }
    // debug() << imie(ile);
  }
  // debug() << ile ;
  FOR(i, 0, n + 1) bin2[i] -= 1;
  lint ans = bin2[n];
  FORE(it, ile) {
    int mask = it->st;
    if (mask == p3[h] - 1)
      continue;
    int u = h - dwojki[mask];
    if (u % 2 == 1) {
      ans -= bin2[it->nd];
    } else {
      ans += bin2[it->nd];
    }
  }
  printf("%lld\\n", ans);
}