prithivMLmods/Coder-Stat huggingface dataset:
Problem p02837 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>

// 2^N < 3*10^4なので、すべての状態をチェックしてもいけそう？
int main() {
  size_t N = 0;
  std::cin >> N;

  std::vector<uint16_t> one_graph(N);  // 1で接続
  std::vector<uint16_t> zero_graph(N); // 0でつながる
  for (size_t i = 0; i < N; ++i) {
    size_t A;
    std::cin >> A;
    for (size_t j = 0; j < A; ++j) {
      int x, y;
      std::cin >> x >> y;
      if (y == 1) {
        one_graph[i] |= size_t(1) << (x - 1);
      } else if (y == 0) {
        zero_graph[i] |= size_t(1) << (x - 1);
      }
    }
  }

  size_t result = 0;
  for (uint16_t flags = 1; flags < (2 << N); ++flags) {
    [&] {
      size_t count = 0;
      for (size_t i = 0; i < N; ++i) {
        // 正直者の発言が状態と矛盾するかを調べる
        if (flags & (1 << i)) {
          const uint16_t one = one_graph[i];
          const uint16_t zero = zero_graph[i];
          if ((flags & one) != one || (~flags & zero) != zero) {
            return;
          }
          count++;
        }
      }
      result = std::max(result, count);
    }();
  }

  std::cout << result;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

// 2^N < 3*10^4なので、すべての状態をチェックしてもいけそう？
int main() {
  size_t N = 0;
  std::cin >> N;

  std::vector<uint16_t> one_graph(N);  // 1で接続
  std::vector<uint16_t> zero_graph(N); // 0でつながる
  for (size_t i = 0; i < N; ++i) {
    size_t A;
    std::cin >> A;
    for (size_t j = 0; j < A; ++j) {
      int x, y;
      std::cin >> x >> y;
      if (y == 1) {
        one_graph[i] |= size_t(1) << (x - 1);
      } else if (y == 0) {
        zero_graph[i] |= size_t(1) << (x - 1);
      }
    }
  }

  size_t result = 0;
  for (uint16_t flags = 1; flags < (1 << N); ++flags) {
    [&] {
      size_t count = 0;
      for (size_t i = 0; i < N; ++i) {
        // 正直者の発言が状態と矛盾するかを調べる
        if (flags & (1 << i)) {
          const uint16_t one = one_graph[i];
          const uint16_t zero = zero_graph[i];
          if ((flags & one) != one || (~flags & zero) != zero) {
            return;
          }
          count++;
        }
      }
      result = std::max(result, count);
    }();
  }

  std::cout << result;

  return 0;
}
Problem p02837 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  vector<vector<int>> x(N), y(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    for (int j = 0; j < A.at(j); j++) {
      int a, b;
      cin >> a >> b;
      x.at(i).push_back(a - 1);
      y.at(i).push_back(b);
    }
  }

  int ans = 0;

  for (int i = 0; i < (1 << N); i++) {
    vector<int> honest(N);
    bool flag = true;
    for (int j = 0; j < N; j++) {
      honest.at(j) = (i >> j) % 2;
    }
    for (int j = 0; j < N; j++) {
      if (honest.at(j) == 0) {
        continue;
      }
      for (int k = 0; k < x.at(j).size(); k++) {
        if (y.at(j).at(k) != honest.at(x.at(j).at(k))) {
          flag = false;
          break;
        }
      }
      if (!flag) {
        break;
      }
    }
    if (flag) {
      int aa = 0;
      for (int j = 0; j < N; j++) {
        aa += honest.at(j);
      }
      ans = max(ans, aa);
    }
  }
  cout << ans << endl;
}


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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  vector<vector<int>> x(N), y(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    for (int j = 0; j < A.at(i); j++) {
      int a, b;
      cin >> a >> b;
      x.at(i).push_back(a - 1);
      y.at(i).push_back(b);
    }
  }

  int ans = 0;

  for (int i = 0; i < (1 << N); i++) {
    vector<int> honest(N);
    bool flag = true;
    for (int j = 0; j < N; j++) {
      honest.at(j) = (i >> j) % 2;
    }
    for (int j = 0; j < N; j++) {
      if (honest.at(j) == 0) {
        continue;
      }
      for (int k = 0; k < x.at(j).size(); k++) {
        if (y.at(j).at(k) != honest.at(x.at(j).at(k))) {
          flag = false;
          break;
        }
      }
      if (!flag) {
        break;
      }
    }
    if (flag) {
      int aa = 0;
      for (int j = 0; j < N; j++) {
        aa += honest.at(j);
      }
      ans = max(ans, aa);
    }
  }
  cout << ans << endl;
}
Problem p02837 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <limits>
#include <math.h>
#include <queue>
#include <vector>

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

