Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef long long int ll;
typedef unsigned long long int sll;
typedef long double ld;
#define A 1000000007
#define D 100000000000000ll
#define B 998244353ll
#define C 1000000000000000000ll
#define M 4000
#define FAST                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define oset                                                                   \
  tree<pair<ll, ll>, null_type, less_equal<pair<ll, ll>>, rb_tree_tag,         \
       tree_order_statistics_node_update>
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define mxe(v) *max_element(v.begin(), v.end())
#define mne(v) *min_element(v.begin(), v.end())
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define ve vector
#define br break
#define PI acos(-1)
#define subt                                                                   \
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC       \
       << "ms\n"

ll dp[200005];

ll pc(ll x) {
  ll co = 0;
  while (x > 0) {
    if (x & 1)
      co++;
    x = x / 2;
  }
  return co;
}

int main() {
  FAST;
  for (int i = 1; i < 200005; i++)
    dp[i] = dp[i % pc(i)] + 1;
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll c = 0;
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      c++;
  if (c == 0) {
    for (int i = 0; i < n; i++)
      cout << 1 << '\n';
    return 0;
  }
  ll x = 0, y = 0, z = 0;
  ll te1[n], te2[n], te3[n];
  te1[n - 1] = 1;
  te2[n - 1] = 1;
  te3[n - 1] = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      x = (x + te1[i]) % (c - 1);
      y = (y + te2[i]) % c;
      z = (z + te3[i]) % (c + 1);
    }
    if (i != 0) {
      te1[i - 1] = (te1[i] * 2ll) % (c - 1);
      te2[i - 1] = (te2[i] * 2ll) % (c);
      te3[i - 1] = (te3[i] * 2ll) % (c + 1);
    }
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      ll a = (x - te1[i] + c - 1) % (c - 1);
      cout << dp[a] + 1 << '\n';
    } else {
      ll a = (z + te3[i]) % (c + 1);
      cout << dp[a] + 1 << '\n';
    }
  }
  subt;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef long long int ll;
typedef unsigned long long int sll;
typedef long double ld;
#define A 1000000007
#define D 100000000000000ll
#define B 998244353ll
#define C 1000000000000000000ll
#define M 4000
#define FAST                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define oset                                                                   \
  tree<pair<ll, ll>, null_type, less_equal<pair<ll, ll>>, rb_tree_tag,         \
       tree_order_statistics_node_update>
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define mxe(v) *max_element(v.begin(), v.end())
#define mne(v) *min_element(v.begin(), v.end())
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define ve vector
#define br break
#define PI acos(-1)
#define subt                                                                   \
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC       \
       << "ms\n"

ll dp[200005];

ll pc(ll x) {
  ll co = 0;
  while (x > 0) {
    if (x & 1)
      co++;
    x = x / 2;
  }
  return co;
}

int main() {
  FAST;
  for (int i = 1; i < 200005; i++)
    dp[i] = dp[i % pc(i)] + 1;
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll c = 0;
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      c++;
  if (c == 0) {
    for (int i = 0; i < n; i++)
      cout << 1 << '\n';
    return 0;
  }
  if (c == 1) {
    if (s[n - 1] == '1') {
      for (int i = 0; i < n; i++) {
        if (s[i] == '1')
          cout << 0 << "\n";
        else
          cout << 2 << "\n";
      }
    } else {
      for (int i = 0; i < n; i++) {
        if (s[i] != '1') {
          if (i != n - 1)
            cout << 1 << '\n';
          else
            cout << 2 << '\n';
        } else
          cout << 0 << '\n';
      }
    }
    return 0;
  }
  ll x = 0, y = 0, z = 0;
  ll te1[n], te2[n], te3[n];
  te1[n - 1] = 1;
  te2[n - 1] = 1;
  te3[n - 1] = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      x = (x + te1[i]) % (c - 1);
      y = (y + te2[i]) % c;
      z = (z + te3[i]) % (c + 1);
    }
    if (i != 0) {
      te1[i - 1] = (te1[i] * 2ll) % (c - 1);
      te2[i - 1] = (te2[i] * 2ll) % (c);
      te3[i - 1] = (te3[i] * 2ll) % (c + 1);
    }
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      ll a = (x - te1[i] + c - 1) % (c - 1);
      cout << dp[a] + 1 << '\n';
    } else {
      ll a = (z + te3[i]) % (c + 1);
      cout << dp[a] + 1 << '\n';
    }
  }
  subt;
  return 0;
}