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

#include <deque>
#include <iostream>
#include <list>
#include <random>
#include <set>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
const int BIG = 100000007;

int main() {
  vector<ll> pcnts(300000, 0);
  for (ll i = 1; i <= pcnts.size(); ++i) {
    ll j = i;
    while (j != 0) {
      if ((j & 1) == 1)
        ++pcnts[i];
      j = j >> 1;
    }
  }

  ll n;
  string s;
  cin >> n >> s;

  ll popcount = 0;

  for (ll i = 0; i < n; ++i) {
    if (s[i] == '1') {
      ++popcount;
    }
  }

  vector<ll> mods1(n, 0);
  vector<ll> mods2(n, 0);

  ll mo1 = 1;
  ll mo2 = 1;

  ll tmod1 = 0;
  ll tmod2 = 0;

  if (popcount - 1 == 0) {

    for (ll i = 0; i < n; ++i) {
      mo2 %= (popcount + 1);

      mods2[i] = mo2;

      if (s[n - i - 1] == '1') {
        tmod2 += mods2[i];
        tmod2 %= (popcount + 1);
      }

      mo2 *= 2;
    }

    for (ll i = n - 1; i >= 0; --i) {
      ll tmod;
      if (s[n - 1 - i] == '1') {
        cout << 0 << endl;
        continue;
      } else {
        tmod = (tmod2 + mods2[i]) % (popcount + 1);
      }

      // cout << "i = " << i << ",tmod = " << tmod << endl;

      ll cnt = 1;
      ll next = tmod;
      while (next > 0) {
        next = next % pcnts[next];
        ++cnt;
      }

      cout << cnt << endl;
    }

  } else {
    for (ll i = 0; i < n; ++i) {
      mo1 %= (popcount - 1);
      mo2 %= (popcount + 1);

      mods1[i] = mo1;
      mods2[i] = mo2;

      if (s[n - i - 1] == '1') {
        tmod1 += mods1[i];
        tmod1 %= (popcount - 1);
        tmod2 += mods2[i];
        tmod2 %= (popcount + 1);
      }

      mo1 *= 2;
      mo2 *= 2;
    }

    for (ll i = n - 1; i >= 0; --i) {
      ll tmod;
      if (s[n - 1 - i] == '1') {
        tmod = (tmod1 - mods1[i]) % (popcount - 1);
        if (tmod < 0)
          tmod = popcount - 1 + tmod;
      } else {
        tmod = (tmod2 + mods2[i]) % (popcount + 1);
      }

      // cout << "i = " << i << ",tmod = " << tmod << endl;

      ll cnt = 1;
      ll next = tmod;
      while (next > 0) {
        next = next % pcnts[next];
        ++cnt;
      }

      cout << cnt << endl;
    }
  }

  /*
      for(ll i = 0;i < n;++i){
          cout << "i = " << i << ",mods1 = " << mods1[i] << ",pcnt = " <<
     (popcount - 1) << endl;
      }

      for(ll i = 0;i < n;++i){
          cout << "i = " << i << ",mods2 = " << mods2[i] << ",pcnt = " <<
     (popcount + 1) << endl;
      }

      cout << tmod1 << ":" << tmod2 << endl;
  */
}


Error: 0

Fixed submission:

#include <deque>
#include <iostream>
#include <list>
#include <random>
#include <set>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
const int BIG = 100000007;

int main() {
  vector<ll> pcnts(300000, 0);
  for (ll i = 1; i <= pcnts.size(); ++i) {
    ll j = i;
    while (j != 0) {
      if ((j & 1) == 1)
        ++pcnts[i];
      j = j >> 1;
    }
  }

  ll n;
  string s;
  cin >> n >> s;

  ll popcount = 0;

  for (ll i = 0; i < n; ++i) {
    if (s[i] == '1') {
      ++popcount;
    }
  }

  vector<ll> mods1(n, 0);
  vector<ll> mods2(n, 0);

  ll mo1 = 1;
  ll mo2 = 1;

  ll tmod1 = 0;
  ll tmod2 = 0;

  for (ll i = 0; i < n; ++i) {
    mo1 %= (popcount - 1);
    mo2 %= (popcount + 1);

    mods1[i] = mo1;
    mods2[i] = mo2;

    if (s[n - i - 1] == '1') {
      tmod1 += mods1[i];
      tmod1 %= (popcount - 1);
      tmod2 += mods2[i];
      tmod2 %= (popcount + 1);
    }

    mo1 *= 2;
    mo2 *= 2;
  }
  /*
      for(ll i = 0;i < n;++i){
          cout << "i = " << i << ",mods1 = " << mods1[i] << ",pcnt = " <<
     (popcount - 1) << endl;
      }

      for(ll i = 0;i < n;++i){
          cout << "i = " << i << ",mods2 = " << mods2[i] << ",pcnt = " <<
     (popcount + 1) << endl;
      }

      cout << tmod1 << ":" << tmod2 << endl;
  */
  for (ll i = n - 1; i >= 0; --i) {
    ll tmod;
    if (s[n - 1 - i] == '1') {
      tmod = (tmod1 - mods1[i]) % (popcount - 1);
      if (tmod < 0)
        tmod = popcount - 1 + tmod;
    } else {
      tmod = (tmod2 + mods2[i]) % (popcount + 1);
    }

    // cout << "i = " << i << ",tmod = " << tmod << endl;

    ll cnt = 1;
    ll next = tmod;
    while (next > 0) {
      next = next % pcnts[next];
      ++cnt;
    }

    cout << cnt << endl;
  }
}