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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const static ll INF = 1e15;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
// __uint128_t
const ll MOD = 1e9 + 7;

vector<ll> DP(1e6, -1);
vector<ll> POP(1e6, -1);

ll func(ll a) {
  ll cnt = 0;
  vector<ll> temp;
  while (a != 0) {
    if (DP[a] != -1) {
      cnt += DP[a];
      for (int i = 0; i < temp.size(); i++)
        DP[a] = cnt - i;
      return cnt;
    }
    temp.push_back(a);
    ll S = POP[a];
    a = a % S;
    cnt++;
  }
  for (int i = 0; i < temp.size(); i++)
    DP[a] = cnt - i;
  return cnt;
}

int main() {
  ll N;
  cin >> N;
  string S;
  cin >> S;
  for (int i = 0; i <= N; i++) {
    bitset<20> B = i;
    POP[i] = B.count();
  }

  ll BS = 0;
  for (auto ch : S)
    if (ch == '1')
      BS++;
  ll MULS = 0;
  ll MULR = 0;
  vector<ll> MODPOWS(1e6, 0), MODPOWR(1e6, 0);

  MODPOWS[0] = 1, MODPOWR[0] = 1;
  for (int i = 1; i < 1e6; i++) {
    MODPOWS[i] = MODPOWS[i - 1] * 2;
    MODPOWS[i] %= (BS + 1);
    MODPOWR[i] = MODPOWR[i - 1] * 2;
    if (BS > 1)
      MODPOWR[i] %= (BS - 1);
  }

  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      // MULS += modpow(2, N-i-1, BS+1);
      MULS += MODPOWS[N - i - 1];
      MULS %= (BS + 1);
      if (1 < BS) {
        // MULR += modpow(2, N-i-1, BS-1);
        MULR += MODPOWR[N - i - 1];
        MULR %= (BS - 1);
      }
    }
  }
  for (int i = 0; i < N; i++) {
    ll MUL;
    if (S[i] == '1') {
      if (BS == 1) {
        cout << 0 << endl;
        continue;
      }
      if (1 < BS)
        MUL = MULR - MODPOWR[N - i - 1];
      if (1 < BS)
        MUL = (MUL + BS - 1) % (BS - 1);
    } else {
      MUL = MULS + MODPOWS[N - i - 1];
      MUL = (MUL + BS + 1) % (BS + 1);
    }
    cout << func(MUL) + 1 << endl;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const static ll INF = 1e15;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
// __uint128_t
const ll MOD = 1e9 + 7;

vector<ll> DP(1e6, -1);
vector<ll> POP(1e6, -1);

ll func(ll a) {
  ll cnt = 0;
  vector<ll> temp;
  while (a != 0) {
    if (DP[a] != -1) {
      cnt += DP[a];
      for (int i = 0; i < temp.size(); i++)
        DP[a] = cnt - i;
      return cnt;
    }
    temp.push_back(a);
    ll S = POP[a];
    a = a % S;
    cnt++;
  }
  for (int i = 0; i < temp.size(); i++)
    DP[a] = cnt - i;
  return cnt;
}

int main() {
  ll N;
  cin >> N;
  string S;
  cin >> S;
  for (int i = 0; i <= N; i++) {
    bitset<20> B = i;
    POP[i] = B.count();
  }

  ll BS = 0;
  for (auto ch : S)
    if (ch == '1')
      BS++;
  ll MULS = 0;
  ll MULR = 0;
  vector<ll> MODPOWS(N + 4, 0), MODPOWR(N + 4, 0);

  MODPOWS[0] = 1, MODPOWR[0] = 1;
  for (int i = 1; i < N + 1; i++) {
    MODPOWS[i] = MODPOWS[i - 1] * 2;
    MODPOWS[i] %= (BS + 1);
    MODPOWR[i] = MODPOWR[i - 1] * 2;
    MODPOWR[i] %= (BS - 1);
  }
  /*

      for(int i = 0; i < N; i++){
          if(S[i] == '1') {
              //MULS += modpow(2, N-i-1, BS+1);
              MULS += MODPOWS[N-i-1];
              MULS %= (BS+1);
              if(1 < BS) {
                  //MULR += modpow(2, N-i-1, BS-1);
                  MULR += MODPOWR[N-i-1];
                  MULR %= (BS-1);
              }
          }
      }
      for(int i = 0; i < N; i++){
          ll MUL;
          if(S[i] == '1'){
              if(BS == 1 && i == N-1) {cout << 0 << endl; continue; }
              if(1 < BS)MUL = MULR - MODPOWR[N-i-1];
              if(1 < BS)MUL = (MUL + BS-1) % (BS-1);
          }
          else{
              MUL = MULS + MODPOWS[N-i-1];
              MUL = (MUL + BS+1) % (BS+1);
          }
          cout << func(MUL)+1 << endl;
      }*/
}