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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define ff first
#define ss second
#define boost                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr)
#include "string"

int mod(int m, int mod) {
  m %= mod;
  return (m + mod) % mod;
}

int power(int x, int y, int md) {
  int res = 1;
  x = mod(x, md);
  while (y > 0) {
    if (y & 1)
      res = mod(res * x, md);
    y = y / 2;
    x = mod(x * x, md);
  }
  return mod(res, md);
}

int curnum(vector<int> &v, int md) {
  int ans = 0;
  for (int &i : v)
    ans = mod(ans + power(2, i, md), md);
  return ans;
}

int32_t main() {
  boost;
  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt = 0;

  /*int tmp = 0;
  for(int i= n- 1;i >= 0; i--) {
          if(s[i] == '1')
                  tmp |= (1 << (n - i - 1));
  }*/

  vector<int> v;

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

  if (cnt == 0) {
    for (int i = 1; i <= n; i++) {
      cout << 1 << endl;
    }
    return 0;
  }

  int c1 = 0, c2 = 0;
  for (int &i : v) {
    c1 = mod(c1 + power(2, i, cnt + 1), cnt + 1);
    if (cnt > 1)
      c2 = mod(c2 + power(2, i, cnt - 1), cnt - 1);
  }

  for (int i = 0; i < n; i++) {
    int tt;
    if (s[i] == '1') {
      cnt--;
      if (cnt == 0) {
        cnt++;
        cout << 0 << endl;
        continue;
      }
      tt = mod(c2 - power(2, n - i - 1, cnt), cnt);
      cnt++;
    } else {
      cnt++;
      tt = mod(c1 + power(2, n - i - 1, cnt), cnt);
      cnt--;
    }

    int ans = 1;
    while (tt > 0) {
      int tmp = __builtin_popcount(tt);
      tt %= tmp;
      ans++;
    }
    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define ff first
#define ss second
#define boost                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr)
#include "string"

int mod(int m, int mod) {
  m %= mod;
  return (m + mod) % mod;
}

int power(int x, int y, int md) {
  int res = 1;
  x = mod(x, md);
  while (y > 0) {
    if (y & 1)
      res = mod(res * x, md);
    y = y / 2;
    x = mod(x * x, md);
  }
  return mod(res, md);
}

int curnum(vector<int> &v, int md) {
  int ans = 0;
  for (int &i : v)
    ans = mod(ans + power(2, i, md), md);
  return ans;
}

int32_t main() {
  boost;
  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt = 0;

  /*int tmp = 0;
  for(int i= n- 1;i >= 0; i--) {
          if(s[i] == '1')
                  tmp |= (1 << (n - i - 1));
  }*/

  vector<int> v;

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

  if (cnt == 0) {
    for (int i = 1; i <= n; i++) {
      cout << 1 << endl;
    }
    return 0;
  }

  int c1 = 0, c2 = 0;
  for (int &i : v) {
    c1 = mod(c1 + power(2, i, cnt + 1), cnt + 1);
    c2 = mod(c2 + power(2, i, cnt - 1), cnt - 1);
  }

  for (int i = 0; i < n; i++) {
    int tt;
    if (s[i] == '1') {
      cnt--;
      if (cnt == 0) {
        cnt++;
        cout << 0 << endl;
        continue;
      }
      tt = mod(c1 - power(2, n - i - 1, cnt), cnt);
      cnt++;
    } else {
      cnt++;
      tt = mod(c2 + power(2, n - i - 1, cnt), cnt);
      cnt--;
    }

    int ans = 1;
    while (tt > 0) {
      int tmp = __builtin_popcount(tt);
      tt %= tmp;
      ans++;
    }
    cout << ans << endl;
  }
}