int main() {
  int n;
  scanf("%d", &n);

  vector<vector<P>> xy;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    vector<P> tmp;
    for (int j = 0; j < a; ++j) {
      int x, y;
      cin >> x >> y;
      x--;
      tmp.push_back(P(x, y));
    }
    xy.push_back(tmp);
  }

  int ans = 0;
  for (int i = 0; i < 1 << n; ++i) {
    bool is_valid = true;
    for (int j = 0; j < n; ++j) {
      if (i >> j & 1) {
        for (int k = 0; k < n; ++k) {
          if (j == k)
            continue;
          for (int x = 0; x < xy[k].size(); ++x) {
            if ((i >> xy[j][x].first & 1) != xy[j][x].second)
              is_valid = false;
          }
        }
      }
    }
    if (is_valid) {
      int cnt = 0;
      for (int j = 0; j < n; ++j) {
        if (i >> j & 1)
          cnt++;
      }
      ans = max(cnt, ans);
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <limits>
#include <math.h>
#include <queue>
#include <vector>

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

int main() {
  int n;
  scanf("%d", &n);

  vector<vector<P>> xy;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    vector<P> tmp;
    for (int j = 0; j < a; ++j) {
      int x, y;
      cin >> x >> y;
      x--;
      tmp.push_back(P(x, y));
    }
    xy.push_back(tmp);
  }

  int ans = 0;
  for (int i = 0; i < 1 << n; ++i) {
    bool is_valid = true;
    for (int j = 0; j < n; ++j) {
      if (i >> j & 1) {
        for (int x = 0; x < xy[j].size(); ++x) {
          if ((i >> xy[j][x].first & 1) != xy[j][x].second)
            is_valid = false;
        }
      }
    }
    if (is_valid) {
      int cnt = 0;
      for (int j = 0; j < n; ++j) {
        if (i >> j & 1)
          cnt++;
      }
      ans = max(cnt, ans);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02837 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int countOnbit(int x) {
  if (x == 0)
    return 0;

  return countOnbit(x >> 1) + (x & 1);
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N + 1);
  vector<vector<int>> x(N + 1); // 指定した番号
  vector<vector<int>> y(N + 1); // 正直者であるか否か

  for (int i = 1; i <= N; i++) {
    cin >> A.at(i);
    for (int j = 1; j <= A.at(i); j++) {
      int a, b;
      cin >> a >> b;
      x[i].push_back(a);
      y[i].push_back(b);
    }
  }

  // 考え方メモ
  // 正直者か不親切な人かのパターンをビットで全て表す
  // ある人kが正直者でかつ証言に矛盾がない時、のビットの1を数える
  // ある人kが正直者である -> 右から(k - 1)番目のビットが1である
  // 矛盾がない時 -> 指定した番号のビット桁の数字が、パターンと等しい時

  int maxCount = 0;
  for (int bits = 1; bits < (1 << N); bits++) {
    bool isOk = true;
    for (int i = 1; i <= N; i++) {           // 人
      if (bits & (1 << (i - 1))) {           // ある人kが正直者である
        for (int j = 1; j <= A.at(i); j++) { // 証言
          if (y.at(i).at(j) == 1) {
            if (!((bits >> (x.at(i).at(j) - 1)) & 1))
              isOk = false;
          } else {
            if ((bits >> (x.at(i).at(j) - 1)) & 1)
              isOk = false;
          }
        }
      }
    }
    if (isOk)
      maxCount = max(maxCount, countOnbit(bits));
  }

  cout << maxCount << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int countOnbit(int x) {
  if (x == 0)
    return 0;

  return countOnbit(x >> 1) + (x & 1);
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N + 1);
  vector<vector<int>> x(N + 1); // 指定した番号
  vector<vector<int>> y(N + 1); // 正直者であるか否か

  for (int i = 1; i <= N; i++) {
    cin >> A.at(i);
    for (int j = 1; j <= A.at(i); j++) {
      int a, b;
      cin >> a >> b;
      x[i].push_back(a);
      y[i].push_back(b);
    }
  }

  // 考え方メモ
  // 正直者か不親切な人かのパターンをビットで全て表す
  // ある人kが正直者でかつ証言に矛盾がない時、のビットの1を数える
  // ある人kが正直者である -> 右から(k - 1)番目のビットが1である
  // 矛盾がない時 -> 指定した番号のビット桁の数字が、パターンと等しい時

  int maxCount = 0;
  for (int bits = 1; bits < (1 << N); bits++) {
    bool isOk = true;
    for (int i = 1; i <= N; i++) {          // 人
      if (bits & (1 << (i - 1))) {          // ある人kが正直者である
        for (int j = 0; j < A.at(i); j++) { // 証言
          if (y.at(i).at(j) == 1) {
            if (!((bits >> (x.at(i).at(j) - 1)) & 1))
              isOk = false;
          } else {
            if ((bits >> (x.at(i).at(j) - 1)) & 1)
              isOk = false;
          }
        }
      }
    }
    if (isOk)
      maxCount = max(maxCount, countOnbit(bits));
  }

  cout << maxCount << endl;

  return 0;
}
Problem p02837 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int N, A, X, Ans = 0;
  cin >> N;
  vector<vector<int>> V(N, vector<int>(N, -1));
  rep(i, N) {
    cin >> A;
    rep(j, A) {
      cin >> X;
      cin >> V.at(i).at(X - 1);
    }
  }
  rep(i, (1 << N)) {
    bitset<3> bs(i);
    bool B = 1;
    rep(j, N) {
      if (bs.test(j)) {
        rep(k, N) {
          if (V.at(j).at(k) != -1 && V.at(j).at(k) != bs[k]) {
            B = 0;
            break;
          }
        }
      }
    }
    if (B == 1)
      Ans = max(Ans, (int)bs.count());
  }
  cout << Ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int N, A, X, Ans = 0;
  cin >> N;
  vector<vector<int>> V(N, vector<int>(N, -1));
  rep(i, N) {
    cin >> A;
    rep(j, A) {
      cin >> X;
      cin >> V.at(i).at(X - 1);
    }
  }
  rep(i, (1 << N)) {
    bitset<15> bs(i);
    bool B = 1;
    rep(j, N) {
      if (bs.test(j)) {
        rep(k, N) {
          if (V.at(j).at(k) != -1 && V.at(j).at(k) != bs[k]) {
            B = 0;
            break;
          }
        }
      }
    }
    if (B == 1)
      Ans = max(Ans, (int)bs.count());
  }
  cout << Ans << endl;
}
Problem p02837 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using pp = pair<int, int>;

int main() {
  int N, doubt = 0, M = 0;
  cin >> N;
  vector<vector<pp>> V(N, vector<pp>(0));

  int A, p, q;

  for (int i = 0; i < N; i++) {
    cin >> A;
    for (int j = 0; j < A; j++) {
      cin >> p >> q;
      V[i].push_back(pp(p, q));
    }
  }

  for (int b = 0; b < (1 << N); b++) {
    bitset<12> s(b);
    vector<int> W(N);
    doubt = 0;

    for (int i = 0; i < N; i++) {
      W[i] = s.test(i);
      // cout << W[i] << endl;
    }

    for (int i = 0; i < N; i++) {
      if (s.test(i)) {
        for (int j = 0; j < (int)V[i].size(); j++) {
          int x = V[i][j].first, y = V[i][j].second;
          if (W[x - 1] != y) {
            doubt = 1;
            break;
          }
        }
        if (doubt == 1) {
          break;
        }
      }
    }
    if (doubt == 0) {
      int rr = 0;
      for (int i = 0; i < N; i++) {
        rr += s.test(i);
      }
      M = max(M, rr);
      // cout << M << endl;
    }
  }
  cout << M << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using pp = pair<int, int>;

int main() {
  int N, doubt = 0, M = 0;
  cin >> N;
  vector<vector<pp>> V(N, vector<pp>(0));

  int A, p, q;

  for (int i = 0; i < N; i++) {
    cin >> A;
    for (int j = 0; j < A; j++) {
      cin >> p >> q;
      V[i].push_back(pp(p, q));
    }
  }

  for (int b = 0; b < (1 << N); b++) {
    bitset<15> s(b);
    vector<int> W(N);
    doubt = 0;

    for (int i = 0; i < N; i++) {
      W[i] = s.test(i);
      // cout << W[i] << endl;
    }

    for (int i = 0; i < N; i++) {
      if (s.test(i)) {
        for (int j = 0; j < (int)V[i].size(); j++) {
          int x = V[i][j].first, y = V[i][j].second;
          if (W[x - 1] != y) {
            doubt = 1;
            break;
          }
        }
        if (doubt == 1) {
          break;
        }
      }
    }
    if (doubt == 0) {
      int rr = 0;
      for (int i = 0; i < N; i++) {
        rr += s.test(i);
      }
      M = max(M, rr);
      // cout << M << endl;
    }
  }
  cout << M << endl;
}
Problem p02837 (Python)

Buggy submission (status: Runtime Error):
n = int(input())

testimony_dict = {}
for i in range(n):
    a = int(input())
    testimony_dict[i + 1] = []
    for j in range(a):
        xy = input().split(" ")
        testimony_dict[i + 1].append(xy)

max_valid_bin = ""
for i in range(2**n - 1, 0, -1):
    is_valid = True
    bin_i = format(i, f"0{n}b")
    for j in range(1, len(bin_i) + 1):
        if bin_i[j - 1] == "1":
            for xy_list in testimony_dict[j]:
                if bin_i[int(xy_list[0]) - 1] != xy_list[1]:
                    is_valid = False
    if is_valid:
        max_valid_bin = bin_i
        break

print(len([i for i in max_valid_bin if i == "1"]))


Fixed submission:
n = int(input())

testimony_dict = {}
for i in range(n):
    a = int(input())
    testimony_dict[i + 1] = []
    for j in range(a):
        xy = input().split(" ")
        testimony_dict[i + 1].append(xy)

max_valid_bin = ""
for i in range(2**n - 1, 0, -1):
    is_valid = True
    bin_i = format(i, "0{}b".format(n))
    for j in range(1, len(bin_i) + 1):
        if bin_i[j - 1] == "1":
            for xy_list in testimony_dict[j]:
                if bin_i[int(xy_list[0]) - 1] != xy_list[1]:
                    is_valid = False
    if is_valid:
        max_valid_bin = bin_i
        break

print(len([i for i in max_valid_bin if i == "1"]))
Problem p02837 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<vector<vector<int>>> ta(n, vector<vector<int>>(n, vector<int>(1, 66)));
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    for (int j = 0; j < a; j++) {
      int x, y;
      cin >> x >> y;
      x--;
      ta[i][x][0] = y;
    }
  }

  vector<int> seen(n);
  int ans = 0;
  for (int i = 0; i < (1 << n); i++) {
    bitset<3> s(i);
    int d = 0;
    int cu = 0;
    for (int j = 0; j < n; j++) {
      if (s.test(j))
        seen[j] = 1;
      else
        seen[j] = 0;
    }
    for (int h = 0; h < n; h++) {
      if (seen[h]) {
        for (int g = 0; g < n; g++) {

          if (ta[h][g][0] == 1) {
            if (seen[g] != 1) {
              cu = 1;
              break;
            }
          } else if (ta[h][g][0] == 0) {
            if (seen[g] != 0) {
              cu = 1;
              break;
            }
          }
        }
      }
      if (cu)
        break;
    }

    if (cu)
      continue;
    for (int u = 0; u < n; u++) {
      if (seen[u])
        d++;
    }
    ans = max(ans, d);
  }
  cout << ans << endl;
}


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

int main() {
  int n;
  cin >> n;
  vector<vector<vector<int>>> ta(n, vector<vector<int>>(n, vector<int>(1, 66)));
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    for (int j = 0; j < a; j++) {
      int x, y;
      cin >> x >> y;
      x--;
      ta[i][x][0] = y;
    }
  }

  vector<int> seen(n);
  int ans = 0;
  for (int i = 0; i < (1 << n); i++) {
    bitset<15> s(i);
    int d = 0;
    int cu = 0;
    for (int j = 0; j < n; j++) {
      if (s.test(j))
        seen[j] = 1;
      else
        seen[j] = 0;
    }
    for (int h = 0; h < n; h++) {
      if (seen[h]) {
        for (int g = 0; g < n; g++) {

          if (ta[h][g][0] == 1) {
            if (seen[g] != 1) {
              cu = 1;
              break;
            }
          } else if (ta[h][g][0] == 0) {
            if (seen[g] != 0) {
              cu = 1;
              break;
            }
          }
        }
      }
      if (cu)
        break;
    }

    if (cu)
      continue;
    for (int u = 0; u < n; u++) {
      if (seen[u])
        d++;
    }
    ans = max(ans, d);
  }
  cout << ans << endl;
}
Problem p02837 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;

  vector<vector<int>> x(n + 1);
  vector<vector<int>> y(n + 1);
  vector<int> a(n + 1);

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

    x[i].resize(a[i]);
    y[i].resize(a[i]);
    for (int j = 1; j <= a[i]; j++) {
      cin >> x[i][j] >> y[i][j];
    }
  }

  int ans = 0;
  for (int mask = 1; mask < (1 << n); mask++) {

    bool ok = true;
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
      if (!(mask & (1 << (i - 1))))
      //   if (!(mask >> (i - 1) & 1))
      {
        continue;
      }
      cnt++;
      for (int j = 1; j <= a[i]; j++) {
        // x[i][j]-1番目の人が正直ならビットは1なはず
        if (((mask >> (x[i][j] - 1)) & 1) ^ y[i][j]) {
          ok = false;
        }
      }
    }

    if (ok) {
      ans = max(ans, cnt);
    }
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;

  vector<vector<int>> x(n + 1);
  vector<vector<int>> y(n + 1);
  vector<int> a(n + 1);

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

    x[i].resize(a[i] + 1);
    y[i].resize(a[i] + 1);
    for (int j = 1; j <= a[i]; j++) {
      cin >> x[i][j] >> y[i][j];
    }
  }

  int ans = 0;
  for (int mask = 1; mask < (1 << n); mask++) {

    bool ok = true;
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
      if (!(mask & (1 << (i - 1))))
      //   if (!(mask >> (i - 1) & 1))
      {
        continue;
      }
      cnt++;
      for (int j = 1; j <= a[i]; j++) {
        // x[i][j]-1番目の人が正直ならビットは1なはず
        if (((mask >> (x[i][j] - 1)) & 1) ^ y[i][j]) {
          ok = false;
        }
      }
    }

    if (ok) {
      ans = max(ans, cnt);
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p02837 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  scanf("%d", &n);
  vector<vector<pair<int, int>>> a(n);
  for (int i = 0; i < n; ++i) {
    int k;
    scanf("%d", &k);
    a[i].resize(k);
    for (int j = 0; j < k; ++j) {
      scanf("%d %d", &a[i][j].first, &a[i][j].second);
      --a[i][j].first;
    }
  }
  int ans = 0;
  for (int mask = 0; mask < (1 << n); ++mask) {
    bool ok = true;
    for (int i = 0; i < n; ++i) {
      if ((mask >> i) & 1) {
        for (int j = 0; j < int(a[i].size()); ++i) {
          ok &= (((mask >> a[i][j].first) & 1) == a[i][j].second);
        }
      }
    }
    if (ok)
      ans = max(ans, __builtin_popcount(mask));
  }
  printf("%d\n", ans);
}

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

