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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for (llint(x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for (llint(x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))

using namespace std;
typedef pair<llint, llint> P;

int n, m, s, t;
int a[55], b[55];
bool used[55];
int beki2[20], beki3[20];
llint dp[55][55];
short cnt[400000000];
bitset<400000000> bs;

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> m >> s >> t;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if ((a[i] & s) != s || (a[i] & (~t)))
      used[i] = true;
  }

  if (s & (~t)) {
    cout << 0 << endl;
    return 0;
  }

  dp[0][0] = 1;
  for (int i = 0; i < 50; i++) {
    for (int j = 0; j <= m; j++) {
      if (j + 1 <= m)
        dp[i + 1][j + 1] += dp[i][j];
      dp[i + 1][j] += dp[i][j];
    }
  }
  for (int i = 0; i <= 50; i++) {
    for (int j = 1; j <= m; j++) {
      dp[i][j] += dp[i][j - 1];
    }
  }

  int p = 0;
  for (int i = 0; i < 18; i++) {
    if (s & (1 << i))
      continue;
    if (!(t & (1 << i)))
      continue;
    for (int j = 1; j <= n; j++) {
      llint x = (a[j] >> i) & 1;
      b[j] |= x << p;
    }
    p++;
  }
  for (int i = 1; i <= n; i++)
    a[i] = b[i];

  beki2[0] = beki3[0] = 1;
  for (int i = 1; i < 20; i++)
    beki2[i] = beki2[i - 1] * 2;
  for (int i = 1; i < 20; i++)
    beki3[i] = beki3[i - 1] * 3;

  if (t == 0) {
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
      if (a[i] == 0)
        cnt++;
    }
    cout << dp[cnt][m] << endl;
    return 0;
  }

  for (int i = 1; i <= n; i++) {
    if (used[i])
      continue;
    int B = 1 << p;
    for (int j = 0; j < B; j++) {
      llint sum = 0;
      for (int k = 0; k < p; k++) {
        if (j & (1 << k)) {
          if (a[i] & (1 << k))
            sum += 1 * beki3[k];
          else
            sum += 2 * beki3[k];
        }
      }
      cnt[sum]++;
    }
  }

  bs[0] = false;
  int r = 1;
  for (int i = 1; i < beki3[p]; i++) {
    if (r == 0)
      bs[i] = bs[i / 3];
    else if (r == 1)
      bs[i] = !bs[i - 1];
    else if (r == 2)
      bs[i] = bs[i - 1];
    r++;
    if (r >= 3)
      r = 0;
  }
  // for(int i = 0; i < 15; i++) cout << cnt[i] << " "; cout<< endl;

  llint ans = 0;
  for (int i = 0; i < beki3[p]; i++) {
    llint tmp = dp[cnt[i]][m] - 1;
    if (bs[i])
      ans -= tmp;
    // cout << i << " " << tmp << endl;
    else
      ans += tmp;
  }
  cout << ans << endl;

  // for(int i = 1; i <= n; i++) cout << a[i] << " "; cout << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for (llint(x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for (llint(x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))

using namespace std;
typedef pair<llint, llint> P;

int n, m, s, t;
int a[55], b[55];
bool used[55];
int beki2[20], beki3[20];
llint dp[55][55];
short cnt[400000000];
bitset<400000000> bs;

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> m >> s >> t;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if ((a[i] & s) != s || (a[i] & (~t)))
      used[i] = true;
  }

  if (s & (~t)) {
    cout << 0 << endl;
    return 0;
  }

  dp[0][0] = 1;
  for (int i = 0; i < 50; i++) {
    for (int j = 0; j <= m; j++) {
      if (j + 1 <= m)
        dp[i + 1][j + 1] += dp[i][j];
      dp[i + 1][j] += dp[i][j];
    }
  }
  for (int i = 0; i <= 50; i++) {
    for (int j = 1; j <= m; j++) {
      dp[i][j] += dp[i][j - 1];
    }
  }

  int p = 0;
  for (int i = 0; i < 18; i++) {
    if (s & (1 << i))
      continue;
    if (!(t & (1 << i)))
      continue;
    for (int j = 1; j <= n; j++) {
      llint x = (a[j] >> i) & 1;
      b[j] |= x << p;
    }
    p++;
  }
  for (int i = 1; i <= n; i++)
    a[i] = b[i];

  beki2[0] = beki3[0] = 1;
  for (int i = 1; i < 20; i++)
    beki2[i] = beki2[i - 1] * 2;
  for (int i = 1; i < 20; i++)
    beki3[i] = beki3[i - 1] * 3;

  if (t == 0) {
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
      if (a[i] == 0)
        cnt++;
    }
    cout << dp[cnt][m] << endl;
    return 0;
  }

  for (int i = 1; i <= n; i++) {
    if (used[i])
      continue;
    int B = 1 << p;
    for (int j = 0; j < B; j++) {
      llint sum = 0;
      for (int k = 0; k < p; k++) {
        if (j & (1 << k)) {
          if (a[i] & (1 << k))
            sum += 1 * beki3[k];
          else
            sum += 2 * beki3[k];
        }
      }
      cnt[sum]++;
    }
  }

  bs[0] = false;
  for (int i = 1; i < beki3[p]; i++) {
    int r = i % 3;
    if (r == 0)
      bs[i] = bs[i / 3];
    else if (r == 1)
      bs[i] = !bs[i - 1];
    else if (r == 2)
      bs[i] = bs[i - 1];
  }
  // for(int i = 0; i < 15; i++) cout << cnt[i] << " "; cout<< endl;

  llint ans = 0;
  for (int i = 0; i < beki3[p]; i++) {
    llint tmp = dp[cnt[i]][m] - 1;
    if (bs[i])
      ans -= tmp;
    // cout << i << " " << tmp << endl;
    else
      ans += tmp;
  }
  cout << ans << endl;

  // for(int i = 1; i <= n; i++) cout << a[i] << " "; cout << endl;

  return 0;
}