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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>

// #include <bits/stdc++.h>
#define maxs(x, y) x = max(x, y)
#define mins(x, y) x = min(x, y)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, i0, n) for (int(i) = (i0); (i) < (n); (i)++)
#define FORR(i, i0, n) for (int(i) = (n)-1; (i) >= (i0); (i)--)
#define SORT(x) sort(x.begin(), x.end())
#define SORTR(x) sort(x.begin(), x.end(), greater<int>())
#define rn return
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple

using namespace std;
using ll = long long;
typedef std::pair<int, int> pii;
typedef std::vector<int> vi;
typedef std::vector<pii> vii;

#define PI 3.14159265358979323846264338327950L
const int mod = 1000000007;

void bin(int n) {
  string ans;
  while (n) {
    ans = to_string(n % 2) + ans;
    n /= 2;
  }
  cout << ans << endl;
}

int mod2[2][200001];

void init(int minus, int plus) {
  int vm = 1;
  int vp = 1;

  if (minus)
    mod2[0][0] = 1 % minus;
  mod2[1][0] = 1 % plus;

  for (int i = 1; i < 200001; i++) {
    if (minus)
      vm *= 2;
    vp *= 2;
    if (minus)
      vm %= minus;
    vp %= plus;
    mod2[0][i] = vm;
    mod2[1][i] = vp;
  }
}

int subsolve(int n, int c) {
  while (n) {
    n %= __builtin_popcount(n);
    c++;
  }
  return c;
}
void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int ones = 0;
  for (auto e : s) {
    if (e == '1')
      ones++;
  }

  int plus = ones + 1;
  int minus = ones - 1;

  init(minus, plus);

  int vm = 0;
  int vp = 0;

  for (int i = 0; i < n; i++) {
    if (s[n - 1 - i] == '1') {
      vm += mod2[0][i];
      vp += mod2[1][i];
      if (minus)
        vm %= minus;
      vp %= plus;
    }
  }

  for (int i = 0; i < n; i++) {

    if (s[i] == '0') {
      int temp = vp;
      temp += mod2[1][n - 1 - i];
      temp %= plus;
      int ans = subsolve(temp, 1);
      cout << ans << endl;

    } else {
      int temp = vm;
      temp -= mod2[0][n - 1 - i];
      if (minus == 0) {
        cout << 0 << endl;
        continue;
      }
      temp %= minus;
      temp = (temp + minus) % minus;
      int ans = subsolve(temp, 1);
      cout << ans << endl;
    }
  }
}

int main() {
  int T;
  T = 1;
  // cin >> T;
  while (T--) {
    solve();
    cout << endl;
  }
}


Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>

// #include <bits/stdc++.h>
#define maxs(x, y) x = max(x, y)
#define mins(x, y) x = min(x, y)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, i0, n) for (int(i) = (i0); (i) < (n); (i)++)
#define FORR(i, i0, n) for (int(i) = (n)-1; (i) >= (i0); (i)--)
#define SORT(x) sort(x.begin(), x.end())
#define SORTR(x) sort(x.begin(), x.end(), greater<int>())
#define rn return
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple

using namespace std;
using ll = long long;
typedef std::pair<int, int> pii;
typedef std::vector<int> vi;
typedef std::vector<pii> vii;

#define PI 3.14159265358979323846264338327950L
const int mod = 1000000007;

void bin(int n) {
  string ans;
  while (n) {
    ans = to_string(n % 2) + ans;
    n /= 2;
  }
  cout << ans << endl;
}

int mod2[2][200001];

void init(int minus, int plus) {
  int vm = 1;
  int vp = 1;

  mod2[0][0] = 1 % minus;
  mod2[1][0] = 1 % plus;

  for (int i = 1; i < 200001; i++) {
    vm *= 2;
    vp *= 2;
    vm %= minus;
    vp %= plus;
    mod2[0][i] = vm;
    mod2[1][i] = vp;
  }
}

int subsolve(int n, int c) {
  while (n) {
    n %= __builtin_popcount(n);
    c++;
  }
  return c;
}
void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int ones = 0;
  for (auto e : s) {
    if (e == '1')
      ones++;
  }

  int plus = ones + 1;
  int minus = ones - 1;

  init(minus, plus);

  int vm = 0;
  int vp = 0;

  for (int i = 0; i < n; i++) {
    if (s[n - 1 - i] == '1') {
      vm += mod2[0][i];
      vp += mod2[1][i];
      vm %= minus;
      vp %= plus;
    }
  }

  for (int i = 0; i < n; i++) {

    if (s[i] == '0') {
      int temp = vp;
      temp += mod2[1][n - 1 - i];
      temp %= plus;
      int ans = subsolve(temp, 1);
      cout << ans << endl;

    } else {
      int temp = vm;
      temp -= mod2[0][n - 1 - i];
      temp %= minus;
      temp = (temp + minus) % minus;
      int ans = subsolve(temp, 1);
      cout << ans << endl;
    }
  }
}

int main() {
  int T;
  T = 1;
  // cin >> T;
  while (T--) {
    solve();
    cout << endl;
  }
}