Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pll = pair<ll, ll>;
#define all(v) v.begin(), v.end()
const ll INF = 1e18;
const ll mod = 1e9 + 7;
const double pie = acos(-1);
vll dx4 = {-1, 0, 1, 0};
vll dy4 = {0, -1, 0, 1};
vll dx8 = {-1, 0, 1, 1, 1, 0, -1, -1};
vll dy8 = {-1, -1, -1, 0, 1, 1, 1, 0};

void fix_cout() { cout << fixed << setprecision(20); }
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
void chmax(ll &a, ll b) {
  if (a < b)
    a = b;
}
void chmin(ll &a, ll b) {
  if (a > b)
    a = b;
}

ll modpow(ll a, ll b, ll m) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    b >>= 1;
    a = a * a % m;
  }
  return res;
}

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll a = 0, b = 0;
  ll cnt = 0;
  for (int i = 0; i < n; i++)
    cnt += (s.at(i) == '1');
  for (int i = 0; i < n; i++) {
    if (s.at(i) == '1') {
      a += modpow(2, n - i - 1, cnt + 1);
      b += modpow(2, n - i - 1, cnt - 1);
    }
  }
  for (int i = 0; i < n; i++) {
    ll tmp, ans = 1, sub = 0;
    if (s.at(i) == '1') {
      tmp = (b - modpow(2, n - i - 1, cnt - 1) + cnt - 1) % (cnt - 1);
      while (tmp) {
        ans++;
        sub = 0;
        for (int j = 0; j < 64; j++)
          sub += (tmp >> j & 1);
        tmp = tmp % sub;
      }
    } else {
      tmp = (a + modpow(2, n - i - 1, cnt + 1)) % (cnt + 1);
      while (tmp) {
        ans++;
        sub = 0;
        for (int j = 0; j < 64; j++)
          sub += (tmp >> j & 1);
        tmp = tmp % sub;
      }
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pll = pair<ll, ll>;
#define all(v) v.begin(), v.end()
const ll INF = 1e18;
const ll mod = 1e9 + 7;
const double pie = acos(-1);
vll dx4 = {-1, 0, 1, 0};
vll dy4 = {0, -1, 0, 1};
vll dx8 = {-1, 0, 1, 1, 1, 0, -1, -1};
vll dy8 = {-1, -1, -1, 0, 1, 1, 1, 0};

void fix_cout() { cout << fixed << setprecision(20); }
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
void chmax(ll &a, ll b) {
  if (a < b)
    a = b;
}
void chmin(ll &a, ll b) {
  if (a > b)
    a = b;
}

ll modpow(ll a, ll b, ll m) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    b >>= 1;
    a = a * a % m;
  }
  return res;
}

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll a = 0, b = 0;
  ll cnt = 0;
  for (int i = 0; i < n; i++)
    cnt += (s.at(i) == '1');
  if (cnt == 0) {
    for (int i = 0; i < n; i++)
      cout << 1 << endl;
    return 0;
  }
  if (cnt == 1) {
    for (int i = 0; i < n; i++) {
      if (s.at(i) == '1')
        cout << 0 << endl;
      else if (i == n - 1)
        cout << 2 << endl;
      else
        cout << 1 << endl;
    }
    return 0;
  }
  for (int i = 0; i < n; i++) {
    if (s.at(i) == '1') {
      a += modpow(2, n - i - 1, cnt + 1);
      b += modpow(2, n - i - 1, cnt - 1);
    }
  }
  for (int i = 0; i < n; i++) {
    ll tmp, ans = 1, sub = 0;
    if (s.at(i) == '1') {
      tmp = (b - modpow(2, n - i - 1, cnt - 1) + cnt - 1) % (cnt - 1);
      while (tmp) {
        ans++;
        sub = 0;
        for (int j = 0; j < 64; j++)
          sub += (tmp >> j & 1);
        tmp = tmp % sub;
      }
    } else {
      tmp = (a + modpow(2, n - i - 1, cnt + 1)) % (cnt + 1);
      while (tmp) {
        ans++;
        sub = 0;
        for (int j = 0; j < 64; j++)
          sub += (tmp >> j & 1);
        tmp = tmp % sub;
      }
    }
    cout << ans << endl;
  }
}