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

#include <bits/stdc++.h>
#define int long long
typedef long long ll;
using namespace std;

const ll MAXN = 200000;
const ll INF = 1001001001;
const ll MOD = 1000000007;

ll N, ans = 0, tmp = 0, tmpp = 0, tmpn = 0;
vector<ll> A;
string S;

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

signed main() {
  cin >> N >> S;
  for (int i = 0; i < N; i++) {
    ll num = S[i] - '0';
    if (num == 1)
      tmp++;
    A.push_back(num);
  }
  if (tmp == 0) {
    for (int i = 0; i < N; i++) {
      cout << 1 << endl;
    }
    return 0;
  }
  if (tmp == 1) {
    for (int i = 0; i < N; i++) {
      if (A[N - 1 - i] == 1) {
        tmpp += modpow(2, i, 2);
        tmpp %= 2;
        if (tmpp < 0)
          tmpp += 2;
      }
    }
    for (int i = 0; i < N; i++) {
      if (A[i] == 1) {
        cout << 0 << endl;
        continue;
      }
      ll id = tmpp;
      id += modpow(2, N - 1 - i, 2);
      id %= 2;
      if (id < 0)
        id += 2;
      if (id == 0)
        cout << 1 << endl;
      else
        cout << 2 << endl;
    }
    return 0;
  }

  for (int i = 0; i < N; i++) {
    if (A[N - 1 - i] == 1) {
      tmpp += modpow(2, i, tmp + 1);
      tmpp %= (tmp + 1);
      if (tmpp < 0)
        tmpp += tmp + 1;
      tmpn += modpow(2, i, tmp - 1);
      tmpn %= (tmp - 1);
      if (tmpn < 0)
        tmpn += tmp - 1;
    }
  }
  for (int i = 0; i < N; i++) {
    // cout << "OK" << endl;
    ans = 1;
    if (A[i] == 0) {
      ll tmp2 = tmpp;
      ll tmp3 = tmp + 1;
      tmp2 += modpow(2, N - 1 - i, tmp3);
      tmp2 %= tmp3;
      if (tmp2 < 0)
        tmp2 += tmp3;
      while (tmp2 != 0) {
        // cout << i << " " << tmp2 << endl;
        tmp3 = __builtin_popcount(tmp2);
        tmp2 %= tmp3;
        if (tmp2 < 0)
          tmp2 += tmp3;
        ans++;
      }
    } else {
      ll tmp2 = tmpn;
      ll tmp3 = tmp - 1;
      tmp2 -= modpow(2, N - 1 - i, tmp3);
      tmp2 %= tmp3;
      if (tmp2 < 0)
        tmp2 += tmp3;
      while (tmp2 != 0) {
        // cout << i << " " << tmp2 << endl;
        tmp3 = __builtin_popcount(tmp2);
        tmp2 %= tmp3;
        if (tmp2 < 0)
          tmp2 += tmp3;
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long
typedef long long ll;
using namespace std;

const ll MAXN = 200000;
const ll INF = 1001001001;
const ll MOD = 1000000007;

ll N, ans = 0, tmp = 0, tmpp = 0, tmpn = 0;
vector<ll> A;
string S;

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

signed main() {
  cin >> N >> S;
  for (int i = 0; i < N; i++) {
    ll num = S[i] - '0';
    if (num == 1)
      tmp++;
    A.push_back(num);
  }
  for (int i = 0; i < N; i++) {
    if (A[N - 1 - i] == 1) {
      tmpp += modpow(2, i, tmp + 1);
      tmpp %= (tmp + 1);
      if (tmpp < 0)
        tmpp += tmp + 1;
      tmpn += modpow(2, i, tmp - 1);
      tmpn %= (tmp - 1);
      if (tmpn < 0)
        tmpn += tmp - 1;
    }
  }
  for (int i = 0; i < N; i++) {
    // cout << "OK" << endl;
    ans = 1;
    if (A[i] == 0) {
      ll tmp2 = tmpp;
      ll tmp3 = tmp + 1;
      tmp2 += modpow(2, N - 1 - i, tmp3);
      tmp2 %= tmp3;
      if (tmp2 < 0)
        tmp2 += tmp3;
      while (tmp2 != 0) {
        // cout << i << " " << tmp2 << endl;
        tmp3 = __builtin_popcount(tmp2);
        tmp2 %= tmp3;
        if (tmp2 < 0)
          tmp2 += tmp3;
        ans++;
      }
    } else {
      ll tmp2 = tmpn;
      ll tmp3 = tmp - 1;
      tmp2 -= modpow(2, N - 1 - i, tmp3);
      tmp2 %= tmp3;
      if (tmp2 < 0)
        tmp2 += tmp3;
      while (tmp2 != 0) {
        // cout << i << " " << tmp2 << endl;
        tmp3 = __builtin_popcount(tmp2);
        tmp2 %= tmp3;
        if (tmp2 < 0)
          tmp2 += tmp3;
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}