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

#include "bits/stdc++.h"
using namespace std;
const long long INF = (long long)(1e18) + 1;
int main() {
  int N;
  string X;
  cin >> N >> X;
  vector<int> tmp0(N);
  tmp0[0] = 0;
  for (int n = 1; n < N; ++n) {
    if (1 == n % 2) {
      tmp0[n] = tmp0[n / 2] + 1;
    } else {
      tmp0[n] = tmp0[n / 2];
    }
  }
  vector<int> tmp1(N);
  tmp1[0] = 0;
  for (int n = 1; n < N; ++n) {
    if (0 == n % tmp0[n]) {
      tmp1[n] = 1;
    } else {
      tmp1[n] = tmp1[n % tmp0[n]] + 1;
    }
  }
  int d = 0;
  for (int n = 0; n < N; ++n) {
    if ('1' == X[n]) {
      d++;
    }
  }

  vector<int> D1(N);
  int sumD1 = 0;
  {
    int num = 1;
    for (int n = 0; n < N; ++n) {
      D1[N - 1 - n] = num;
      if ('1' == X[N - 1 - n]) {
        sumD1 += num;
        sumD1 %= (d + 1);
      }
      num *= 2;
      num %= (d + 1);
    }
  }
  vector<int> D2(N);
  int sumD2 = 0;
  {
    int num = 1;
    if (1 != d) {
      for (int n = 0; n < N; ++n) {
        D2[N - 1 - n] = num;
        if ('1' == X[N - 1 - n]) {
          sumD2 += num;
          sumD2 %= (d - 1);
        }
        num *= 2;
        num %= (d - 1);
      }
    }
  }
  // cout << d<< endl;
  // cout << sumD1 << endl;
  // cout << sumD2 << endl;

  for (int n = 0; n < N; ++n) {
    int dcopy = d;
    int sumDcopy = 0;
    string Xcopy = X;
    if ('0' == Xcopy[n]) {
      Xcopy[n] = '1';
      dcopy++;
      sumDcopy = sumD1 + D1[n];
      sumDcopy %= (dcopy);
    } else {
      Xcopy[n] = '0';
      dcopy--;
      if (0 == dcopy) {
        cout << 0 << endl;
        continue;
      }
      sumDcopy = sumD2 + (dcopy)-D2[n];
      sumDcopy %= (dcopy);
    }
    // cout << sumDcopy<<"_";
    if (0 == sumDcopy) {
      cout << 1 << endl;
    } else {
      cout << tmp1[sumDcopy] + 1 << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
const long long INF = (long long)(1e18) + 1;
int main() {
  int N;
  string X;
  cin >> N >> X;
  vector<int> tmp0(N);
  tmp0[0] = 0;
  for (int n = 1; n < N; ++n) {
    if (1 == n % 2) {
      tmp0[n] = tmp0[n / 2] + 1;
    } else {
      tmp0[n] = tmp0[n / 2];
    }
  }
  vector<int> tmp1(N);
  tmp1[0] = 0;
  for (int n = 1; n < N; ++n) {
    if (0 == n % tmp0[n]) {
      tmp1[n] = 1;
    } else {
      tmp1[n] = tmp1[n % tmp0[n]] + 1;
    }
  }
  int d = 0;
  for (int n = 0; n < N; ++n) {
    if ('1' == X[n]) {
      d++;
    }
  }

  vector<int> D1(N);
  int sumD1 = 0;
  {
    int num = 1;
    for (int n = 0; n < N; ++n) {
      D1[N - 1 - n] = num;
      if ('1' == X[N - 1 - n]) {
        sumD1 += num;
        sumD1 %= (d + 1);
      }
      num *= 2;
      num %= (d + 1);
    }
  }
  vector<int> D2(N);
  int sumD2 = 0;
  {
    int num = 1;
    for (int n = 0; n < N; ++n) {
      D2[N - 1 - n] = num;
      if ('1' == X[N - 1 - n]) {
        sumD2 += num;
        sumD2 %= (d - 1);
      }
      num *= 2;
      num %= (d - 1);
    }
  }
  // cout << d<< endl;
  // cout << sumD1 << endl;
  // cout << sumD2 << endl;

  for (int n = 0; n < N; ++n) {
    int dcopy = d;
    int sumDcopy = 0;
    string Xcopy = X;
    if ('0' == Xcopy[n]) {
      Xcopy[n] = '1';
      dcopy++;
      sumDcopy = sumD1 + D1[n];
      sumDcopy %= (dcopy);
    } else {
      Xcopy[n] = '0';
      dcopy--;
      sumDcopy = sumD2 + (dcopy)-D2[n];
      sumDcopy %= (dcopy);
    }
    // cout << sumDcopy<<"_";
    if (0 == sumDcopy) {
      cout << 1 << endl;
    } else {
      cout << tmp1[sumDcopy] + 1 << endl;
    }
  }
  return 0;
}