int main() {
  int n;
  scanf("%d", &n);
  vector<vector<pair<int, int>>> a(n);
  for (int i = 0; i < n; ++i) {
    int k;
    scanf("%d", &k);
    a[i].resize(k);
    for (int j = 0; j < k; ++j) {
      scanf("%d %d", &a[i][j].first, &a[i][j].second);
      --a[i][j].first;
    }
  }
  int ans = 0;
  for (int mask = 0; mask < (1 << n); ++mask) {
    bool ok = true;
    for (int i = 0; i < n; ++i) {
      if ((mask >> i) & 1) {
        for (int j = 0; j < int(a[i].size()); ++j) {
          ok &= (((mask >> a[i][j].first) & 1) == a[i][j].second);
        }
      }
    }
    if (ok)
      ans = max(ans, __builtin_popcount(mask));
  }
  printf("%d\n", ans);
}
Problem p02837 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ll long long
#define lld long double
#define ALL(x) x.begin(), x.end()
#ifdef DEBUG
#define line() cerr << "[" << __LINE__ << "] ";
#define dump(i) cerr << #i ": " << i << " ";
#define dumpl(i) cerr << #i ": " << i << endl;
#else
#define line(i)
#define dump(i)
#define dumpl(i)
#endif
using namespace std;
int n;

