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

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (auto i = a; i < b; i++)
#define rep2(i, a) for (auto i : a)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x, std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end());
#define vvec vector<vector<ll>>
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
#define INF 1 << 30
const int mod = 1000000007;
ll power(ll x, ll p) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x *= x;
      p /= 2;
    } else {
      a *= x;
      p--;
    }
  }
  return a;
}
ll mpower(ll x, ll p, ll mo) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x = x * x % mo;
      p /= 2;
    } else {
      a = a * x % mo;
      p--;
    }
  }
  return a % mo;
}
ll co(ll n, ll k) {
  ll a = 1;
  rep(i, 1, k) {
    a *= n - i + 1;
    a /= i;
  }
  return a;
}
ll mc(ll n, ll m) {
  ll k = 1, l = 1;
  rep(i, n - m + 1, n + 1) k = k * i % mod;
  rep(i, 1, m + 1) l = l * i % mod;
  l = mpower(l, mod - 2, 0);
  return k * l % mod;
}
ll popcnt(ll v) {
  ll ans = 0;
  while (v) {
    if (v % 2)
      ans++;
    v /= 2;
  }
  return ans;
}
int main() {
  ll n, c, k[3] = {0};
  string s;
  cin >> n >> s;
  c = 0;
  int pp = -1, f = 0;
  rep(i2, 0, n) {
    if (s[i2] == '1') {
      if (f == 0)
        f = 1, pp = i2;
      else
        pp = -1;
      c++;
    }
  }
  if (c == 0) {
    rep(i, 0, n) puts("1");
    return 0;
  }
  rep(i2, 0, n) {
    if (s[i2] == '1') {
      k[0] += mpower(2, n - i2 - 1, c);
      k[1] += mpower(2, n - i2 - 1, c + 1);
      if (c >= 2)
        k[2] += mpower(2, n - i2 - 1, c - 1);
    }
    k[0] %= c;
    k[1] %= c + 1;
    if (c >= 2)
      k[2] %= c - 1;
  }
  rep(i, 0, n) {
    ll kk;
    if (c == 1 && s[i] == '1') {
      puts("0");
      continue;
    }
    if (s[i] == '1')
      kk = k[2] - mpower(2, n - i - 1, c - 1) + c - 1, kk %= c - 1;
    else
      kk = k[1] + mpower(2, n - i - 1, c + 1), kk %= c + 1;
    int ans = 1;
    while (kk) {
      kk %= popcnt(kk);
      ans++;
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (auto i = a; i < b; i++)
#define rep2(i, a) for (auto i : a)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x, std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end());
#define vvec vector<vector<ll>>
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
#define INF 1 << 30
const int mod = 1000000007;
ll power(ll x, ll p) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x *= x;
      p /= 2;
    } else {
      a *= x;
      p--;
    }
  }
  return a;
}
ll mpower(ll x, ll p, ll mo) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x = x * x % mo;
      p /= 2;
    } else {
      a = a * x % mo;
      p--;
    }
  }
  return a % mo;
}
ll co(ll n, ll k) {
  ll a = 1;
  rep(i, 1, k) {
    a *= n - i + 1;
    a /= i;
  }
  return a;
}
ll mc(ll n, ll m) {
  ll k = 1, l = 1;
  rep(i, n - m + 1, n + 1) k = k * i % mod;
  rep(i, 1, m + 1) l = l * i % mod;
  l = mpower(l, mod - 2, 0);
  return k * l % mod;
}
ll popcnt(ll v) {
  ll ans = 0;
  while (v) {
    if (v % 2)
      ans++;
    v /= 2;
  }
  return ans;
}
int main() {
  ll n, c, k[3] = {0};
  string s;
  cin >> n >> s;
  c = 0;
  int pp = -1, f = 0;
  rep(i2, 0, n) {
    if (s[i2] == '1') {
      if (f == 0)
        f = 1, pp = i2;
      else
        pp = -1;
      c++;
    }
  }
  if (c == 0) {
    rep(i, 0, n) puts("1");
    return 0;
  }
  rep(i2, 0, n) {
    if (s[i2] == '1') {
      k[0] += mpower(2, n - i2 - 1, c);
      k[1] += mpower(2, n - i2 - 1, c + 1);
      if (c)
        k[2] += mpower(2, n - i2 - 1, c - 1);
    }
    k[0] %= c;
    k[1] %= c + 1;
    if (c)
      k[2] %= c - 1;
  }
  rep(i, 0, n) {
    ll kk;
    if (c == 1 && s[i] == '1') {
      puts("0");
      continue;
    }
    if (s[i] == '1')
      kk = k[2] - mpower(2, n - i - 1, c - 1), kk %= c - 1;
    else
      kk = k[1] + mpower(2, n - i - 1, c + 1), kk %= c + 1;
    int ans = 1;
    while (kk) {
      kk %= popcnt(kk);
      ans++;
    }
    printf("%d\\n", ans);
  }
  return 0;
}