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

#include <algorithm>
#include <assert.h>
#include <bit>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

typedef long double ld;
typedef long long ll;

using namespace std;

/* TEMPLATE ENDS HERE */

#define MAXN (1 << 20)

string s;
int a[MAXN];
int n;
int cnt;
int ret[MAXN][3];
int cur[3];
int nmod[MAXN];
int corr[MAXN];
int f[MAXN];

int solve(int k) {
  if (!k) {
    return 0;
  }
  int &ret = f[k];
  if (ret >= 0) {
    return ret;
  }
  ret = 1 + solve(k % __builtin_popcount(k));
  return ret;
}

int main() {
  cout << fixed << setprecision(12);
  memset(f, -1, sizeof(f));
  for (int i = 0; i < MAXN; i++) {
    f[i] = solve(i);
  }

  cin >> n;
  cin >> s;
  for (int i = 0; i < n; i++) {
    a[i] = s[i] - '0';
    cnt += a[i];
  }
  for (int j = 0; j < 3; j++) {
    cur[j] = 1;
  }
  for (int i = n - 1; i >= 0; i--) {
    if (a[i]) {
      nmod[i] = 0;
      corr[i] = -cur[nmod[i]];
    } else {
      nmod[i] = 2;
      corr[i] = cur[nmod[i]];
    }
    for (int j = 0; j < 3; j++) {
      int mod = cnt + j - 1;
      if (!mod) {
        continue;
      }
      ret[i][j] = ret[i + 1][j];
      ret[i][j] += cur[j] * a[i];
      ret[i][j] %= mod;
      cur[j] <<= 1;
      cur[j] %= mod;
    }
  }
  for (int i = 0; i < n; i++) {
    int ans = ret[0][nmod[i]];
    int cmod = nmod[i] + cnt - 1;
    if (!cmod) {
      cout << 0 << endl;
      continue;
    }
    ans += corr[i];
    ans = (ans + cmod) % cmod;
    cout << f[ans] + 1 << endl;
  }
  return 0;
}

ll MOD = ((ll)1e9 + 7);

struct mod {
  ll x;

  mod(ll tx = 0) {
    x = tx;
    x %= MOD;
  }

  mod &operator=(const ll &tx) {
    x = tx % MOD;
    return *this;
  }

  mod &operator=(const mod &m2) {
    x = m2.x;
    return *this;
  }

  mod operator+(const mod &m2) const { return mod((x + m2.x) % MOD); }

  mod &operator+=(const mod &m2) {
    x += m2.x;
    x %= MOD;
    return *this;
  }

  mod operator-(const mod &m2) const { return mod((x - m2.x + MOD) % MOD); }

  mod &operator-=(const mod &m2) {
    x -= m2.x;
    x = (x + MOD) % MOD;
    return *this;
  }

  mod operator*(const mod &m2) const { return mod((x * m2.x) % MOD); }

  mod &operator*=(const mod &m2) {
    x *= m2.x;
    x %= MOD;
    return *this;
  }

  mod mypow(ll k) const {
    mod ret = 1;
    mod cur = x;
    while (k) {
      if (k & 1) {
        ret *= cur;
      }
      cur *= cur;
      k >>= 1;
    }
    return ret;
  }

  mod inv() const { return mypow(MOD - 2); }

  mod operator/(const mod &m2) const { return (*this) * m2.inv(); }

  mod &operator/=(const mod &m2) {
    x *= m2.inv().x;
    x %= MOD;
    return *this;
  }

  bool operator<(const mod &m2) const { return x < m2.x; }
};

ostream &operator<<(ostream &os, const mod &m) { return os << m.x; }


Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bit>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

typedef long double ld;
typedef long long ll;

using namespace std;

/* TEMPLATE ENDS HERE */

#define MAXN (1 << 20)

string s;
int a[MAXN];
int n;
int cnt;
int ret[MAXN][3];
int cur[3];
int nmod[MAXN];
int corr[MAXN];
int f[MAXN];

int solve(int k) {
  if (!k) {
    return 0;
  }
  int &ret = f[k];
  if (ret >= 0) {
    return ret;
  }
  ret = 1 + solve(k % __builtin_popcount(k));
  return ret;
}

int main() {
  cout << fixed << setprecision(12);
  memset(f, -1, sizeof(f));
  for (int i = 0; i < MAXN; i++) {
    f[i] = solve(i);
  }

  cin >> n;
  cin >> s;
  for (int i = 0; i < n; i++) {
    a[i] = s[i] - '0';
    cnt += a[i];
  }
  for (int j = 0; j < 3; j++) {
    cur[j] = 1;
  }
  for (int i = n - 1; i >= 0; i--) {
    if (a[i]) {
      nmod[i] = 0;
      corr[i] = -cur[nmod[i]];
    } else {
      nmod[i] = 2;
      corr[i] = cur[nmod[i]];
    }
    for (int j = 0; j < 3; j++) {
      int mod = cnt + j - 1;
      ret[i][j] = ret[i + 1][j];
      ret[i][j] += cur[j] * a[i];
      ret[i][j] %= mod;
      cur[j] <<= 1;
      cur[j] %= mod;
    }
  }
  for (int i = 0; i < n; i++) {
    if ((cnt == 1) && a[i]) {
      cout << 0 << endl;
      continue;
    }
    int ans = ret[0][nmod[i]];
    int cmod = nmod[i] + cnt - 1;
    ans += corr[i];
    ans = (ans + cmod) % cmod;
    cout << f[ans] + 1 << endl;
  }
  return 0;
}

ll MOD = ((ll)1e9 + 7);

struct mod {
  ll x;

  mod(ll tx = 0) {
    x = tx;
    x %= MOD;
  }

  mod &operator=(const ll &tx) {
    x = tx % MOD;
    return *this;
  }

  mod &operator=(const mod &m2) {
    x = m2.x;
    return *this;
  }

  mod operator+(const mod &m2) const { return mod((x + m2.x) % MOD); }

  mod &operator+=(const mod &m2) {
    x += m2.x;
    x %= MOD;
    return *this;
  }

  mod operator-(const mod &m2) const { return mod((x - m2.x + MOD) % MOD); }

  mod &operator-=(const mod &m2) {
    x -= m2.x;
    x = (x + MOD) % MOD;
    return *this;
  }

  mod operator*(const mod &m2) const { return mod((x * m2.x) % MOD); }

  mod &operator*=(const mod &m2) {
    x *= m2.x;
    x %= MOD;
    return *this;
  }

  mod mypow(ll k) const {
    mod ret = 1;
    mod cur = x;
    while (k) {
      if (k & 1) {
        ret *= cur;
      }
      cur *= cur;
      k >>= 1;
    }
    return ret;
  }

  mod inv() const { return mypow(MOD - 2); }

  mod operator/(const mod &m2) const { return (*this) * m2.inv(); }

  mod &operator/=(const mod &m2) {
    x *= m2.inv().x;
    x %= MOD;
    return *this;
  }

  bool operator<(const mod &m2) const { return x < m2.x; }
};

ostream &operator<<(ostream &os, const mod &m) { return os << m.x; }