int ok(vector<vector<int>> &v, int m)

{
  int d[n];
  int ret = 0;
  rep(i, m) {
    d[i] = (m >> i) & 1;
    if (d[i] == 1)
      ret++;
  }
  rep(i, n) {
    rep(j, n) {
      if (v[i][j] == -1)
        continue;
      if (d[i] == 1 && d[j] != v[i][j]) {
        return 0;
      }
    }
  }
  return ret;
}

int main(int argc, char const *argv[]) {
  cin >> n;
  vector<vector<int>> v(n, vector<int>(n, -1));
  rep(i, n) {
    int m;
    cin >> m;
    rep(j, m) {
      int x, y;
      cin >> x >> y;
      x--;
      v[i][x] = y;
    }
  }
  cerr << "ok" << pow(2, n) << endl;
  int ans = 0;
  rep(i, pow(2, n)) {
    cerr << "*" << i << endl;
    ans = max(ans, ok(v, i));
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ll long long
#define lld long double
#define ALL(x) x.begin(), x.end()
#ifdef DEBUG
#define line() cerr << "[" << __LINE__ << "] ";
#define dump(i) cerr << #i ": " << i << " ";
#define dumpl(i) cerr << #i ": " << i << endl;
#else
#define line(i)
#define dump(i)
#define dumpl(i)
#endif
using namespace std;
int n;

int ok(vector<vector<int>> &v, int m)

{
  int d[n];
  int ret = 0;
  rep(i, n) {
    d[i] = (m >> i) & 1;
    if (d[i] == 1)
      ret++;
  }
  rep(i, n) {
    rep(j, n) {
      if (v[i][j] == -1)
        continue;
      if (d[i] == 1 && d[j] != v[i][j]) {
        return 0;
      }
    }
  }
  return ret;
}

int main(int argc, char const *argv[]) {
  cin >> n;
  vector<vector<int>> v(n, vector<int>(n, -1));
  rep(i, n) {
    int m;
    cin >> m;
    rep(j, m) {
      int x, y;
      cin >> x >> y;
      x--;
      v[i][x] = y;
    }
  }
  cerr << "ok" << pow(2, n) << endl;
  int ans = 0;
  rep(i, pow(2, n)) {
    cerr << "*" << i << endl;
    ans = max(ans, ok(v, i));
  }
  cout << ans << endl;
  return 0;
}
Problem p02838 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>

long a[300010], s[64][300010][2];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%ld", a + i);
    for (int j = 0; j < 61; ++j) {
      int k = (a[i] >> j) & 1;
      if (k == 0) {
        s[j][i + 1][0] = s[j][i][0] + 1;
        s[j][i + 1][1] = s[j][i][1];
      } else {
        s[j][i + 1][0] = s[j][i][0];
        s[j][i + 1][1] = s[j][i][1] + 1;
      }
    }
  }

  const long mod = 1e9 + 7;
  long res = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < 61; ++j) {
      int x = (a[i] >> j) & 1;
      long t = s[j][n][!x] - s[j][i][!x];
      t *= (1L << j) % mod;
      while (t >= mod)
        t -= mod;
      res += t;
      if (res >= mod)
        res -= mod;
    }
  }
  printf("%ld\n", res);
}


Fixed submission:
#include <cstdio>

long a[300010], s[64][300010][2];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%ld", a + i);
    for (int j = 0; j < 61; ++j) {
      int k = (a[i] >> j) & 1;
      if (k == 0) {
        s[j][i + 1][0] = s[j][i][0] + 1;
        s[j][i + 1][1] = s[j][i][1];
      } else {
        s[j][i + 1][0] = s[j][i][0];
        s[j][i + 1][1] = s[j][i][1] + 1;
      }
    }
  }

  const long mod = 1e9 + 7;
  long res = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < 61; ++j) {
      int x = (a[i] >> j) & 1;
      long t = s[j][n][!x] - s[j][i][!x];
      (t *= (1L << j) % mod) %= mod;
      res += t;
      if (res >= mod)
        res -= mod;
    }
  }
  printf("%ld\n", res);
}
Problem p02838 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pb push_back
#define f first
#define s second
#define MOD 1000000007

using namespace std;

uint64_t A[100001];

ll modexp(int i, ll p) {
  if (p == 0)
    return 1;
  ll ans = modexp(i, p / 2);
  ans = (ans * ans) % MOD;
  if (p % 2)
    ans = (i * ans) % MOD;
  return ans;
}

