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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll big = 1e18;
const double PI = 2 * asin(1);

int DP[200005];

int main() {
  int cnt;
  for (int i = 1; i < 200005; ++i) {
    cnt = 0;
    for (int j = 0; j < 32; ++j) {
      if ((i & (1 << j)) != 0)
        cnt++;
    }
    DP[i] = DP[i % cnt] + 1;
  }
  int N;
  cin >> N;
  string S;
  cin >> S;
  cnt = 0;
  for (int i = 0; i < S.size(); ++i) {
    if (S[i] == '1')
      cnt++;
  }
  if (cnt == 0) {
    for (int i = 0; i < N; ++i)
      cout << 1 << endl;
    return 0;
  }
  int amarimin = 0, amariadd = 0;
  int two = 1;
  if (cnt > 1) {
    for (int i = S.size() - 1; i >= 0; --i) {
      if (S[i] == '1') {
        amarimin += two % (cnt - 1);
        amarimin %= cnt - 1;
      }
      two *= 2;
      two %= cnt - 1;
    }
  }
  two = 1;
  for (int i = S.size() - 1; i >= 0; --i) {
    if (S[i] == '1') {
      amariadd += two % (cnt + 1);
      amariadd %= cnt + 1;
    }
    two *= 2;
    two %= cnt + 1;
  }
  vector<int> ans(N);
  bool zero[N];
  for (int i = 0; i < N; ++i)
    zero[i] = false;
  int twoadd = 1, twomin = 1;
  for (int i = S.size() - 1; i >= 0; --i) {
    if (S[i] == '1') {
      if (cnt == 1)
        zero[i] = true;
      else {
        ans[i] = (amarimin - twomin + (cnt - 1)) % (cnt - 1);
      }
    } else {
      ans[i] = (amariadd + twoadd) % (cnt + 1);
    }
    twoadd *= 2;
    twoadd %= cnt + 1;
    if (cnt > 1) {
      twomin *= 2;
      twomin %= cnt - 1;
    }
  }
  for (int i = 0; i < N; ++i) {
    if (zero[i])
      cout << 0 << endl;
    else
      cout << 1 + DP[ans[i]] << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll big = 1e18;
const double PI = 2 * asin(1);

int DP[200005];

int main() {
  int cnt;
  for (int i = 1; i < 200005; ++i) {
    cnt = 0;
    for (int j = 0; j < 32; ++j) {
      if ((i & (1 << j)) != 0)
        cnt++;
    }
    DP[i] = DP[i % cnt] + 1;
  }
  int N;
  cin >> N;
  string S;
  cin >> S;
  cnt = 0;
  for (int i = 0; i < S.size(); ++i) {
    if (S[i] == '1')
      cnt++;
  }
  if (cnt == 0) {
    for (int i = 0; i < N; ++i)
      cout << 1 << endl;
    return 0;
  }
  int amarimin = 0, amariadd = 0;
  int two = 1;
  if (cnt > 1) {
    for (int i = S.size() - 1; i >= 0; --i) {
      if (S[i] == '1')
        amarimin += two % (cnt - 1);
      two *= 2;
      two %= cnt - 1;
    }
  }
  two = 1;
  for (int i = S.size() - 1; i >= 0; --i) {
    if (S[i] == '1')
      amariadd += two % (cnt + 1);
    two *= 2;
    two %= cnt + 1;
  }
  vector<int> ans(N);
  bool zero[N];
  for (int i = 0; i < N; ++i)
    zero[i] = false;
  int twoadd = 1, twomin = 1;
  for (int i = S.size() - 1; i >= 0; --i) {
    if (S[i] == '1') {
      if (cnt == 1)
        zero[i] = true;
      else {
        ans[i] = (amarimin - twomin + (cnt - 1)) % (cnt - 1);
      }
    } else {
      ans[i] = (amariadd + twoadd) % (cnt + 1);
    }
    twoadd *= 2;
    twoadd %= cnt + 1;
    if (cnt > 1) {
      twomin *= 2;
      twomin %= cnt - 1;
    }
  }
  for (int i = 0; i < N; ++i) {
    if (zero[i])
      cout << 0 << endl;
    else
      cout << 1 + DP[ans[i]] << endl;
  }
}