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

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\\n"

using namespace std;
long long MOD = (long long)1e9 + 7;

int n, pcnt[2], len, f[200009], pos[2];
string s, x[2];
long long rmd[2], ba;
bool flag[2];

long long quickpow(long long b, int e) {
  return e ? ((e & 1 ? b : 1) *
              quickpow(((b % MOD) * (b % MOD)) % MOD, e >> 1)) %
                 MOD
           : 1;
}

int gun(long long cur) {
  int cnt = 0;
  while (cur) {
    if (cur & 1)
      cnt++;
    cur /= 2;
  }
  return cnt;
}

void fun(int ix, long long cur) {
  while (cur) {
    cur %= gun(cur);
    f[ix]++;
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> s;
  len = s.length();
  x[0] = s;
  x[1] = s;
  for (int j = 0; j < 2; j++) {
    for (int i = 0; i < len; i++) {
      if (s[i] == '0' + j) {
        x[j][i] = 1 - j + '0';
        pos[j] = i;
        flag[j] = true;
        break;
      }
    }
  }
  for (int j = 0; j < 2; j++) {
    if (flag[j]) {
      for (int i = 0; i < len; i++) {
        pcnt[j] += x[j][i] - '0';
      }
      ba = 1;
      if (pcnt[j] == 0)
        continue;
      for (int i = len - 1; i >= 0; i--) {
        if (x[j][i] == '1') {
          rmd[j] += ba % pcnt[j];
          rmd[j] %= pcnt[j];
        }
        ba = ba + ba;
        ba %= pcnt[j];
      }
    }
  }
  for (int i = 0; i < len; i++) {
    int cur = s[i] - '0';
    long long tmp = rmd[cur];
    MOD = pcnt[cur];
    if (pcnt[cur] == 0) {
      cout << 0 << E;
      continue;
    }
    f[i] = 1;
    if (cur == 0) {
      tmp -= quickpow(2, len - pos[cur] - 1);
      if (tmp < 0)
        tmp += MOD;
      tmp += quickpow(2, len - i - 1);
      tmp %= MOD;
    } else {
      tmp += quickpow(2, len - pos[cur] - 1);
      tmp %= MOD;
      tmp -= quickpow(2, len - i - 1);
      if (tmp < 0)
        tmp += MOD;
    }
    fun(i, tmp);
    cout << f[i] << E;
  }
  // system("pause");
  return 0;
}

Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\\n"

using namespace std;
long long MOD = (long long)1e9 + 7;

int n, pcnt[2], len, f[200009], pos[2];
string s, x[2];
long long rmd[2], ba;
bool flag[2];

long long quickpow(long long b, int e) {
  return e ? ((e & 1 ? b : 1) *
              quickpow(((b % MOD) * (b % MOD)) % MOD, e >> 1)) %
                 MOD
           : 1;
}

int gun(long long cur) {
  int cnt = 0;
  while (cur) {
    if (cur & 1)
      cnt++;
    cur /= 2;
  }
  return cnt;
}

void fun(int ix, long long cur) {
  while (cur) {
    cur %= gun(cur);
    f[ix]++;
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> s;
  len = s.length();
  x[0] = s;
  x[1] = s;
  for (int j = 0; j < 2; j++) {
    for (int i = 0; i < len; i++) {
      if (s[i] == '0' + j) {
        x[j][i] = 1 - j + '0';
        pos[j] = i;
        flag[j] = true;
        break;
      }
    }
  }
  for (int j = 0; j < 2; j++) {
    if (flag[j]) {
      for (int i = 0; i < len; i++) {
        pcnt[j] += x[j][i] - '0';
      }
      ba = 1;
      for (int i = len - 1; i >= 0; i--) {
        if (x[j][i] == '1') {
          rmd[j] += ba % pcnt[j];
          rmd[j] %= pcnt[j];
        }
        ba = ba + ba;
        ba %= pcnt[j];
      }
    }
  }
  for (int i = 0; i < len; i++) {
    f[i] = 1;
    int cur = s[i] - '0';
    long long tmp = rmd[cur];
    MOD = pcnt[cur];
    if (cur == 0) {
      tmp -= quickpow(2, len - pos[cur] - 1);
      if (tmp < 0)
        tmp += MOD;
      tmp += quickpow(2, len - i - 1);
      tmp %= MOD;
    } else {
      tmp += quickpow(2, len - pos[cur] - 1);
      tmp %= MOD;
      tmp -= quickpow(2, len - i - 1);
      if (tmp < 0)
        tmp += MOD;
    }
    fun(i, tmp);
    cout << f[i] << E;
  }
  // system("pause");
  return 0;
}