void solve() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  ll ans = 0;
  for (int bit = 0; bit < 60; bit++) {
    ll x = 0;
    for (int i = 0; i < n; i++) {
      if ((A[i] & (1ULL << bit))) {
        x++;
      }
    }
    // cout<<bit<<" : "<<x<<" sex\n";
    x = (x * (n - x)) % MOD;
    ans += (modexp(2, bit) * x) % MOD;
    ans = ans % MOD;
  }
  cout << ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(12);
  int t = 1; // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
    cout << "\n";
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pb push_back
#define f first
#define s second
#define MOD 1000000007

using namespace std;

uint64_t A[300001];

ll modexp(int i, ll p) {
  if (p == 0)
    return 1;
  ll ans = modexp(i, p / 2);
  ans = (ans * ans) % MOD;
  if (p % 2)
    ans = (i * ans) % MOD;
  return ans;
}

void solve() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  ll ans = 0;
  for (int bit = 0; bit < 60; bit++) {
    ll x = 0;
    for (int i = 0; i < n; i++) {
      if ((A[i] & (1ULL << bit))) {
        x++;
      }
    }
    // cout<<bit<<" : "<<x<<" sex\n";
    x = (x * (n - x)) % MOD;
    ans += (modexp(2, bit) * x) % MOD;
    ans = ans % MOD;
  }
  cout << ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(12);
  int t = 1; // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
    cout << "\n";
  }
  return 0;
}
Problem p02838 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define sort(A) sort(A.begin(), A.end())
#define reverse(A) reverse(A.begin(), A.end())
typedef long long ll;
typedef pair<int, int> P;

