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

#include <bits/stdc++.h>
#include <functional>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define rep1(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = a; i >= n; i--)
#define per1(i, a, n) for (int i = a; i > n; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define endl "\\n"
#define pi 3.14159265358979323846264338327950288419716939937510
#define IOS                                                                    \\
  cin.sync_with_stdio(false);                                                  \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll mod = 1000000007;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll qpow(ll a, ll b) {
  ll s = 1;
  while (b > 0) {
    if (b % 2 == 1) {
      s = s * a;
    }
    a = a * a;
    b = b >> 1;
  }
  return s;
}
ll qpowmod(ll a, ll b, ll c) {
  ll res, t;
  res = 1;
  t = a % c;
  while (b) {
    if (b & 1) {
      res = res * t % c;
    }
    t = t * t % c;
    b >>= 1;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
inline long long read() {
  long long k = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    k = k * 10 + ch - '0';
    ch = getchar();
  }
  return k * f;
}
inline void write(long long x) {
  if (x < 0)
    x = -x, putchar('-');
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
// head
ll n, cnt, sum1, sum2, res;
string s;
bitset<105> bt;
int f(ll res) {
  if (res == 0) {
    return 1;
  }
  ll ans = 1;
  bt = res;
  ans += f(res % (bt.count()));
  return ans;
}
int main() {
  // IOS;
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);
  cin >> n >> s;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1')
      cnt++;
  }
  for (int i = 0; i <= n - 1; i++) {
    if (s[i] == '1') {
      if (cnt != 1)
        sum1 += qpowmod(2, n - i - 1, cnt - 1);
      sum2 += qpowmod(2, n - i - 1, cnt + 1);
    }
  }
  for (int i = 0; i <= n - 1; i++) {
    if (s[i] == '1') {
      if (cnt == 1) {
        cout << 0 << endl;
        continue;
      }
      res = sum1;
      res = (((res - qpowmod(2, n - i - 1, cnt - 1)) % (cnt - 1)) + (cnt - 1));
      res %= (cnt - 1);
    } else {
      res = sum2;
      res += qpowmod(2, n - i - 1, cnt + 1);
      res %= (cnt + 1);
    }
    cout << f(res) << endl;
  }
  return 0;
}
/*

*/


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <functional>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define rep1(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = a; i >= n; i--)
#define per1(i, a, n) for (int i = a; i > n; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define endl "\\n"
#define pi 3.14159265358979323846264338327950288419716939937510
#define IOS                                                                    \\
  cin.sync_with_stdio(false);                                                  \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll mod = 1000000007;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll qpow(ll a, ll b) {
  ll s = 1;
  while (b > 0) {
    if (b % 2 == 1) {
      s = s * a;
    }
    a = a * a;
    b = b >> 1;
  }
  return s;
}
ll qpowmod(ll a, ll b, ll c) {
  ll res, t;
  res = 1;
  t = a % c;
  while (b) {
    if (b & 1) {
      res = res * t % c;
    }
    t = t * t % c;
    b >>= 1;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
inline long long read() {
  long long k = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    k = k * 10 + ch - '0';
    ch = getchar();
  }
  return k * f;
}
inline void write(long long x) {
  if (x < 0)
    x = -x, putchar('-');
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
// head
ll n, cnt, sum1, sum2, res;
string s;
bitset<105> bt;
int f(ll res) {
  if (res < 0) {
    return 1;
  }
  if (res == 0) {
    return 1;
  }
  ll ans = 1;
  bt = res;
  ans += f(res % (bt.count()));
  return ans;
}
int main() {
  // IOS;
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);
  cin >> n >> s;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1')
      cnt++;
  }
  for (int i = 0; i <= n - 1; i++) {
    if (s[i] == '1') {
      sum1 += qpowmod(2, n - i - 1, cnt - 1);
      sum2 += qpowmod(2, n - i - 1, cnt + 1);
    }
  }
  for (int i = 0; i <= n - 1; i++) {
    if (s[i] == '1') {
      res = sum1;
      res = (res + (cnt - 1) - qpowmod(2, n - i - 1, cnt - 1));
      res %= (cnt - 1);
      cout << f(res) << endl;
    } else {
      res = sum2;
      res += qpowmod(2, n - i - 1, cnt + 1);
      res %= (cnt + 1);
      cout << f(res) << endl;
    }
  }
  return 0;
}
/*

*/