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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> P;
const ll MOD = 1000000007;
const ll MAX_N = 500010;
const ll INF = 999999999999;
vector<ll> memo(1000000, INF);

ll solve(ll n) {
  if (n == 0) {
    return 1;
  }
  if (memo[n] != INF) {
    return memo[n];
  }
  ll bus = __builtin_popcount(n);
  ll a = n % bus;
  memo[n] = solve(a) + 1;
  return memo[n];
}

ll modpow(ll a, ll n, ll mod) {
  ll ret = 1;
  while (n > 0) {
    if (n & 1)
      ret = ret * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return ret;
}

int main() {
  memo[0] = 1;
  ll n;
  cin >> n;
  string x;
  cin >> x;
  ll s, m, l;
  m = 0;
  for (ll i = 0; i < n; i++) {
    if (x[i] == '1')
      m++;
  }
  s = m - 1;
  l = m + 1;
  ll modm = 0;
  ll modl = 0;
  ll mods = 0;
  if (s == 0) {
    for (ll i = n - 1; i >= 0; i--) {
      if (x[n - 1 - i] == '1') {
        modm += modpow(2, i, m);
        modm %= m;
        modl += modpow(2, i, l);
        modl %= l;
      }
    }
    vector<ll> ans(n);
    for (ll i = n - 1; i >= 0; i--) {
      if (x[n - 1 - i] == '0') {
        ll buf = modl;
        buf += modpow(2, i, l);
        buf %= l;
        // cout<<"..."<<buf<<endl;
        ans[n - 1 - i] = solve(buf);
      } else {
        ans[n - 1 - i] = 0;
      }
    }
    for (ll i = 0; i < n; i++)
      cout << ans[i] << endl;
    return 0;
  }

  // cout<<mods<<endl;
  for (ll i = n - 1; i >= 0; i--) {
    if (x[n - 1 - i] == '1') {
      modm += modpow(2, i, m);
      modm %= m;
      modl += modpow(2, i, l);
      modl %= l;
      mods += modpow(2, i, s);
      mods %= s;
    }
  }
  vector<ll> ans(n);
  for (ll i = n - 1; i >= 0; i--) {
    if (x[n - 1 - i] == '0') {
      ll buf = modl;
      buf += modpow(2, i, l);
      buf %= l;
      // cout<<"..."<<buf<<endl;
      ans[n - 1 - i] = solve(buf);
    } else {
      ll buf = mods;
      buf -= modpow(2, i, s);
      buf %= s;
      if (buf < 0) {
        buf += s;
      }
      // cout<<"..."<<buf<<endl;
      ans[n - 1 - i] = solve(buf);
    }
  }
  for (ll i = 0; i < n; i++)
    cout << ans[i] << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> P;
const ll MOD = 1000000007;
const ll MAX_N = 500010;
const ll INF = 999999999999;
vector<ll> memo(1000000, INF);

ll solve(ll n) {
  if (n == 0) {
    return 1;
  }
  if (memo[n] != INF) {
    return memo[n];
  }
  ll bus = __builtin_popcount(n);
  ll a = n % bus;
  memo[n] = solve(a) + 1;
  return memo[n];
}

ll modpow(ll a, ll n, ll mod) {
  if (mod == 0) {
    return 0;
  }
  ll ret = 1;
  while (n > 0) {
    if (n & 1)
      ret = ret * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return ret;
}

int main() {
  memo[0] = 1;
  ll n;
  cin >> n;
  string x;
  cin >> x;
  ll s, m, l;
  m = 0;
  for (ll i = 0; i < n; i++) {
    if (x[i] == '1')
      m++;
  }
  s = m - 1;
  l = m + 1;
  ll modm = 0;
  ll modl = 0;
  ll mods = 0;
  // cout<<mods<<endl;
  for (ll i = n - 1; i >= 0; i--) {
    if (x[n - 1 - i] == '1') {
      modm += modpow(2, i, m);
      modm %= m;
      modl += modpow(2, i, l);
      modl %= l;
      mods += modpow(2, i, s);
      mods %= s;
    }
  }
  vector<ll> ans(n);
  for (ll i = n - 1; i >= 0; i--) {
    if (x[n - 1 - i] == '0') {
      ll buf = modl;
      buf += modpow(2, i, l);
      buf %= l;
      // cout<<"..."<<buf<<endl;
      ans[n - 1 - i] = solve(buf);
    } else {
      ll buf = mods;
      buf -= modpow(2, i, s);
      buf %= s;
      if (buf < 0) {
        buf += s;
      }
      // cout<<"..."<<buf<<endl;
      ans[n - 1 - i] = solve(buf);
    }
  }
  for (ll i = 0; i < n; i++)
    cout << ans[i] << endl;
}