int main() {
  const int INF = pow(10, 9) + 7;
  int n;
  cin >> n;
  vector<ll> A(n);
  rep(i, n) { cin >> A[i]; }
  ll ans = 0;
  rep(i, n - 1) {
    for (int j = i + 1; j < n; j++) {
      ans += A[i] ^ A[j];
      ans %= INF;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define sort(A) sort(A.begin(), A.end())
#define reverse(A) reverse(A.begin(), A.end())
typedef long long ll;
typedef pair<int, int> P;

int main() {
  const int INF = pow(10, 9) + 7;
  int n;
  cin >> n;
  vector<ll> A(n);
  rep(i, n) { cin >> A[i]; }
  ll ans = 0;
  rep(i, 60) {
    ll x = 0;
    rep(j, n) if (A[j] >> i & 1) x++;
    ll now = x * (n - x);
    rep(j, i) now = now * 2 % INF;
    ans += now;
    ans %= INF;
  }
  cout << ans << endl;
}
Problem p02838 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, s, n) for (ll i = s; i < n; i++)
#define repe(i, s, n) for (ll i = s; i <= n; i++)
#define rep_r(i, s, n) for (ll i = n - 1; i > s; i--)
#define rep_re(i, s, n) for (ll i = n - 1; i >= s; i--)
ll MOD = 1e9 + 7;

ll a[100000] = {};
ll Pow(ll n, ll p) {
  if (p == 0)
    return 1;
  if (p == 1)
    return n;
  ll t = Pow(n, p / 2);
  if (p & 1)
    return (t * t % MOD) * n % MOD;
  else
    return t * t % MOD;
}
int main() {
  ll n;
  cin >> n;
  rep(i, 0, n) cin >> a[i];

  ll ans = 0;
  rep(i, 0, 60) {
    ll mask = 1ll << i;

    ll z = 0, o = 0;
    rep(i, 0, n) {
      ll v = mask & a[i];
      if (v == 0) {
        z++;
      } else {
        o++;
      }
    }
    ll p = z * o % MOD;
    ans = (ans + (Pow(2, i) * p) % MOD) % MOD;
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, s, n) for (ll i = s; i < n; i++)
#define repe(i, s, n) for (ll i = s; i <= n; i++)
#define rep_r(i, s, n) for (ll i = n - 1; i > s; i--)
#define rep_re(i, s, n) for (ll i = n - 1; i >= s; i--)
ll MOD = 1e9 + 7;

ll a[300001] = {};
ll Pow(ll n, ll p) {
  if (p == 0)
    return 1;
  if (p == 1)
    return n;
  ll t = Pow(n, p / 2);
  if (p & 1)
    return (t * t % MOD) * n % MOD;
  else
    return t * t % MOD;
}
int main() {
  ll n;
  cin >> n;
  rep(i, 0, n) cin >> a[i];

  ll ans = 0;
  rep(i, 0, 60) {
    ll mask = 1ll << i;

    ll z = 0, o = 0;
    rep(i, 0, n) {
      ll v = mask & a[i];
      if (v == 0) {
        z++;
      } else {
        o++;
      }
    }
    ll p = z * o % MOD;
    ans = (ans + (Pow(2, i) * p) % MOD) % MOD;
  }

  cout << ans << endl;

  return 0;
}
Problem p02838 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;

ll N, cnt[100000][63], A[100000], sum[63];

int main(void) {
  cin >> N;
  rep(i, N) {
    cin >> A[i];
    ll x = A[i];
    ll m = 0;
    while (x > 0) {
      if (x & 1 == 1)
        cnt[i][m]++;
      x = x >> 1;
      m++;
    }
    rep(j, 63) sum[j] += cnt[i][j];
    // rep(j,3)cout << sum[j];
    // cout << endl;
  }

  ll ans = 0;
  rep(i, N) {
    ll p = 1;
    rep(j, 63) {
      sum[j] -= cnt[i][j];
      if (cnt[i][j] == 0)
        ans = (ans + (sum[j] * p) % MOD) % MOD;
      else
        ans = (ans + ((N - 1 - i - sum[j]) * p) % MOD) % MOD;
      p = (2 * p) % MOD;
    }
  }
  ans = ans % MOD;
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;

ll N, cnt[300000][63], A[300000], sum[63];

int main(void) {
  cin >> N;
  rep(i, N) {
    cin >> A[i];
    ll x = A[i];
    ll m = 0;
    while (x > 0) {
      if (x & 1 == 1)
        cnt[i][m]++;
      x = x >> 1;
      m++;
    }
    rep(j, 63) sum[j] += cnt[i][j];
    // rep(j,3)cout << sum[j];
    // cout << endl;
  }

  ll ans = 0;
  rep(i, N) {
    ll p = 1;
    rep(j, 63) {
      sum[j] -= cnt[i][j];
      if (cnt[i][j] == 0)
        ans = (ans + (sum[j] * p) % MOD) % MOD;
      else
        ans = (ans + ((N - 1 - i - sum[j]) * p) % MOD) % MOD;
      p = (2 * p) % MOD;
    }
  }
  ans = ans % MOD;
  cout << ans << endl;
}
Problem p02838 (C++)

Buggy submission (status: Time Limit Exceeded):
#define rep(i, n) for (ll i = 0; i < n; i++)
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  ll n;
  ll p = 1e9 + 7;
  cin >> n;
  vector<ll> cnt(61, 0);
  rep(i, n) {
    ll a;
    cin >> a;
    ll j = 0;
    while (a >= 1) {
      if (a % 2 == 1)
        cnt[j]++;
      a /= 2;
      j++;
    }
  }
  ll ans = 0;
  rep(i, 61) {
    ll base = cnt[i] * (n - cnt[i]);
    // printf("%lld, %lld\n", i, cnt[i]);
    rep(j, base) {
      ans += ((1LL << i) % p);
      ans = ans % p;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#define rep(i, n) for (ll i = 0; i < n; i++)
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  ll n;
  ll p = 1e9 + 7;
  cin >> n;
  vector<ll> cnt(61, 0);
  rep(i, n) {
    ll a;
    cin >> a;
    ll j = 0;
    while (a >= 1) {
      if (a % 2 == 1)
        cnt[j]++;
      a /= 2;
      j++;
    }
  }
  ll ans = 0;
  rep(i, 61) {
    ll base = cnt[i] * (n - cnt[i]);
    // printf("%lld, %lld\n", i, cnt[i]);
    ll tmp = base;
    rep(j, i) tmp = ((tmp << 1) % p);
    ans = (ans + tmp) % p;
  }
  cout << ans << endl;
  return 0;
}
Problem p02838 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  const ll mod = 1e9 + 7;

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

  // bits[i][j][0or1] := i番目以降の数にj桁目が0or1になる数がいくつあるか
  vector<vector<vector<ll>>> bits(n, vector<vector<ll>>(60, vector<ll>(2)));

  for (ll i = n - 1; i >= 0; i--) {
    for (ll j = 0; j < 60; j++) {
      if (i < n - 1) {
        bits[i][j][0] = bits[i + 1][j][0];
        bits[i][j][1] = bits[i + 1][j][1];
      }
      if (a[i] & (1ll << j)) {
        bits[i][j][1]++;
      } else {
        bits[i][j][0]++;
      }
    }
  }

  ll ans = 0;
  for (ll i = 0; i < n - 1; i++) {
    for (ll j = 0; j < 60; j++) {
      if (a[i] & (1ll << j)) {
        for (int k = 0; k < bits[i + 1][j][0]; k++) {
          ans += (1ll << j);
          ans %= mod;
        }
        // ans += ((1ll << j) % mod) * bits[i + 1][j][0];
        // ans %= mod;
      } else {
        for (int k = 0; k < bits[i + 1][j][1]; k++) {
          ans += (1ll << j);
          ans %= mod;
        }
        // ans += ((1ll << j) % mod) * bits[i + 1][j][1];
        // ans %= mod;
      }
    }
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  const ll mod = 1e9 + 7;

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

  // bits[i][j][0or1] := i番目以降の数にj桁目が0or1になる数がいくつあるか
  vector<vector<vector<ll>>> bits(n, vector<vector<ll>>(60, vector<ll>(2)));

  for (ll i = n - 1; i >= 0; i--) {
    for (ll j = 0; j < 60; j++) {
      if (i < n - 1) {
        bits[i][j][0] = bits[i + 1][j][0];
        bits[i][j][1] = bits[i + 1][j][1];
      }
      if (a[i] & (1ll << j)) {
        bits[i][j][1]++;
      } else {
        bits[i][j][0]++;
      }
    }
  }

  ll ans = 0;
  // for (ll i = 0; i < n - 1; i++) {
  //   for (ll j = 0; j < 60; j++) {
  //     if (a[i] & (1ll << j)) {
  //       ans += ((1ll << j) % mod) * bits[i + 1][j][0];
  //       ans %= mod;
  //     } else {
  //       ans += ((1ll << j) % mod) * bits[i + 1][j][1];
  //       ans %= mod;
  //     }
  //   }
  // }
  for (ll j = 0; j < 60; j++) {
    ans += (1ll << j) % mod * bits[0][j][0] % mod * bits[0][j][1] % mod;
    ans %= mod;
  }
  cout << ans << endl;

  return 0;
}
Problem p02838 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
typedef long long ll;
typedef pair<int, int> P;

ll dis[100000] = {};

int main() {
  int n, i, j;
  ll a, tem, res = 0, count[60][2] = {};
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a;
    tem = 1;
    for (j = 0; j < 60; j++) {
      count[j][(int)((tem & a) > 0)]++;
      tem <<= 1;
    }
  }
  tem = 1;
  for (i = 0; i < n; i++) {
    res += ((tem % MOD) * (count[i][0] * count[i][1] % MOD)) % MOD;
    res %= MOD;
    tem <<= 1;
  }
  cout << res;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
typedef long long ll;
typedef pair<int, int> P;

ll dis[100000] = {};

int main() {
  int n, i, j;
  ll a, tem, res = 0, count[60][2] = {};
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a;
    tem = 1;
    for (j = 0; j < 60; j++) {
      count[j][(int)((tem & a) > 0)]++;
      tem <<= 1;
    }
  }
  tem = 1;
  for (i = 0; i < 60; i++) {
    res += ((tem % MOD) * (count[i][0] * count[i][1] % MOD)) % MOD;
    res %= MOD;
    tem <<= 1;
  }
  cout << res;
  return 0;
}
Problem p02838 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

int main() {
  int N;
  cin >> N;
  ll *a = new ll[N];
  for (int i = 0; i < N; i++)
    cin >> a[i];

  ll mod = 1e9 + 7;

  vector<vector<int>> b(N, vector<int>(60));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 60; j++) {
      b[i][j] = a[i] % 2;
      a[i] /= 2;
    }
  }
  /*
  for (int i = 0; i < N; i++) {
      for (int j = 0; j < 60; j++)
          cout << b[i][j];
      cout << endl;
  }
  */

  vector<vector<ll>> p(N, vector<ll>(60)), q(N, vector<ll>(60));
  for (int k = 0; k < 60; k++) {
    p[N - 1][k] = (1 - b[N - 1][k]);
    q[N - 1][k] = b[N - 1][k];

    for (int i = N - 1; i > 0; i--) {
      p[i - 1][k] = p[i][k] + (1 - b[i - 1][k]);
      q[i - 1][k] = q[i][k] + b[i - 1][k];
    }
  }
  /*
  cout << endl;
  for (int i = 0; i < N; i++) {
      for (int k = 0; k < 60; k++)
          cout << p[i][k];
      cout << endl;
  }
  */

  ll ans = 0, tmp = 1;
  for (int k = 0; k < 60; k++) {
    for (int i = 0; i < N - 1; i++) {
      if (b[i][k] == 0)
        ans += tmp * q[i + 1][k] % mod;
      else
        ans += tmp * p[i + 1][k] % mod;
      ans %= mod;
    }
    tmp *= 2;
    tmp %= mod;
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

int main() {
  int N;
  cin >> N;
  ll *a = new ll[N];
  for (int i = 0; i < N; i++)
    cin >> a[i];

  ll mod = 1e9 + 7;

  vector<vector<int>> b(N, vector<int>(60));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 60; j++) {
      b[i][j] = a[i] % 2;
      a[i] /= 2;
    }
  }
  /*
  for (int i = 0; i < N; i++) {
      for (int j = 0; j < 60; j++)
          cout << b[i][j];
      cout << endl;
  }
  */

  vector<vector<ll>> p(N, vector<ll>(60)), q(N, vector<ll>(60));
  for (int k = 0; k < 60; k++) {
    p[N - 1][k] = (1 - b[N - 1][k]);
    q[N - 1][k] = b[N - 1][k];

    for (int i = N - 1; i > 0; i--) {
      p[i - 1][k] = p[i][k] + (1 - b[i - 1][k]);
      q[i - 1][k] = q[i][k] + b[i - 1][k];
    }
  }
  /*
  cout << endl;
  for (int i = 0; i < N; i++) {
      for (int k = 0; k < 60; k++)
          cout << p[i][k];
      cout << endl;
  }
  */

  ll ans = 0, tmp = 1;
  for (int k = 0; k < 60; k++) {
    ans += tmp * p[0][k] % mod * q[0][k] % mod;
    ans %= mod;
    tmp *= 2;
    tmp %= mod;
  }

  cout << ans << endl;
  return 0;
}
Problem p02838 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long
using pi = pair<int, int>;
#define mp make_pair
const int mod = 1e9 + 7;

signed main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (auto &i : a)
    cin >> i;
  int suma = 0, land = 0;
  for (auto i : a)
    suma = (suma + i) % mod;
  int res = 0;
  for (auto i : a)
    for (auto j : a)
      land = (land + (i & j)) % mod;
  res = (n * suma - land) % mod;
  cout << res;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long
using pi = pair<int, int>;
#define mp make_pair
const int mod = 1e9 + 7;

signed main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (auto &i : a)
    cin >> i;
  int suma = 0, land = 0;
  for (auto i : a)
    suma = (suma + i) % mod;
  int res = 0;

  bool ref = true;
  int p = 1, cnt, ps = 0;
  while (ref) {
    ref = false;
    cnt = ps = 0;
    for (auto &i : a)
      if (i) {
        ref = true;
        if (i & 1)
          ++cnt;
        i >>= 1;
      }
    ps = cnt % mod * cnt % mod * p % mod;
    land = (land + ps) % mod;
    p <<= 1;
    p %= mod;
  }
  res = suma % mod * n % mod;
  res = (res - land) % mod;
  while (res < 0)
    res += mod;
  cout << res % mod << endl;
}
Problem p02838 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int MOD = 1e9 + 7, MAX_N = 30006;
int N;
ll A[MAX_N];
ll bit0[70], bit1[70]; // how many 0's and 1's have we seen at bit i

void solve() {
  ll x = A[N - 1];
  for (int i = 0; i < 62; ++i) {
    if (x >> i & 1)
      bit1[i]++;
    else
      bit0[i]++;
  }
  ll res = 0;
  for (int i = N - 2; i >= 0; --i) {
    ll x = A[i];
    for (int j = 0; j < 62; ++j) {
      if (x >> j & 1)
        res = (res + (bit0[j] % MOD) * ((1LL << j) % MOD)) % MOD, bit1[j] += 1;
      else
        res = (res + (bit1[j] % MOD) * ((1LL << j) % MOD)) % MOD, bit0[j] += 1;
    }
  }
  cout << res << "\n";
}

int main() {
  cin >> N;
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int MOD = 1e9 + 7, MAX_N = 300006;
int N;
ll A[MAX_N];
ll bit0[70], bit1[70]; // how many 0's and 1's have we seen at bit i

void solve() {
  ll x = A[N - 1];
  for (int i = 0; i < 62; ++i) {
    if (x >> i & 1)
      bit1[i]++;
    else
      bit0[i]++;
  }
  ll res = 0;
  for (int i = N - 2; i >= 0; --i) {
    ll x = A[i];
    for (int j = 0; j < 62; ++j) {
      if (x >> j & 1)
        res = (res + (bit0[j] % MOD) * ((1LL << j) % MOD)) % MOD, bit1[j] += 1;
      else
        res = (res + (bit1[j] % MOD) * ((1LL << j) % MOD)) % MOD, bit0[j] += 1;
    }
  }
  cout << res << "\n";
}

int main() {
  cin >> N;
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  solve();
  return 0;
}
Problem p02838 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define f first
#define s second
#define MOD 1000000007
#define PMOD 998244353
#define pb(x) push_back(x)
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plii;
typedef pair<int, pii> piii;
const int INF = 1e9 + 10;
const ll LINF = 1LL * INF * INF;
const int MAXN = 2e5 + 10;
const int MAXM = 5e3 + 10;

priority_queue<int> pq;
vector<vector<int>> graph;
queue<int> que;

ll A[MAXN];
ll cal[110];

int main() {
  ll n, m, k, a, b, x, y, q;
  int sum = 0;
  int cnt = 0;
  int mx = 0;
  int mn = INF;
  ll cur = 0, idx = -1;
  int tc;

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> A[i];
    cur = A[i];
    for (int j = 0; j < 60; j++) {
      cal[j] += (cur & 1);
      cur >>= 1;
    }
  }

  ll res = 0;
  ll bi = 1;

  for (int i = 0; i < 60; i++) {

    cur = cal[i] * (n - cal[i]);
    cur %= MOD;
    res += (bi * cur);
    res %= MOD;
    bi <<= 1;
    if (bi >= MOD)
      bi -= MOD;
  }

  cout << res << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define f first
#define s second
#define MOD 1000000007
#define PMOD 998244353
#define pb(x) push_back(x)
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plii;
typedef pair<int, pii> piii;
const int INF = 1e9 + 10;
const ll LINF = 1LL * INF * INF;
const int MAXN = 3e5 + 10;
const int MAXM = 5e3 + 10;

priority_queue<int> pq;
vector<vector<int>> graph;
queue<int> que;

ll A[MAXN];
ll cal[110];

int main() {
  ll n, m, k, a, b, x, y, q;
  int sum = 0;
  int cnt = 0;
  int mx = 0;
  int mn = INF;
  ll cur = 0, idx = -1;
  int tc;

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> A[i];
    cur = A[i];
    for (int j = 0; j < 60; j++) {
      cal[j] += (cur & 1);
      cur >>= 1;
    }
  }

  ll res = 0;
  ll bi = 1;

  for (int i = 0; i < 60; i++) {

    cur = cal[i] * (n - cal[i]);
    cur %= MOD;
    res += (bi * cur);
    res %= MOD;
    bi <<= 1;
    if (bi >= MOD)
      bi -= MOD;
  }

  cout << res << "\n";

  return 0;
}
Problem p02838 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))

