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

#include <algorithm>
#include <bit>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
using namespace std;
const double PI = 3.14159265358979323846;
const long long int MOD = 1000000000 + 7;

struct UnionFind {
  vector<int> parent;

  UnionFind(int N) : parent(N) {
    for (int i = 0; i < N; i++) {
      parent[i] = -1;
    }
  }

  int root(int i) {
    if (parent[i] < 0) {
      return i;
    }
    return (parent[i] = root(parent[i]));
  }

  bool unite(int from, int to) {
    int rx = root(from);
    int ry = root(to);
    if (rx != ry) {
      parent[ry] += parent[rx];
      parent[rx] = ry;

      return true;
    } else {
      return false;
    }
  }

  bool same(int x, int y) { return root(x) == root(y); }

  int treeSize(int x) { return -parent[root(x)]; }
};

long long int modpow(long long int base, long long int pow, long long int mod) {
  if (pow == 1) {
    return base;
  } else if (pow == 0) {
    return 1;
  }

  if (pow % 2 == 0) {
    auto temp = modpow(base, pow / 2, mod);
    return (temp * temp) % mod;
  } else {
    return (base * modpow(base, pow - 1, mod)) % mod;
  }
}

long long int moddiv(long long int X, long long int Y, long long int mod) {
  auto fermatDiv = modpow(Y, mod - 2, mod);

  return (X * fermatDiv) % mod;
}

long long modCombination(long long left, long long right, long long int mod) {
  long long answer = 1;
  if (left > right) {
    for (long long i = 0; i < right; i++) {
      answer = (answer * (left - i)) % mod;
      answer = moddiv(answer, (i + 1), mod);
    }
  }
  return answer;
}

bool IsPrime(long long N) {
  if (N == 1) {
    return false;
  }
  for (long long i = 2; i * i <= N; i++) {
    if (N % i == 0) {
      return false;
    }
  }

  return true;
}

vector<pair<long long, long long>> prime_factorize(long long N) {
  vector<pair<long long, long long>> res;
  for (long long a = 2; a * a <= N; ++a) {
    if (N % a != 0)
      continue;
    long long ex = 0; // 指数

    // 割れる限り割り続ける
    while (N % a == 0) {
      ++ex;
      N /= a;
    }

    // その結果を push
    res.push_back({a, ex});
  }

  // 最後に残った数について
  if (N != 1)
    res.push_back({N, 1});
  return res;
}

map<long long, set<long long>> cache;
set<long long> enum_divisors(long long N) {
  set<long long> res;
  for (long long i = 1; i * i <= N; ++i) {
    if (N % i == 0) {
      res.insert(i);

      if (cache.count(N / i) > 0) {
        auto copy = cache[N / i];
        auto copy2 = cache[N / i];
        for (auto v : res) {
          if (v != 1) {
            // cout << "v*" << v << endl;
            for (auto itr = copy2.begin(); itr != copy2.end(); itr++) {
              copy.insert(*itr * v);
            }
            break;
          }
        }
        cache[N] = copy;
        return copy;
      }

      // 重複しないならば i の相方である N/i も push
      if (N / i != i)
        res.insert(N / i);
    }
  }

  cache[N] = res;
  return res;
}

long long euclid(long long a, long long b) {
  if (a > b) {
    long long temp = b;
    b = a;
    a = temp;
  }
  if (b % a == 0) {
    return a;
  } else {
    return euclid(a, b - a);
  }
}

long long gcd(long long a, long long b) {
  if (b > a) {
    long long temp = b;
    b = a;
    a = temp;
  }
  // cout << "a:" << a << "b:" << b << endl;
  long long c = a % b;
  if (c == 0) {
    return b;
  } else {
    return gcd(b, c);
  }
}

struct Camel {
public:
  int K;

public:
  int L, R;

public:
  int minus;

public:
  bool isForward;
  bool operator<(const Camel &right) const { return minus > right.minus; }
};

