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

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------

int N, K, S, T;
int A[51];

ll C[55][55];

int dp[1 << 18];
ll dp2[1 << 18];

void solve() {
  int i, j, k, l, r, x, y;
  string s;

  FOR(i, 52)
  for (j = 0; j <= i; j++) C[i][j] =
      (j == 0 || j == i) ? 1 : (C[i - 1][j - 1] + C[i - 1][j]);

  cin >> N >> K >> S >> T;

  if (S & ~T)
    return _P("0\\n");

  FOR(i, N) {
    cin >> A[i];
    if ((A[i] & ~T) || (A[i] & S) != S) {
      i--;
      N--;
      continue;
    }
  }

  int mask;
  int U = T & (~S);
  FOR(mask, 1 << 18) if ((mask & U) == mask) {
    FOR(i, N) dp[A[i] & mask]++;
    FOR(i, N) if (dp[A[i] & mask]) {
      for (j = 1; j <= min(K, dp[A[i] & mask]); j++)
        dp2[mask] += C[dp[A[i] & mask]][j];
      dp[A[i] & mask] = 0;
    }
  }

  ll ret = 0;
  FOR(mask, 1 << 18) {
    if (__builtin_popcount(mask) % 2)
      ret -= dp2[mask];
    else
      ret += dp2[mask];
  }

  cout << ret << endl;
}

int main(int argc, char **argv) {
  string s;
  int i;
  if (argc == 1)
    ios::sync_with_stdio(false), cin.tie(0);
  FOR(i, argc - 1) s += argv[i + 1], s += '\\n';
  FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------

int N, K, S, T;
int A[51], B[51], C[51];

ll from[51][1 << 18];

void solve() {
  int i, j, k, l, r, x, y;
  string s;

  cin >> N >> K >> S >> T;

  FOR(i, N) {
    cin >> A[i];
    if (A[i] & ~T) {
      i--;
      N--;
      continue;
    }
    if ((A[i] & S) != S) {
      i--;
      N--;
      continue;
    }
    B[i] = A[i] ^ ((1 << 18) - 1);
  }

  ll ret = 0;

  int mask, tomask;
  FOR(i, N) {
    int add = 0;
    int del = 0;
    FOR(j, 18) {
      if ((S & (1 << j)) == 0 && (T & (1 << j))) {
        if (A[i] & (1 << j))
          del |= 1 << j;
        else
          add |= 1 << j;
      }
    }
    tomask = add | del;
    ZERO(from);
    from[0][0] = 1;
    for (j = i + 1; j < N; j++) {
      int nmask = (B[j] & del) | (A[j] & add);
      for (int mask = tomask; mask >= 0; mask--) {
        mask &= tomask;
        for (x = j - i - 1; x >= 0; x--)
          from[x + 1][mask | nmask] += from[x][mask];
      }
    }
    for (j = 0; j <= K - 1; j++)
      ret += from[j][tomask];
  }
  cout << ret << endl;
}

int main(int argc, char **argv) {
  string s;
  int i;
  if (argc == 1)
    ios::sync_with_stdio(false), cin.tie(0);
  FOR(i, argc - 1) s += argv[i + 1], s += '\\n';
  FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}