result = 0
for i in range(60):
    on = sum([(1 << i) & a for a in A]) >> i
    result += (on * (N - on)) << i
    result %= (10**9) + 7

print(result)


Fixed submission:
N = int(input())
A = list(map(int, input().split()))

result = 0
for i in range(60):
    target = 1 << i
    on = sum([target & a for a in A]) >> i
    result += (on * (N - on)) << i
    result %= (10**9) + 7

print(result)
Problem p02838 (Python)

Buggy submission (status: Runtime Error):
def solve(string):
    n, *a = map(int, string.split())
    a = ("{:060b}".format(_a) for _a in a)
    m = 10**9 + 7
    ans = 0
    for s in map(str.count("1"), zip(*a)):
        ans <<= 1
        ans += s * (n - s)
        ans %= m
    return str(ans)


if __name__ == "__main__":
    import sys

    print(solve(sys.stdin.read().strip()))


Fixed submission:
def solve(string):
    n, *a = map(int, string.split())
    a = ("{:060b}".format(_a) for _a in a)
    m = 10**9 + 7
    ans = 0
    for s in map(lambda x: x.count("1"), zip(*a)):
        ans <<= 1
        ans += s * (n - s)
        ans %= m
    return str(ans)


if __name__ == "__main__":
    import sys

    print(solve(sys.stdin.read().strip()))
