Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;
using ll = long long int;
const ll MOD = 1e9 + 5;

ll mod_pow(ll k, ll p) {
  if (k == 0) {
    return 1 % p;
  }
  if (k == 1) {
    return 2 % p;
  }

  if (k % 2 == 0) {
    ll tmp = mod_pow(k / 2, p);
    return tmp * tmp % p;
  } else {
    ll tmp = mod_pow((k - 1) / 2, p);
    tmp *= tmp;
    tmp %= p;
    tmp *= 2;
    tmp %= p;
    return tmp;
  }
}

ll pow_fast(ll n, ll k) {
  if (k == 0) {
    return 1;
  }
  if (k == 1) {
    return n;
  }

  if (k % 2 == 0) {
    ll tmp = pow_fast(n, k / 2) % MOD;
    return tmp * tmp % MOD;
  } else {
    ll tmp = pow_fast(n, (k - 1) / 2);
    return tmp * tmp * 2;
  }
}

int main() {
  vector<ll> memo(1e5 + 1, 0);
  for (ll i = 1; i <= 1e6; i++) {
    bitset<64> bits(i);
    memo[i] += memo[i % bits.count()] + 1;
  }

  ll N;
  cin >> N;
  string S;
  cin >> S;
  vector<ll> X(N);
  ll popcount = 0;
  for (size_t i = 0; i < N; i++) {
    X[i] = S[i] - '0';
    if (X[i] == 1) {
      popcount += 1;
    }
  }

  vector<ll> UP(N, 0);
  vector<ll> DOWN(N, 0);

  for (ll i = 0; i < N; i++) {
    if (i == 0) {
      UP[i] = 1 % (popcount + 1);
      continue;
    }
    UP[i] = UP[i - 1] * 2 % (popcount + 1);
  }
  for (ll i = 0; i < N; i++) {
    if (popcount - 1 == 0) {
      break;
    }
    if (i == 0) {
      DOWN[i] = 1 % (popcount - 1);
      continue;
    }
    DOWN[i] = DOWN[i - 1] * 2 % (popcount - 1);
  }

  ll up = 0;
  ll down = 0;
  for (ll i = 0; i < N; i++) {
    if (X[i] == 0) {
      continue;
    }
    up += UP[N - 1 - i];
    up %= (popcount + 1);
  }
  for (ll i = 0; i < N; i++) {
    if (popcount - 1 == 0) {
      break;
    }
    if (X[i] == 0) {
      continue;
    }
    down += DOWN[N - 1 - i];
    down %= (popcount - 1);
  }

  ll cnt = 0;
  for (ll i = 0; i < N; i++) {
    if (X[i] == 1) {
      ll f = popcount - 1;
      if (f == 0) {
        cout << 0 << endl;
        continue;
      }

      ll n = (down - DOWN[N - 1 - i] + f) % f;
      cout << (memo[n] + 1) << endl;
    } else {
      ll f = popcount + 1;
      ll n = (up + UP[N - 1 - i] + f) % f;

      cout << (memo[n] + 1) << endl;
    }
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;
using ll = long long int;
const ll MOD = 1e9 + 5;

ll mod_pow(ll k, ll p) {
  if (k == 0) {
    return 1 % p;
  }
  if (k == 1) {
    return 2 % p;
  }

  if (k % 2 == 0) {
    ll tmp = mod_pow(k / 2, p);
    return tmp * tmp % p;
  } else {
    ll tmp = mod_pow((k - 1) / 2, p);
    tmp *= tmp;
    tmp %= p;
    tmp *= 2;
    tmp %= p;
    return tmp;
  }
}

ll pow_fast(ll n, ll k) {
  if (k == 0) {
    return 1;
  }
  if (k == 1) {
    return n;
  }

  if (k % 2 == 0) {
    ll tmp = pow_fast(n, k / 2) % MOD;
    return tmp * tmp % MOD;
  } else {
    ll tmp = pow_fast(n, (k - 1) / 2);
    return tmp * tmp * 2;
  }
}

int main() {
  vector<ll> memo(1e6 + 1, 0);
  for (ll i = 1; i <= 1e6; i++) {
    bitset<64> bits(i);
    memo[i] += memo[i % bits.count()] + 1;
  }

  ll N;
  cin >> N;
  string S;
  cin >> S;
  vector<ll> X(N);
  ll popcount = 0;
  for (size_t i = 0; i < N; i++) {
    X[i] = S[i] - '0';
    if (X[i] == 1) {
      popcount += 1;
    }
  }

  vector<ll> UP(N, 0);
  vector<ll> DOWN(N, 0);

  for (ll i = 0; i < N; i++) {
    if (i == 0) {
      UP[i] = 1 % (popcount + 1);
      continue;
    }
    UP[i] = UP[i - 1] * 2 % (popcount + 1);
  }
  for (ll i = 0; i < N; i++) {
    if (popcount - 1 == 0) {
      break;
    }
    if (i == 0) {
      DOWN[i] = 1 % (popcount - 1);
      continue;
    }
    DOWN[i] = DOWN[i - 1] * 2 % (popcount - 1);
  }

  ll up = 0;
  ll down = 0;
  for (ll i = 0; i < N; i++) {
    if (X[i] == 0) {
      continue;
    }
    up += UP[N - 1 - i];
    up %= (popcount + 1);
  }
  for (ll i = 0; i < N; i++) {
    if (popcount - 1 == 0) {
      break;
    }
    if (X[i] == 0) {
      continue;
    }
    down += DOWN[N - 1 - i];
    down %= (popcount - 1);
  }

  ll cnt = 0;
  for (ll i = 0; i < N; i++) {
    if (X[i] == 1) {
      ll f = popcount - 1;
      if (f == 0) {
        cout << 0 << endl;
        continue;
      }

      ll n = (down - DOWN[N - 1 - i] + f) % f;
      cout << (memo[n] + 1) << endl;
    } else {
      ll f = popcount + 1;
      ll n = (up + UP[N - 1 - i] + f) % f;

      cout << (memo[n] + 1) << endl;
    }
  }
}