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

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define all(aaa) aaa.begin(), aaa.end()

const int N = 51, B = 18, M = (1 << B);
int a[N], b[N];
ll dp[N];
int ct = B;

int removeBit(int x, int p) {
  int res = 0, y = 0;
  for (int i = 0; i < ct; i++) {
    if (i != p) {
      res |= (((x >> i) & 1) << y);
      y++;
    }
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, k, s, t;
  cin >> n >> k >> s >> t;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  for (int i = B - 1; i >= 0; i--) {
    int x = (s >> i) & 1, y = (t >> i) & 1;

    if (x > y) {
      cout << "0\\n";
      return 0;
    }

    if (x == 1) {
      int new_n = 0;
      for (int j = 0; j < n; j++) {
        if ((a[j] >> i) & 1)
          a[new_n++] = removeBit(a[j], i);
      }
      n = new_n;
      ct--;
    }

    if (y == 0) {
      int new_n = 0;
      for (int j = 0; j < n; j++) {
        if (((a[j] >> i) & 1) == 0)
          a[new_n++] = removeBit(a[j], i);
      }
      n = new_n;
      ct--;
    }
  }

  ll ans = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      b[j] = (a[i] ^ a[j]);
    }

    for (int j = 0; j < (1 << ct); j++) {
      memset(dp, 0, sizeof(dp));
      dp[1] = 1;

      for (int h = i + 1; h < n; h++) {
        if ((b[h] & j) == 0) {
          for (int z = k - 1; z > 0; z--) {
            dp[z + 1] += dp[z];
          }
        }
      }
      int p = 0;
      for (int h = 0; h < ct; h++)
        p ^= (j >> h);

      ll cur = 0;
      for (int h = 1; h <= k; h++)
        cur += dp[h];

      if (p & 1)
        ans -= cur;
      else
        ans += cur;
    }
  }

  cout << ans;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define all(aaa) aaa.begin(), aaa.end()

const int N = 51, B = 18, M = (1 << B);
int a[N], b[N];
ll dp[N][M];
int ct = B;

int removeBit(int x, int p) {
  int res = 0, y = 0;
  for (int i = 0; i < ct; i++) {
    if (i != p) {
      res |= (((x >> i) & 1) << y);
      y++;
    }
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, k, s, t;
  cin >> n >> k >> s >> t;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  for (int i = B - 1; i >= 0; i--) {
    int x = (s >> i) & 1, y = (t >> i) & 1;

    if (x > y) {
      cout << "0\\n";
      return 0;
    }

    if (x == 1) {
      int new_n = 0;
      for (int j = 0; j < n; j++) {
        if ((a[j] >> i) & 1)
          a[new_n++] = removeBit(a[j], i);
      }
      n = new_n;
      ct--;
      // cout << i << "\\n";
    }
    if (y == 0) {
      int new_n = 0;
      for (int j = 0; j < n; j++) {
        if (((a[j] >> i) & 1) == 0)
          a[new_n++] = removeBit(a[j], i);
      }
      n = new_n;
      ct--;
      // cout << i << "\\n";
    }
  }

  ll ans = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      b[j] = (a[i] ^ a[j]);
    }

    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;

    for (int j = i; j < n - 1; j++) {
      for (int h = min(k - 1, j - i); h >= 0; h--) {
        for (int k = (1 << ct) - 1; k >= 0; k--) {
          dp[h + 1][k | b[j + 1]] += dp[h][k];
        }
      }
    }

    for (int j = 0; j < k; j++) {
      ans += dp[j][(1 << ct) - 1];
    }
  }

  cout << ans;

  return 0;
}