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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

int f[200010];
int n;

char x[200010];
int posCountL;
int posCount; // 原始数据
int posCountH;
int remL[200010]; // 对 -1的数据
int remM[200010]; // 对 原始数据
int remH[200010]; // 对 +1的数据
int curRemL;
int curRemH;

int doRevert(int pos) {
  if (x[pos] == '0') {
    x[pos] = '1';
    return 1;
  } else {
    x[pos] = '0';
    return -1;
  }
}

int getF(int num) {
  if (f[num] != -1)
    return f[num];

  int count = 0;
  int theNum = num;
  while (theNum > 0) {
    int r = theNum % 2;
    count = count + r;
    theNum = theNum / 2;
  }

  int target = num % count;
  f[num] = getF(target) + 1;
  return f[num];
}

int main() {
  for (int i = 0; i < 200010; i++)
    f[i] = -1;
  f[0] = 0;
  f[1] = 1;

  scanf("%d\\n", &n);
  scanf("%s\\n", x);

  if ((n == 1) && (x[0] == '1')) {
    printf("0\\n");
    return 0;
  }

  posCount = 0;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1')
      posCount++;
  }
  posCountL = posCount - 1;
  posCountH = posCount + 1;
  remL[0] = 1;
  remM[0] = 1;
  remH[0] = 1;
  for (int i = 1; i <= n; i++) {
    if (posCountL > 0) {
      remL[i] = (remL[i - 1] * 2) % posCountL;
    }
    if (posCount > 0) {
      remM[i] = (remM[i - 1] * 2) % posCount;
    }

    remH[i] = (remH[i - 1] * 2) % posCountH;
  }

  curRemL = 0;
  curRemH = 0;
  for (int i = 0; i < n; i++) {
    curRemL = curRemL * 2;
    curRemH = curRemH * 2;
    if (x[i] == '1') {
      curRemL++;
      curRemH++;
    }

    if (posCountL > 0) {
      curRemL = curRemL % posCountL;
    }

    curRemH = curRemH % posCountH;
  }

  int realBase;
  int realRem;
  int ans;
  for (int i = 0; i < n; i++) {
    realBase = n - i - 1;
    int diff = doRevert(i);
    if (diff == -1) {
      if (posCountL == 0) {
        ans = 0;
      } else {
        // L侧
        realRem = curRemL + posCountL - remL[realBase];
        realRem = realRem % posCountL;
        ans = getF(realRem) + 1;
      }
    } else { // 1
      realRem = curRemH + remH[realBase];
      realRem = realRem % posCountH;
      ans = getF(realRem) + 1;
    }

    printf("%d\\n", ans);

    doRevert(i);
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

int f[200010];
int n;

char x[200010];
int posCountL;
int posCount; // 原始数据
int posCountH;
int remL[200010]; // 对 -1的数据
int remM[200010]; // 对 原始数据
int remH[200010]; // 对 +1的数据
int curRemL;
int curRemH;

int doRevert(int pos) {
  if (x[pos] == '0') {
    x[pos] = '1';
    return 1;
  } else {
    x[pos] = '0';
    return -1;
  }
}

int getF(int num) {
  if (f[num] != -1)
    return f[num];

  int count = 0;
  int theNum = num;
  while (theNum > 0) {
    int r = theNum % 2;
    count = count + r;
    theNum = theNum / 2;
  }

  int target = num % count;
  f[num] = getF(target) + 1;
  return f[num];
}

int main() {
  for (int i = 0; i < 200010; i++)
    f[i] = -1;
  f[0] = 0;
  f[1] = 1;

  scanf("%d\\n", &n);
  scanf("%s\\n", x);

  if ((n == 1) && (x[0] == '1')) {
    printf("1\\n");
    return 0;
  }

  posCount = 0;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1')
      posCount++;
  }
  posCountL = posCount - 1;
  posCountH = posCount + 1;
  remL[0] = 1;
  remM[0] = 1;
  remH[0] = 1;
  for (int i = 1; i <= n; i++) {
    remL[i] = (remL[i - 1] * 2) % posCountL;
    remM[i] = (remM[i - 1] * 2) % posCount;
    remH[i] = (remH[i - 1] * 2) % posCountH;
  }

  curRemL = 0;
  curRemH = 0;
  for (int i = 0; i < n; i++) {
    curRemL = curRemL * 2;
    curRemH = curRemH * 2;
    if (x[i] == '1') {
      curRemL++;
      curRemH++;
    }
    curRemL = curRemL % posCountL;
    curRemH = curRemH % posCountH;
  }

  int realBase;
  int realRem;
  int ans;
  for (int i = 0; i < n; i++) {
    realBase = n - i - 1;
    int diff = doRevert(i);
    if (diff == -1) {
      // L侧
      realRem = curRemL + posCountL - remL[realBase];
      realRem = realRem % posCountL;
      ans = getF(realRem) + 1;
    } else { // 1
      realRem = curRemH + remH[realBase];
      realRem = realRem % posCountH;
      ans = getF(realRem) + 1;
    }

    printf("%d\\n", ans);

    doRevert(i);
  }

  return 0;
}