Problem p02649 (C++) — buggy submission, verdict: Runtime Error

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

typedef unsigned long long ll;

int main() {
  int n, k, s, t;
  int a[52];
  scanf("%d%d%d%d", &n, &k, &s, &t);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int j = 0; j < 18; j++) {
    if (((s >> j) & 1) == 1 && ((t >> j) & 1) == 0) {
      puts("0");
      return 0;
    }
  }

  int n_ = 0;
  int a_[52];
  for (int i = 1; i <= n; i++) {
    bool ok = true;
    for (int j = 0; j < 18; j++) {
      if (((s >> j) & 1) == 1 && ((a[i] >> j) & 1) == 0)
        ok = false;
      if (((t >> j) & 1) == 0 && ((a[i] >> j) & 1) == 1)
        ok = false;
    }
    if (ok) {
      n_++;
      a_[n_] = a[i];
    }
  }
  n = n_;
  for (int i = 1; i <= n_; i++) {
    a[i] = a_[i];
  }

  if (n == 0) {
    puts("0");
    return 0;
  }
  vector<int> v;
  for (int j = 0; j < 18; j++) {
    if (((s >> j) & 1) == 0 && ((t >> j) & 1) == 1)
      v.push_back(j);
  }
  if (v.size() == 0) {
    if (n == 1)
      puts("1");
    else
      puts("0");
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    a_[i] = 0;
    for (int j = 0; j < v.size(); j++) {
      if ((a[i] >> v[j]) & 1)
        a_[i] += 1 << j;
    }
    a[i] = a_[i];
  }
  int tt = v.size();

  ll ret = 0;
  static int cnt[1 << 18];
  static int dig[1 << 18] = {};
  for (int i = 0; i < (1 << 18); i++) {
    dig[i] = 0;
    for (int j = 0; j < 18; j++) {
      if ((i >> j) & 1)
        dig[i]++;
    }
  }
  static ll C[52][52] = {};
  C[0][0] = 1;
  for (int i = 1; i <= 50; i++) {
    C[i][0] = C[i][i] = 1;
    for (int j = 1; j <= i - 1; j++) {
      C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
    }
  }
  for (int i = 0; i <= 50; i++) {
    for (int j = 1; j <= 50; j++) {
      C[i][j] += C[i][j - 1];
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < (1 << tt); j++)
      cnt[j] = 0;
    for (int j = i + 1; j <= n; j++) {
      cnt[((1 << tt) - 1) ^ (a[i] ^ a[j])]++;
    }
    for (int k = 0; k < tt; k++) {
      for (int j = 0; j < (1 << tt); j++) {
        if ((j >> k) & 1) {
        } else
          cnt[j] += cnt[j + (1 << k)];
      }
    }
    for (int j = 0; j < (1 << tt); j++) {
      if (dig[j] & 1)
        ret -= C[cnt[j]][k - 1];
      else
        ret += C[cnt[j]][k - 1];
    }
  }
  cout << ret << endl;
}

Error: 0

Fixed submission:

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

typedef unsigned long long ll;

int main() {
  int n, k, s, t;
  int a[52];
  scanf("%d%d%d%d", &n, &k, &s, &t);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int j = 0; j < 18; j++) {
    if (((s >> j) & 1) == 1 && ((t >> j) & 1) == 0) {
      puts("0");
      return 0;
    }
  }

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

  int n_ = 0;
  int a_[52];
  for (int i = 1; i <= n; i++) {
    bool ok = true;
    for (int j = 0; j < 18; j++) {
      if (((s >> j) & 1) == 1 && ((a[i] >> j) & 1) == 0)
        ok = false;
      if (((t >> j) & 1) == 0 && ((a[i] >> j) & 1) == 1)
        ok = false;
    }
    if (ok) {
      n_++;
      a_[n_] = a[i];
    }
  }
  n = n_;
  for (int i = 1; i <= n_; i++) {
    a[i] = a_[i];
  }

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

  if (n == 0) {
    puts("0");
    return 0;
  }
  vector<int> v;
  for (int j = 0; j < 18; j++) {
    if (((s >> j) & 1) == 0 && ((t >> j) & 1) == 1)
      v.push_back(j);
  }
  if (v.size() == 0) {
    if (n == 1)
      puts("1");
    else
      puts("0");
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    a_[i] = 0;
    for (int j = 0; j < v.size(); j++) {
      if ((a[i] >> v[j]) & 1)
        a_[i] += 1 << j;
    }
    a[i] = a_[i];
  }
  int tt = v.size();

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

  ll ret = 0;
  static int cnt[1 << 18];
  static int dig[1 << 18] = {};
  for (int i = 0; i < (1 << 18); i++) {
    dig[i] = 0;
    for (int j = 0; j < 18; j++) {
      if ((i >> j) & 1)
        dig[i]++;
    }
  }
  static ll C[52][52] = {};
  C[0][0] = 1;
  for (int i = 1; i <= 50; i++) {
    C[i][0] = C[i][i] = 1;
    for (int j = 1; j <= i - 1; j++) {
      C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
    }
  }
  for (int i = 0; i <= 50; i++) {
    for (int j = 1; j <= 50; j++) {
      C[i][j] += C[i][j - 1];
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < (1 << tt); j++)
      cnt[j] = 0;
    for (int j = i + 1; j <= n; j++) {
      cnt[((1 << tt) - 1) ^ (a[i] ^ a[j])]++;
    }
    /*for(int j=0;j<(1<<tt);j++){
            cout<<cnt[j]<<" ";
    }cout<<endl;*/
    for (int k = 0; k < t; k++) {
      for (int j = 0; j < (1 << tt); j++) {
        if ((j >> k) & 1) {
        } else
          cnt[j] += cnt[j + (1 << k)];
      }
    }
    /*for(int j=0;j<(1<<tt);j++){
            cout<<cnt[j]<<" ";
    }cout<<endl;*/
    for (int j = 0; j < (1 << tt); j++) {
      if (dig[j] & 1)
        ret -= C[cnt[j]][k - 1];
      else
        ret += C[cnt[j]][k - 1];
      // cout<<j<<" "<<ret<<endl;
    }
    // cout<<i<<" "<<ret<<endl;
  }
  cout << ret << endl;
}