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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> P;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
const int MAXN = 100000;
const int MAXE = 100000;
const int MAXV = 10000;
const int inf = 1e9;
const ll INF = 2e18;
const ll MOD = 1e9 + 7;

struct UnionFind {
  vector<int> par, rank;
  UnionFind(int n) : par(n), rank(n) {
    for (int i = 0; i < n; ++i)
      par[i] = i;
  }
  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }
  void unite(int x, int y) {
    int rx = root(x), ry = root(y);
    if (rx == ry)
      return;
    if (rank[ry] > rank[rx])
      par[rx] = ry;
    else
      par[ry] = rx;
    if (rank[rx] == rank[ry])
      rank[rx]++;
  }
  bool same(int x, int y) { return root(x) == root(y); }
};

vector<ll> fac, inv, finv;

void initcom() {
  fac = inv = finv = vector<ll>(1e6);
  fac[0] = fac[1] = 1;
  inv[1] = 1;
  finv[0] = finv[1] = 1;
  for (ll i = 2; i < 1e6; ++i) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

ll com(int n, int k) {
  if (n < 0 || k < 0)
    return 0;
  if (n < k)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
ll mopow(ll a, ll x, ll mod) {
  ll res = 1;
  while (x > 0) {
    if (x & 1)
      res = res * a % mod;
    a = a * a % mod;
    x >>= 1;
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  vector<int> pop(2e6, 0);
  for (int i = 1; i < 2e6; ++i) {
    pop[i] = pop[i % __builtin_popcount(i)] + 1;
  }
  reverse(x.begin(), x.end());
  int cnt = 0;
  for (int i = 0; i < n; ++i)
    if (x[i] == '1')
      cnt++;

  int tot1 = 0, p1 = 1;
  int tot2 = 0, p2 = 1;
  for (int i = 0; i < n; ++i) {
    if (x[i] == '1') {
      tot1 = (tot1 + p1) % (cnt + 1);
      if (2 <= cnt)
        tot2 = (tot2 + p2) % (cnt - 1);
    }
    p1 = (p1 * 2) % (cnt + 1);
    if (2 <= cnt)
      p2 = (p2 * 2) % (cnt - 1);
  }

  vector<int> ans;
  p1 = p2 = 1;
  for (int i = 0; i < n; ++i) {
    if (x[i] == '1') {
      if (cnt == 1)
        ans.push_back(0);
      else {
        int y = (tot2 - p2 + (cnt - 1)) % (cnt - 1);
        ans.push_back(pop[y] + 1);
      }
    } else {
      int y = (tot1 + p1) % (cnt + 1);
      ans.push_back(pop[y] + 1);
    }

    p1 = (p1 * 2) % (cnt + 1);
    if (2 <= cnt)
      p2 = (p2 * 2) % (cnt - 1);
  }
  reverse(ans.begin(), ans.end());
  for (int i = 0; i < n; ++i)
    cout << ans[i] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> P;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
const int MAXN = 100000;
const int MAXE = 100000;
const int MAXV = 10000;
const int inf = 1e9;
const ll INF = 2e18;
const ll MOD = 1e9 + 7;

struct UnionFind {
  vector<int> par, rank;
  UnionFind(int n) : par(n), rank(n) {
    for (int i = 0; i < n; ++i)
      par[i] = i;
  }
  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }
  void unite(int x, int y) {
    int rx = root(x), ry = root(y);
    if (rx == ry)
      return;
    if (rank[ry] > rank[rx])
      par[rx] = ry;
    else
      par[ry] = rx;
    if (rank[rx] == rank[ry])
      rank[rx]++;
  }
  bool same(int x, int y) { return root(x) == root(y); }
};

vector<ll> fac, inv, finv;

void initcom() {
  fac = inv = finv = vector<ll>(1e6);
  fac[0] = fac[1] = 1;
  inv[1] = 1;
  finv[0] = finv[1] = 1;
  for (ll i = 2; i < 1e6; ++i) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

ll com(int n, int k) {
  if (n < 0 || k < 0)
    return 0;
  if (n < k)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
ll mopow(ll a, ll x, ll mod) {
  ll res = 1;
  while (x > 0) {
    if (x & 1)
      res = res * a % mod;
    a = a * a % mod;
    x >>= 1;
  }
  return res;
}
ll popcount(ll x) {
  ll res = 0;
  while (x > 0) {
    if (x & 1)
      res++;
    x >>= 1;
  }
  return res;
}
ll f(ll x) {
  ll res = 0;
  while (x > 0) {
    x = x % popcount(x);
    res++;
  }
  return res;
}
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < n; ++i) {
    s[i] = '0' + (int)('1' - s[i]);
    cout << f(stoll(s, 0, 2)) << endl;
    s[i] = '0' + (int)('1' - s[i]);
  }
  return 0;
}