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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
// const long long MOD=1e9+7;
ll mod_pow(ll x, ll y, ll MOD) { // x^yを返す//計算量は0(log(y))
  ll tmp = x, res = 1;
  while (y) {
    if (y % 2) {
      res = res * tmp % MOD;
    }
    y >>= 1;
    tmp = tmp * tmp % MOD;
  }
  return res;
}
int main() {
  ll N, count = 0;
  cin >> N;
  string X;
  cin >> X;
  for (ll i = 0; i < N; i++) {
    if (X.at(i) == '1') {
      count++;
    }
  }
  ll summ = 0;
  ll ing = 1;
  ll M = count + 1;
  ll m = count - 1;
  for (ll i = 0; i < N; i++) {
    if (m == 0) {
      break;
    }
    if (X.at(N - i - 1) == '1') {
      summ += ing;
      summ %= m;
      // cout<<i<<" "<<ing<<endl;
    }
    ing *= 2;
    ing %= m;
  }

  ll sumM = 0;
  ing = 1;
  for (ll i = 0; i < N; i++) {
    if (X.at(N - i - 1) == '1') {
      sumM += ing;
      sumM %= M;
      // cout<<i<<" "<<ing<<endl;
    }
    ing *= 2;
    ing %= M;
  }
  ll rock;
  // cout<<summ<<endl;
  // cout<<m<<" "<<summ<<"    "<<M<<" "<<sumM<<endl;
  // map<ll,ll> mp;
  // map<ll,ll> cal;
  for (ll i = 0; i < N; i++) {
    // m
    if (X.at(i) == '0') {
      rock = mod_pow(2, N - i - 1, M);
      rock += sumM;
      rock %= M;
      // cout<<i<<" "<<rock<<endl;
    } else {
      if (m == 0) {
        cout << 0 << endl;
        continue;
      }
      rock = -1 * mod_pow(2, N - i - 1, m);
      rock += summ;
      // cout<<rock<<endl;
      rock %= m;
      rock += m;
      rock %= m;
      // cout<<i<<" "<<rock<<endl;
    }
    // mp[i]=1
    // cal[i]=rock;
    // cout<<" "<<i<<endl;
    if (rock == 0) {
      cout << 1 << endl;
    } else {
      ll start = rock;
      ll ans = 1;
      while (start > 0) {
        ll memo = start;
        ll div = 2;
        ll c = 0;
        while (start > 0) {
          if (start % div == 0) {
            start /= div;
          } else {
            c++;
            start -= (start % div);
            start /= div;
          }
        }
        ans++;
        if (c == 0) {
          break;
        }
        start = memo % c;
      }
      cout << ans << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
// const long long MOD=1e9+7;
ll mod_pow(ll x, ll y, ll MOD) { // x^yを返す//計算量は0(log(y))
  ll tmp = x, res = 1;
  while (y) {
    if (y % 2) {
      res = res * tmp % MOD;
    }
    y >>= 1;
    tmp = tmp * tmp % MOD;
  }
  return res;
}
int main() {
  ll N, count = 0;
  cin >> N;
  string X;
  cin >> X;
  for (ll i = 0; i < N; i++) {
    if (X.at(i) == '1') {
      count++;
    }
  }
  ll summ = 0;
  ll ing = 1;
  ll M = count + 1;
  ll m = count - 1;
  for (ll i = 0; i < N; i++) {
    if (X.at(N - i - 1) == '1') {
      summ += ing;
      summ %= m;
      // cout<<i<<" "<<ing<<endl;
    }
    ing *= 2;
    ing %= m;
  }

  ll sumM = 0;
  ing = 1;
  for (ll i = 0; i < N; i++) {
    if (X.at(N - i - 1) == '1') {
      sumM += ing;
      sumM %= M;
      // cout<<i<<" "<<ing<<endl;
    }
    ing *= 2;
    ing %= M;
  }
  ll rock;
  // cout<<summ<<endl;
  // cout<<m<<" "<<summ<<"    "<<M<<" "<<sumM<<endl;
  // map<ll,ll> mp;
  // map<ll,ll> cal;
  for (ll i = 0; i < N; i++) {
    // m
    if (X.at(i) == '0') {
      rock = mod_pow(2, N - i - 1, M);
      rock += sumM;
      rock %= M;
      // cout<<i<<" "<<rock<<endl;
    } else {
      if (m == 0) {
        cout << 1 << endl;
        continue;
      }
      rock = -1 * mod_pow(2, N - i - 1, m);
      rock += summ;
      // cout<<rock<<endl;
      rock %= m;
      rock += m;
      rock %= m;
      // cout<<i<<" "<<rock<<endl;
    }
    // mp[i]=1
    // cal[i]=rock;

    if (rock == 0) {
      cout << 1 << endl;
    } else {
      ll start = rock;
      ll ans = 1;
      while (start > 0) {
        ll memo = start;
        ll div = 2;
        ll c = 0;
        while (start > 0) {
          if (start % div == 0) {
            start /= div;
          } else {
            c++;
            start -= (start % div);
            start /= div;
          }
        }
        ans++;
        if (c == 0) {
          break;
        }
        start = memo % c;
      }
      cout << ans << endl;
    }
  }
}