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

#include <bits/stdc++.h>

#include <iostream>
// #include <algorithm>
//  #include <iomanip>
#define ll long long
#define map unordered_map
#define set unordered_set
#define pll pair<ll, ll>
#define vll vector<ll>
#define mll map<ll, ll>

using namespace std;

const ll MOD = 1000000007LL;
const ll INF = (1LL << 60LL);

ll am_memo_m1[200005];
ll am_memo_p1[200005];

ll count_bit(ll val) {
  ll r = 0;
  ll c = 1;
  for (;;) {
    if (val & c) {
      r += 1;
    }
    c *= 2;
    if (c > val) {
      break;
    }
  }
  return r;
}

ll calc(ll val) {
  ll r = 0;
  for (;;) {
    if (val == 0) {
      break;
    }
    ll bnum = count_bit(val);
    val = val % bnum;
    r += 1;
  }
  return r;
}

int main() {
  ll N;
  scanf("%lld", &N);

  string s;
  cin >> s;

  ll popcount = 0;

  vector<int> digits(N + 1);
  for (ll i = N - 1; i >= 0; i--) {
    ll index = N - i;
    if (s[i] == '1') {
      digits[index] = 1;
      popcount++;
    } else {
      digits[index] = 0;
    }
  }
  // cout << "popcount:" << popcount << endl;

  ll am_m1 = 0;
  ll am_p1 = 0;

  ll cur_m1 = 1;
  ll cur_p1 = 1;

  ll popcount_m1 = popcount - 1;
  ll popcount_p1 = popcount + 1;

  for (ll i = 1; i <= N; i++) {
    if (digits[i]) {
      if (popcount_m1 > 0) {
        am_m1 = (am_m1 + cur_m1) % popcount_m1;
      }
      am_p1 = (am_p1 + cur_p1) % popcount_p1;
    }
    am_memo_m1[i] = cur_m1;
    am_memo_p1[i] = cur_p1;

    if (popcount_m1 > 0) {
      cur_m1 = (cur_m1 * 2) % popcount_m1;
    }
    cur_p1 = (cur_p1 * 2) % popcount_p1;
  }

  // cout << "digits[1]:" << digits[1] << endl;  // => 1
  // cout << "digits[2]:" << digits[2] << endl;  // => 1
  // cout << "digits[3]:" << digits[3] << endl;  // => 0

  // cout << "am_memo_p1[1]:" << am_memo_p1[1] << endl;  // => 1
  // cout << "am_memo_p1[2]:" << am_memo_p1[2] << endl;  // => 2
  // cout << "am_memo_p1[3]:" << am_memo_p1[3] << endl;  // => 1

  // cout << "popcount_p1:" << popcount_p1 << endl;

  // cout << "am_m1:" << am_m1 << endl;
  // cout << "am_p1:" << am_p1 << endl;

  // digits = [1, 1, 0]
  for (ll i = N; i >= 1; i--) {
    ll popcount;
    ll am;
    if (digits[i]) {
      popcount = popcount_m1;
      if (popcount_m1 == 0) {
        am = 0;
      } else {
        popcount = popcount_m1;
        // i桁目が消滅する
        am = (((am_m1 - am_memo_m1[i]) % popcount) + popcount) % popcount;
      }
    } else {
      popcount = popcount_p1;
      // i桁目が出現する
      am = ((am_p1 + am_memo_p1[i]) % popcount);
    }

    // cout << 1 + calc(am) << endl;
    if (popcount == 0) {
      cout << 0 << endl;
    } else {
      cout << 1 + calc(am) << endl;
    }
  }

  ll am_ll = 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#include <iostream>
// #include <algorithm>
//  #include <iomanip>
#define ll long long
#define map unordered_map
#define set unordered_set
#define pll pair<ll, ll>
#define vll vector<ll>
#define mll map<ll, ll>

using namespace std;

const ll MOD = 1000000007LL;
const ll INF = (1LL << 60LL);

ll am_memo_m1[200005];
ll am_memo_p1[200005];

ll count_bit(ll val) {
  ll r = 0;
  ll c = 1;
  for (;;) {
    if (val & c) {
      r += 1;
    }
    c *= 2;
    if (c > val) {
      break;
    }
  }
  return r;
}

ll calc(ll val) {
  ll r = 0;
  for (;;) {
    if (val == 0) {
      break;
    }
    ll bnum = count_bit(val);
    val = val % bnum;
    r += 1;
  }
  return r;
}

int main() {
  ll N;
  scanf("%lld", &N);

  string s;
  cin >> s;

  ll popcount = 0;

  vector<int> digits(N + 1);
  for (ll i = N - 1; i >= 0; i--) {
    ll index = N - i;
    if (s[i] == '1') {
      digits[index] = 1;
      popcount++;
    } else {
      digits[index] = 0;
    }
  }
  // cout << "popcount:" << popcount << endl;

  ll am_m1 = 0;
  ll am_p1 = 0;

  ll cur_m1 = 1;
  ll cur_p1 = 1;

  ll popcount_m1 = popcount - 1;
  ll popcount_p1 = popcount + 1;

  for (ll i = 1; i <= N; i++) {
    if (digits[i]) {
      if (popcount_m1 > 0) {
        am_m1 = (am_m1 + cur_m1) % popcount_m1;
      }
      am_p1 = (am_p1 + cur_p1) % popcount_p1;
    }
    am_memo_m1[i] = cur_m1;
    am_memo_p1[i] = cur_p1;

    if (popcount_m1 > 0) {
      cur_m1 = (cur_m1 * 2) % popcount_m1;
    }
    cur_p1 = (cur_p1 * 2) % popcount_p1;
  }

  // cout << "digits[1]:" << digits[1] << endl;  // => 1
  // cout << "digits[2]:" << digits[2] << endl;  // => 1
  // cout << "digits[3]:" << digits[3] << endl;  // => 0

  // cout << "am_memo_p1[1]:" << am_memo_p1[1] << endl;  // => 1
  // cout << "am_memo_p1[2]:" << am_memo_p1[2] << endl;  // => 2
  // cout << "am_memo_p1[3]:" << am_memo_p1[3] << endl;  // => 1

  // cout << "popcount_p1:" << popcount_p1 << endl;

  // cout << "am_m1:" << am_m1 << endl;
  // cout << "am_p1:" << am_p1 << endl;

  // digits = [1, 1, 0]
  for (ll i = N; i >= 1; i--) {
    ll popcount;
    ll am;
    if (digits[i]) {
      popcount = popcount_m1;
      am = am_m1;
      // i桁目が消滅する
      am = (((am - am_memo_m1[i]) % popcount) + popcount) % popcount;
    } else {
      popcount = popcount_p1;
      am = am_p1;
      // i桁目が出現する
      am = ((am + am_memo_p1[i]) % popcount);
    }

    // cout << "am:" << am << endl;

    cout << 1 + calc(am) << endl;
  }

  ll am_ll = 0;
}