int main() {
  int N;
  cin >> N;
  string x;
  cin >> x;

  if (N == 1) {
    if (x == "1") {
      cout << "0" << endl;
    } else {
      cout << "1" << endl;
    }

    return 0;
  }

  vector<vector<int>> pow2mod(2, vector<int>(N));
  int originalPopCount = 0;
  for (auto d : x) {
    if (d == '1') {
      originalPopCount++;
    }
  }

  for (int i = 0; i < N; i++) {
    if (i != 0) {
      pow2mod[0][i] = (pow2mod[0][i - 1] * 2) % (originalPopCount + 1);
    } else {
      pow2mod[0][0] = 1 % (originalPopCount + 1);
    }
  }

  if (originalPopCount > 1) {
    for (int i = 0; i < N; i++) {
      if (i != 0) {
        pow2mod[1][i] = (pow2mod[1][i - 1] * 2) % (originalPopCount - 1);
      } else {
        pow2mod[1][0] = 1 % (originalPopCount - 1);
      }
    }
  } else {
    for (int i = 0; i < N; i++) {
      pow2mod[1][i] = 0;
    }
  }

  vector<int> originalMod(2, 0);

  for (int i = 0; i < N; i++) {
    if (x[N - (i + 1)] == '1') {
      originalMod[0] = originalMod[0] + pow2mod[0][i];
      originalMod[0] %= (originalPopCount + 1);
    }
  }

  for (int i = 0; i < N; i++) {
    if (x[N - (i + 1)] == '1') {
      if (originalPopCount > 1) {
        originalMod[1] = originalMod[1] + pow2mod[1][i];
        originalMod[1] %= (originalPopCount - 1);
      } else {
        originalMod[1] = 0;
      }
    }
  }

  for (int i = 0; i < N; i++) {
    int f = 0;
    int count = 1;
    if (x[i] == '1') {
      if (originalPopCount <= 1) {
        cout << "0" << endl;
        continue;
      }
      f = originalMod[1];
      f -= pow2mod[1][N - (i + 1)];
      f = (f + (originalPopCount - 1)) % (originalPopCount - 1);
    } else {
      f = originalMod[0];
      f += pow2mod[0][N - (i + 1)];
      f %= (originalPopCount + 1);
    }

    while (f != 0) {
      bitset<32> bs(f);
      int pcount = bs.count();

      f = f % pcount;
      count++;
    }

    cout << count << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bit>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
using namespace std;
const double PI = 3.14159265358979323846;
const long long int MOD = 1000000000 + 7;

struct UnionFind {
  vector<int> parent;

  UnionFind(int N) : parent(N) {
    for (int i = 0; i < N; i++) {
      parent[i] = -1;
    }
  }

  int root(int i) {
    if (parent[i] < 0) {
      return i;
    }
    return (parent[i] = root(parent[i]));
  }

  bool unite(int from, int to) {
    int rx = root(from);
    int ry = root(to);
    if (rx != ry) {
      parent[ry] += parent[rx];
      parent[rx] = ry;

      return true;
    } else {
      return false;
    }
  }

  bool same(int x, int y) { return root(x) == root(y); }

  int treeSize(int x) { return -parent[root(x)]; }
};

long long int modpow(long long int base, long long int pow, long long int mod) {
  if (pow == 1) {
    return base;
  } else if (pow == 0) {
    return 1;
  }

  if (pow % 2 == 0) {
    auto temp = modpow(base, pow / 2, mod);
    return (temp * temp) % mod;
  } else {
    return (base * modpow(base, pow - 1, mod)) % mod;
  }
}

long long int moddiv(long long int X, long long int Y, long long int mod) {
  auto fermatDiv = modpow(Y, mod - 2, mod);

  return (X * fermatDiv) % mod;
}

long long modCombination(long long left, long long right, long long int mod) {
  long long answer = 1;
  if (left > right) {
    for (long long i = 0; i < right; i++) {
      answer = (answer * (left - i)) % mod;
      answer = moddiv(answer, (i + 1), mod);
    }
  }
  return answer;
}

bool IsPrime(long long N) {
  if (N == 1) {
    return false;
  }
  for (long long i = 2; i * i <= N; i++) {
    if (N % i == 0) {
      return false;
    }
  }

  return true;
}

vector<pair<long long, long long>> prime_factorize(long long N) {
  vector<pair<long long, long long>> res;
  for (long long a = 2; a * a <= N; ++a) {
    if (N % a != 0)
      continue;
    long long ex = 0; // 指数

    // 割れる限り割り続ける
    while (N % a == 0) {
      ++ex;
      N /= a;
    }

    // その結果を push
    res.push_back({a, ex});
  }

  // 最後に残った数について
  if (N != 1)
    res.push_back({N, 1});
  return res;
}

map<long long, set<long long>> cache;
set<long long> enum_divisors(long long N) {
  set<long long> res;
  for (long long i = 1; i * i <= N; ++i) {
    if (N % i == 0) {
      res.insert(i);

      if (cache.count(N / i) > 0) {
        auto copy = cache[N / i];
        auto copy2 = cache[N / i];
        for (auto v : res) {
          if (v != 1) {
            // cout << "v*" << v << endl;
            for (auto itr = copy2.begin(); itr != copy2.end(); itr++) {
              copy.insert(*itr * v);
            }
            break;
          }
        }
        cache[N] = copy;
        return copy;
      }

      // 重複しないならば i の相方である N/i も push
      if (N / i != i)
        res.insert(N / i);
    }
  }

  cache[N] = res;
  return res;
}

long long euclid(long long a, long long b) {
  if (a > b) {
    long long temp = b;
    b = a;
    a = temp;
  }
  if (b % a == 0) {
    return a;
  } else {
    return euclid(a, b - a);
  }
}

long long gcd(long long a, long long b) {
  if (b > a) {
    long long temp = b;
    b = a;
    a = temp;
  }
  // cout << "a:" << a << "b:" << b << endl;
  long long c = a % b;
  if (c == 0) {
    return b;
  } else {
    return gcd(b, c);
  }
}

struct Camel {
public:
  int K;

public:
  int L, R;

public:
  int minus;

public:
  bool isForward;
  bool operator<(const Camel &right) const { return minus > right.minus; }
};

int main() {
  int N;
  cin >> N;
  string x;
  cin >> x;

  if (N == 1) {
    if (x == "1") {
      cout << "0" << endl;
    } else {
      cout << "1" << endl;
    }

    return 0;
  }

  vector<vector<int>> pow2mod(2, vector<int>(N));
  int originalPopCount = 0;
  for (auto d : x) {
    if (d == '1') {
      originalPopCount++;
    }
  }

  for (int i = 0; i < N; i++) {
    if (i != 0) {
      pow2mod[0][i] = (pow2mod[0][i - 1] * 2) % (originalPopCount + 1);
    } else {
      pow2mod[0][0] = 1 % (originalPopCount + 1);
    }
  }

  for (int i = 0; i < N; i++) {
    if (i != 0) {
      pow2mod[1][i] = (pow2mod[1][i - 1] * 2) % (originalPopCount - 1);
    } else {
      pow2mod[1][0] = 1 % (originalPopCount - 1);
    }
  }

  vector<int> originalMod(2, 0);

  for (int i = 0; i < N; i++) {
    if (x[N - (i + 1)] == '1') {
      originalMod[0] = originalMod[0] + pow2mod[0][i];
      originalMod[0] %= (originalPopCount + 1);
    }
  }

  for (int i = 0; i < N; i++) {
    if (x[N - (i + 1)] == '1') {
      originalMod[1] = originalMod[1] + pow2mod[1][i];
      originalMod[1] %= (originalPopCount - 1);
    }
  }

  for (int i = 0; i < N; i++) {
    int f = 0;
    int count = 1;
    if (x[i] == '1') {
      f = originalMod[1];
      f -= pow2mod[1][N - (i + 1)];
      f = (f + (originalPopCount - 1)) % (originalPopCount - 1);
    } else {
      f = originalMod[0];
      f += pow2mod[0][N - (i + 1)];
      f %= (originalPopCount + 1);
    }

    while (f != 0) {
      bitset<32> bs(f);
      int pcount = bs.count();

      f = f % pcount;
      count++;
    }

    cout << count << endl;
  }
  return 0;
}