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

/*  -*- coding: utf-8 -*-
 *
 * d.cc: D - Anything Goes to Zero
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 200000;

/* typedef */

typedef long long ll;

/* global variables */

char s[MAX_N + 4];
int bnums[MAX_N + 1], fs[MAX_N + 1];
int bns[2], rs[2];

/* subroutines */

int powmod(int a, int n, int mod) { // a^n % MOD
  int pm = 1;
  while (n > 0) {
    if (n & 1)
      pm = (ll)pm * a % mod;
    a = (ll)a * a % mod;
    n >>= 1;
  }
  return pm;
}

/* main */

int main() {
  for (int bits = 1, msb = 1; bits <= MAX_N; bits++) {
    if ((msb << 1) <= bits)
      msb <<= 1;
    bnums[bits] = bnums[bits ^ msb] + 1;
    fs[bits] = fs[bits % bnums[bits]] + 1;
  }

  int n;
  scanf("%d%s", &n, s);

  int bn = 0;
  for (int i = 0; i < n; i++)
    bn += s[i] - '0';
  bns[0] = bn - 1, bns[1] = bn + 1;

  for (int i = 0; i < n; i++)
    for (int j = 0; j < 2; j++)
      if (bns[j] > 0)
        rs[j] = (rs[j] * 2 + s[i] - '0') % bns[j];

  for (int i = 0; i < n; i++) {
    int j = '1' - s[i];
    int f = 0;
    if (bns[j] > 0) {
      int p = powmod(2, n - 1 - i, bns[j]);
      if (j == 0)
        p = (bns[j] - p) % bns[j];
      f = fs[(rs[j] + p) % bns[j]] + 1;
    }
    printf("%d\\n", f);
    // printf("i=%d, j=%d, r=%d, f=%d\\n", i, j, r, fs[r]);
  }

  return 0;
}


Error: 0

Fixed submission:

/*  -*- coding: utf-8 -*-
 *
 * d.cc: D - Anything Goes to Zero
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 200000;

/* typedef */

typedef long long ll;

/* global variables */

char s[MAX_N + 4];
int bnums[MAX_N + 1], fs[MAX_N + 1];
int bns[2], rs[2];

/* subroutines */

int powmod(int a, int n, int mod) { // a^n % MOD
  int pm = 1;
  while (n > 0) {
    if (n & 1)
      pm = (ll)pm * a % mod;
    a = (ll)a * a % mod;
    n >>= 1;
  }
  return pm;
}

/* main */

int main() {
  for (int bits = 1, msb = 1; bits <= MAX_N; bits++) {
    if ((msb << 1) <= bits)
      msb <<= 1;
    bnums[bits] = bnums[bits ^ msb] + 1;
    fs[bits] = fs[bits % bnums[bits]] + 1;
  }

  int n;
  scanf("%d%s", &n, s);

  int bn = 0;
  for (int i = 0; i < n; i++)
    bn += s[i] - '0';
  bns[0] = bn - 1, bns[1] = bn + 1;

  for (int i = 0; i < n; i++)
    for (int j = 0; j < 2; j++)
      if (bns[j] > 0)
        rs[j] = (rs[j] * 2 + s[i] - '0') % bns[j];

  for (int i = 0; i < n; i++) {
    int j = '1' - s[i];
    int p = powmod(2, n - 1 - i, bns[j]);
    if (j == 0)
      p = (bns[j] - p) % bns[j];
    int r = (bns[j] == 0) ? 0 : (rs[j] + p) % bns[j];
    printf("%d\\n", fs[r] + 1);
    // printf("i=%d, j=%d, r=%d, f=%d\\n", i, j, r, fs[r]);
  }

  return 0;
}