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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define sz(a) (int)a.size()
using namespace std;

// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt = 0;
  rep(i, n) {
    if (s[i] == '1')
      cnt++;
  }
  map<int, int> mp;
  mp[0] = 0;
  rep(i, cnt + 10) {
    int ai = i + 1;
    int cnti = 0;
    rep(iiii, 20) {
      if (ai & (1 << iiii))
        cnti++;
    }
    mp[ai] = mp[ai % cnti] + 1;
  }
  int cnt1 = cnt + 1;
  int cnt2 = cnt - 1;
  int amari1 = 0;
  int amari2 = 0;
  int amari = 1;
  rep(i, n) {
    int ai = amari * (s[n - 1 - i] - '0');
    amari1 = (amari1 + ai) % cnt1;
    amari = amari * 2 % cnt1;
  }
  amari = 1;
  if (cnt2 != 0) {
    rep(i, n) {
      int ai = amari * (s[n - 1 - i] - '0');
      amari2 = (amari2 + ai) % cnt2;
      amari = amari * 2 % cnt2;
    }
  }
  rep(i, n) {
    if (s[i] == '0') {
      int amari1i = amari1;
      amari1i += modpow(2, (n - 1 - i), cnt1);
      amari1i %= cnt1;
      cout << mp[amari1i] + 1 << endl;
    } else {
      if (cnt2 == 0) {
        cout << 0 << endl;
        continue;
      }
      int amari2i = amari2;
      amari2i -= modpow(2, (n - 1 - i), cnt2);
      amari2i = (amari2i + cnt2) % cnt2;
      cout << mp[amari2i] + 1 << endl;
    }
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define sz(a) (int)a.size()
using namespace std;

// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt = 0;
  rep(i, n) {
    if (s[i] == '1')
      cnt++;
  }
  map<int, int> mp;
  mp[0] = 0;
  rep(i, cnt + 10) {
    int ai = i + 1;
    int cnti = 0;
    rep(iiii, 20) {
      if (ai & (1 << iiii))
        cnti++;
    }
    mp[ai] = mp[ai % cnti] + 1;
  }
  int cnt1 = cnt + 1;
  int cnt2 = cnt - 1;
  int amari1 = 0;
  int amari2 = 0;
  int amari = 1;
  rep(i, n) {
    int ai = amari * (s[n - 1 - i] - '0');
    amari1 = (amari1 + ai) % cnt1;
    amari = amari * 2 % cnt1;
  }
  amari = 1;
  rep(i, n) {
    int ai = amari * (s[n - 1 - i] - '0');
    amari2 = (amari2 + ai) % cnt2;
    amari = amari * 2 % cnt2;
  }
  rep(i, n) {
    if (s[i] == '0') {
      int amari1i = amari1;
      amari1i += modpow(2, (n - 1 - i), cnt1);
      amari1i %= cnt1;
      cout << mp[amari1i] + 1 << endl;
    } else {
      if (cnt2 == 0) {
        cout << 0 << endl;
        continue;
      }
      int amari2i = amari2;
      amari2i -= modpow(2, (n - 1 - i), cnt2);
      amari2i = (amari2i + cnt2) % cnt2;
      cout << mp[amari2i] + 1 << endl;
    }
  }
}