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

// Created by Kira

#include <bits/stdc++.h>
using namespace std;

#define IOS                                                                    \\
  cin.sync_with_stdio(0);                                                      \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define pii pair<int, int>
#define pb push_back
#define vi vector<int>
using ll = long long;

int mod = 1e9 + 7;

ll pwr(ll a, ll b);

int main() {
  IOS

      int n;
  cin >> n;
  string s;
  cin >> s;

  // preprocessing
  vector<int> f(n + 1);
  f[0] = 0;

  for (int i = 1; i <= n; i++) {
    int val = i;
    int b = __builtin_popcount(val);
    val %= b;
    f[i] = 1 + f[val];
  }

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

  mod = cnt + 1;
  int val1 = 0, val2 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      val1 = (val1 * 2) % mod;
    } else {
      val1 = (2 * val1 + 1) % mod;
    }
  }

  mod = cnt - 1;
  if (cnt > 1) {
    for (int i = 0; i < n; i++) {
      if (s[i] == '0') {
        val2 = (val2 * 2) % mod;
      } else {
        val2 = (2 * val2 + 1) % mod;
      }
    }
  }
  int ans = -1;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      // add 2 ^ i
      mod = cnt + 1;
      ans = (val1 + pwr(2ll, n - i - 1) % mod) % mod;
    } else {
      mod = cnt - 1;
      if (cnt == 1) {
        cout << 0 << "\\n";
        continue;
      }
      ans = (val2 - pwr(2ll, n - i - 1) % mod + mod) % mod;
    }
    cout << 1 + f[ans] << "\\n";
  }
}

ll pwr(ll a, ll b) {
  a %= mod; // Remove mod if not required
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }

  return res;
}


Error: 0

Fixed submission:

// Created by Kira

#include <bits/stdc++.h>
using namespace std;

#define IOS                                                                    \\
  cin.sync_with_stdio(0);                                                      \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define pii pair<int, int>
#define pb push_back
#define vi vector<int>
using ll = long long;

int mod = 1e9 + 7;

ll pwr(ll a, ll b);

int main() {
  IOS

      int n;
  cin >> n;
  string s;
  cin >> s;

  // preprocessing
  vector<int> f(n + 1);
  f[0] = 0;

  for (int i = 1; i <= n; i++) {
    int val = i;
    int b = __builtin_popcount(val);
    val %= b;
    f[i] = 1 + f[val];
  }

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

  mod = cnt + 1;
  int val1 = 0, val2 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      val1 = (val1 * 2) % mod;
    } else {
      val1 = (2 * val1 + 1) % mod;
    }
  }

  mod = cnt - 1;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      val2 = (val2 * 2) % mod;
    } else {
      val2 = (2 * val2 + 1) % mod;
    }
  }
  int ans = -1;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      // add 2 ^ i
      mod = cnt + 1;
      ans = (val1 + pwr(2ll, n - i - 1) % mod) % mod;
    } else {
      mod = cnt - 1;
      ans = (val2 - pwr(2ll, n - i - 1) % mod + mod) % mod;
    }
    cout << 1 + f[ans] << "\\n";
  }
}

ll pwr(ll a, ll b) {
  a %= mod; // Remove mod if not required
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }

  return res;
}