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

/*
AuThOr Gwj
*/
#include <bits/stdc++.h>
#define rb(a, b, c) for (int a = b; a <= c; ++a)
#define rl(a, b, c) for (int a = b; a >= c; --a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a, b) make_pair(a, b)
#define FIR first
#define SEC second
#define FREO freopen("check.out", "w", stdout)
#define rep(a, b) for (int a = 0; a < b; ++a)
#define KEEP while (1)
#define SRAND                                                                  \\
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng() % a
#define ALL(a) a.begin(), a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define debug_pair(A) cerr << A.FIR << " " << A.SEC << endl;
#define biggest(A, B) A = max(A, B)
#define smallest(A, B) A = min(A, B)
using namespace std;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> mp;
typedef pair<mp, mp> superpair;
int f[200000 + 20];
int b(int x) {
  int cnt = 0;
  while (x) {
    if (x & 1)
      cnt++;
    x >>= 1;
  }
  return cnt;
}
string s;
int tmp = 0;
int l1, l2, l3;
int MOD;
LL quick(int A, int B) {
  LL res;
  if (B == 0)
    return 1;
  res = quick(A, B >> 1);
  res *= res;
  res %= MOD;
  if (B & 1) {
    res *= (LL)A;
    res %= MOD;
  }
  return res;
}
int main() {
  fastio;
  //	freopen("tt.in","r",stdin);
  f[0] = 0;
  rb(i, 1, 200000 + 1) { f[i] = f[i % b(i)] + 1; }
  int n;
  cin >> n >> s;
  rep(i, n) {
    if (s[i] == '1') {
      tmp++;
    }
  }
  if (tmp == 0) {
    rb(i, 1, n) cout << 1 << endl;
    return 0;
  }
  l1 = l2 = l3 = 0;
  rep(i, n) {
    if (tmp != 1)
      l1 <<= 1;
    if (tmp)
      l2 <<= 1;
    l3 <<= 1;
    if (s[i] == '1') {
      if (tmp != 1)
        l1++;
      if (tmp)
        l2++;
      l3++;
    }
    if (tmp != 1)
      l1 %= tmp - 1;
    if (tmp)
      l2 %= tmp;
    l3 %= tmp + 1;
  }
  rep(i, n) {
    int res;
    if (s[i] == '1') {
      //->0
      if (tmp != 1) {
        MOD = tmp - 1;
        res = l1;
        res -= quick(2, n - 1 - i);
        res += MOD;
        res %= MOD;
        res = f[res] + 1;

      } else {
        res = 0;
      }
    } else {
      MOD = tmp + 1;
      res = l3;
      res += quick(2, n - 1 - i);
      res %= MOD;
      res = f[res] + 1;
    }
    cout << res << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

/*
AuThOr Gwj
*/
#include <bits/stdc++.h>
#define rb(a, b, c) for (int a = b; a <= c; ++a)
#define rl(a, b, c) for (int a = b; a >= c; --a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a, b) make_pair(a, b)
#define FIR first
#define SEC second
#define FREO freopen("check.out", "w", stdout)
#define rep(a, b) for (int a = 0; a < b; ++a)
#define KEEP while (1)
#define SRAND                                                                  \\
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng() % a
#define ALL(a) a.begin(), a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define debug_pair(A) cerr << A.FIR << " " << A.SEC << endl;
#define biggest(A, B) A = max(A, B)
#define smallest(A, B) A = min(A, B)
using namespace std;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> mp;
typedef pair<mp, mp> superpair;
int f[200000 + 20];
int b(int x) {
  int cnt = 0;
  while (x) {
    if (x & 1)
      cnt++;
    x >>= 1;
  }
  return cnt;
}
string s;
int tmp = 0;
int l1, l2, l3;
int MOD;
LL quick(int A, int B) {
  LL res;
  if (B == 0)
    return 1;
  res = quick(A, B >> 1);
  res *= res;
  res %= MOD;
  if (B & 1) {
    res *= (LL)A;
    res %= MOD;
  }
  return res;
}
int main() {
  fastio;
  //	freopen("tt.in","r",stdin);
  f[0] = 0;
  rb(i, 1, 200000 + 1) { f[i] = f[i % b(i)] + 1; }
  int n;
  cin >> n >> s;
  rep(i, n) {
    if (s[i] == '1') {
      tmp++;
    }
  }
  l1 = l2 = l3 = 0;
  rep(i, n) {
    if (tmp != 1)
      l1 <<= 1;
    l2 <<= 1;
    l3 <<= 1;
    if (s[i] == '1') {
      if (tmp != 1)
        l1++;
      l2++;
      l3++;
    }
    if (tmp != 1)
      l1 %= tmp - 1;
    l2 %= tmp;
    l3 %= tmp + 1;
  }
  rep(i, n) {
    int res;
    if (s[i] == '1') {
      //->0
      if (tmp != 1) {

        MOD = tmp - 1;
        res = l1;
        res -= quick(2, n - 1 - i);
        res += MOD;
        res %= MOD;
        res = f[res] + 1;

      } else {
        res = 0;
      }
    } else {
      MOD = tmp + 1;
      res = l3;
      res += quick(2, n - 1 - i);
      res %= MOD;
      res = f[res] + 1;
    }
    cout << res << endl;
  }
  return 0;
}