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

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

signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  int n;
  cin >> n;
  string s;
  cin >> s;

  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt += (s[i] == '1');
  }

  int cntnp[2] = {cnt - 1, cnt + 1};
  int cr[2] = {0, 0};
  int rem[n][2];
  int tillnow[2] = {0, 0};
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j < 2; j++) {
      if (cntnp[j] == 0)
        continue;
      cr[j] = (1 + tillnow[j]) % cntnp[j];
      tillnow[j] += cr[j];
      rem[i][j] = cr[j];
    }
  }

  int sum[2] = {0, 0};
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      for (int j = 0; j < 2; j++) {
        sum[j] += rem[i][j];
      }
    }
  }
  for (int i = 0; i < n; i++) {
    //        int actual = 0;
    //        for (int j=n-1; j>=0; j--){
    //            if (i==j){
    //                if (s[i]=='0'){
    //                    actual += 1<<(n-1-j);
    //                }
    //            }
    //            else if (s[j]=='1') actual += 1<<(n-1-j);
    //        }
    //        cout<<"actual -- "<<actual<<endl;

    int curr_cnt;
    int curr_sum;
    if (s[i] == '0') {
      curr_cnt = cntnp[1];
      curr_sum = sum[1] + rem[i][1];
    } else {
      curr_cnt = cntnp[0];
      curr_sum = sum[0] - rem[i][0];
    }

    //        cout<<"actual mod"<<actual%curr_cnt<<endl;
    int ans = 1;
    if (curr_cnt == 0) {
      cout << 0 << endl;
      continue;
    }
    int now = curr_sum % curr_cnt;
    while (now > 0) {
      //            cout<<"Now -- "<<now<<endl;
      int now_cnt = 0;
      int tmp = now;
      while (tmp > 0) {
        if (tmp & 1)
          now_cnt++;
        tmp >>= 1;
      }
      now = now % now_cnt;
      ans++;
    }
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  int n;
  cin >> n;
  string s;
  cin >> s;

  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt += (s[i] == '1');
  }

  int cntnp[2] = {cnt - 1, cnt + 1};
  int cr[2] = {0, 0};
  int rem[n][2];
  int tillnow[2] = {0, 0};
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j < 2; j++) {
      cr[j] = (1 + tillnow[j]) % cntnp[j];
      tillnow[j] += cr[j];
      rem[i][j] = cr[j];
    }
  }

  int sum[2] = {0, 0};
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      for (int j = 0; j < 2; j++) {
        sum[j] += rem[i][j];
      }
    }
  }
  for (int i = 0; i < n; i++) {
    //        int actual = 0;
    //        for (int j=n-1; j>=0; j--){
    //            if (i==j){
    //                if (s[i]=='0'){
    //                    actual += 1<<(n-1-j);
    //                }
    //            }
    //            else if (s[j]=='1') actual += 1<<(n-1-j);
    //        }
    //        cout<<"actual -- "<<actual<<endl;

    int curr_cnt;
    int curr_sum;
    if (s[i] == '0') {
      curr_cnt = cntnp[1];
      curr_sum = sum[1] + rem[i][1];
    } else {
      curr_cnt = cntnp[0];
      curr_sum = sum[0] - rem[i][0];
    }

    //        cout<<"actual mod"<<actual%curr_cnt<<endl;
    int ans = 1;
    if (curr_cnt == 0) {
      cout << 1 << endl;
      continue;
    }
    int now = curr_sum % curr_cnt;
    while (now > 0) {
      //            cout<<"Now -- "<<now<<endl;
      int now_cnt = 0;
      int tmp = now;
      while (tmp > 0) {
        if (tmp & 1)
          now_cnt++;
        tmp >>= 1;
      }
      if (now_cnt == 0)
        break;
      now = now % now_cnt;
      ans++;
    }
    cout << ans << endl;
  }
  return 0;
}