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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int, int> pint;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
// 問題文および制約はちゃんと確認しよう！
// サイズは10^5じゃなくて2×10^5とかかもしれないし、重要な制約・条件を見落としているかも
// とりあえずサンプルを読んでから解法を考えよう？
int zyo[200100][2];
int dp[200100];
int out[200100];
int cal(int x) {
  if (x == 0)
    return 0;
  if (dp[x] >= 0)
    return dp[x];
  return dp[x] = 1 + cal(x % __builtin_popcount(x));
}
int main() {
  memset(dp, -1, sizeof(dp));
  int n, m = 0;
  string s;
  cin >> n;
  cin >> s;
  reverse(All(s));
  rep(i, n) m += (s[i] - '0');
  int mo[2] = {m + 1, max(1, m - 1)};
  zyo[0][0] = zyo[0][1] = 1;
  rep(i, n + 10) rep(j, 2) {
    zyo[i + 1][j] = zyo[i][j] * 2;
    zyo[i + 1][j] %= mo[j];
  }
  int sum[2] = {0, 0};
  rep(i, n) rep(j, 2) {
    sum[j] += zyo[i][j] * (s[i] - '0');
    sum[j] %= mo[j];
  }
  rep(i, n) {
    if (s[i] == '1') {
      if (m == 1) {
        out[i] = 0;
        continue;
      }
      int su = sum[1] - zyo[i][1];
      su %= mo[1];
      su += mo[1];
      su %= mo[1];
      out[i] = 1 + cal(su);
    } else {
      int su = sum[0] + zyo[i][0];
      su %= mo[0];
      out[i] = 1 + cal(su);
    }
  }
  rep(i, n) cout << out[n - 1 - i] << endl;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int, int> pint;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
// 問題文および制約はちゃんと確認しよう！
// サイズは10^5じゃなくて2×10^5とかかもしれないし、重要な制約・条件を見落としているかも
// とりあえずサンプルを読んでから解法を考えよう？
int zyo[200100][2];
int dp[200100];
int out[200100];
int cal(int x) {
  if (x == 0)
    return 0;
  if (dp[x] >= 0)
    return dp[x];
  return dp[x] = 1 + cal(x % __builtin_popcount(x));
}
int main() {
  memset(dp, -1, sizeof(dp));
  int n, m;
  string s;
  cin >> n;
  cin >> s;
  reverse(All(s));
  rep(i, n) m += (s[i] - '0');
  int mo[2] = {m + 1, max(1, m - 1)};
  zyo[0][0] = zyo[0][1] = 1;
  rep(i, n + 10) rep(j, 2) {
    zyo[i + 1][j] = zyo[i][j] * 2;
    zyo[i + 1][j] %= mo[j];
  }
  int sum[2] = {0, 0};
  rep(i, n) rep(j, 2) {
    sum[j] += zyo[i][j] * (s[i] - '0');
    sum[j] %= mo[j];
  }
  rep(i, n) {
    if (s[i] == '1') {
      if (m == 1) {
        out[i] = 0;
        continue;
      }
      int su = sum[1] - zyo[i][1];
      su %= mo[1];
      su += mo[1];
      su %= mo[1];
      out[i] = 1 + cal(su);
    } else {
      int su = sum[0] + zyo[i][0];
      su %= mo[0];
      out[i] = 1 + cal(su);
    }
  }
  rep(i, n) cout << out[n - 1 - i] << endl;
}