Problem p02838 (Python)

Buggy submission (status: Time Limit Exceeded):
#!/usr/bin/env python3
n, *a = map(int, open(0).read().split())
b = [0] * 60
ans = 0
for i in range(n):
    for j in range(60):
        ans += 2**j * (i - b[j] if a[i] >> j & 1 else b[j])
        ans %= 10**9 + 7
    for j in range(len(bin(a[i])) - 2):
        b[j] += a[i] >> j & 1
print(ans)


Fixed submission:
#!/usr/bin/env python3
n, *a = map(int, open(0).read().split())
b = [0] * 60
ans = 0
for i in range(n):
    for j in range(60):
        ans += (i - b[j] if a[i] >> j & 1 else b[j]) << j
    ans %= 10**9 + 7
    for j in range(60):
        b[j] += a[i] >> j & 1
print(ans)
Problem p02838 (Python)

Buggy submission (status: Runtime Error):
import numpy as np

N = int(input())
A = np.fromstring(input(), np.int64, " ")

result = 0
for bit in range(60):
    c = int((A & 1).sum())
    A >>= 1
    result = (result + c * (N - c) * (1 << bit)) % 1000000007
print(result)


Fixed submission:
import numpy as np

N = int(input())
A = np.fromstring(input(), dtype=np.int64, sep=" ")

result = 0
for bit in range(60):
    c = int((A & 1).sum())
    A >>= 1
    result = (result + c * (N - c) * (1 << bit)) % 1000000007
print(result)
Problem p02838 (Python)

Buggy submission (status: Time Limit Exceeded):
def main():
    MOD = 10**9 + 7

    N = int(input())
    (*a,) = map(int, input().split())

    b = [0] * 60
    for x in a:
        for j in range(60):
            b[j] += (x & 1) > 0
            x >>= 1

    t = 1
    ans = 0
    for j in range(60):
        ans = (ans + b[j] * (N - b[j]) * t) % MOD
        t <<= 1
    print(ans)


if __name__ == "__main__":
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())


Fixed submission:
def main():
    MOD = 10**9 + 7

    N = int(input())
    (*a,) = map(int, input().split())

    b = [0] * 60
    for x in a:
        for j in range(60):
            b[j] += (x & 1) > 0
            x >>= 1

    t = 1
    ans = 0
    for j in range(60):
        ans = (ans + b[j] * (N - b[j]) * t) % MOD
        t <<= 1
    print(ans)


if __name__ == "__main__":
    main()
Problem p02838 (Python)

Buggy submission (status: Time Limit Exceeded):
MOD = 10**9 + 7
N = int(input())
A = list(map(int, input().split()))

ans = 0
for i in range(N - 1):
    for j in range(i + 1, N):
        ans += A[i] ^ A[j]
        ans %= MOD
print(ans)


Fixed submission:
MOD = 10**9 + 7
N = int(input())
A = list(map(int, input().split()))

ans = 0
m = 1
for i in range(60):
    bits = sum((a & m for a in A))
    bits //= m
    ans += bits * (N - bits) * m
    ans %= MOD
    m <<= 1
print(ans % MOD)
Problem p02838 (Python)

Buggy submission (status: Time Limit Exceeded):
n, *a = map(int, open(0).read().split())
# すべての組み合わせ
# あるビットについて、そのビットが立つ回数
mod = 10**9 + 7
ans = 0
for i in range(60):
    cnt = 0
    for j in range(n):
        cnt += a[j] >> i & 1
    ans = (ans + (1 << i) * cnt * (n - cnt) % mod) % mod
print(ans)


Fixed submission:
n, *a = map(int, open(0).read().split())
mod = 10**9 + 7
ans = 0
for i in range(60):
    cnt = 0
    for j in range(n):
        cnt += a[j] >> i & 1
    ans = (ans + (1 << i) * cnt * (n - cnt) % mod) % mod
print(ans)
Problem p02838 (Python)

Buggy submission (status: Time Limit Exceeded):
import numpy as np

N = int(input())
A = np.array(list(map(int, input().split())))
ans = 0
MOD = 10**9 + 7

for digit in range(60):
    one = sum((A >> digit) & 1)
    ans += (one * (N - one) * pow(2, digit, MOD)) % MOD
    ans %= MOD

print(ans)


Fixed submission:
import numpy as np

N = int(input())
A = np.array(list(map(int, input().split())))
ans = 0
MOD = 10**9 + 7

for digit in range(60):
    one = np.count_nonzero((A >> digit) & 1)
    ans += (one * (N - one) * pow(2, digit, MOD)) % MOD
    ans %= MOD

print(ans)