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

#include <iostream>
#define ll long long int
using namespace std;
ll dp[200005];
ll func(ll cnt, ll mv) {
  if (cnt == 0) {
    return mv;
  }
  ll temp = dp[cnt];
  cnt = cnt % temp;
  return func(cnt, mv + 1);
}
ll fastexp(ll a, ll b, ll m) {
  ll ans = 1;
  while (b) {
    if (b % 2 == 1) {
      ans = (ans * a) % m;
    }

    a = (a * a) % m;
    b = b / 2;
  }
  return ans;
}
int main() {
  ll n;
  string s;
  cin >> n;

  cin >> s;

  for (int i = 0; i <= 200004; i++) {
    ll cnt = 0;
    ll tmp = i;
    while (tmp) {
      if (tmp % 2 == 1)
        cnt++;

      tmp /= 2;
    }

    dp[i] = cnt;
  }

  ll ones = 0;
  ll start[200005];
  ll ans1 = 0, ans2 = 0;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '1') {
      ones++;
    }
  }

  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '1') {
      ans1 = (ans1 + fastexp(2, s.length() - 1 - i, ones + 1)) % (ones + 1);
      if (ones != 1) {
        ans2 = (ans2 + fastexp(2, s.length() - 1 - i, ones - 1)) % (ones - 1);
      }
    }
  }

  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '0') {
      ll temp = ans1;
      temp = (temp + fastexp(2, s.length() - 1 - i, ones + 1)) % (ones + 1);
      start[i] = temp;
    } else {
      if (ones == 1) {
        continue;
      }
      ll temp = ans2;
      temp = (temp - fastexp(2, s.length() - 1 - i, ones - 1) + ones - 1) %
             (ones - 1);
      start[i] = temp;
    }
  }
  for (int i = 0; i < s.length(); i++) {

    if (s[i] == '0') {
      cout << func(start[i], 1) << endl;
    } else {
      if (ones == 1) {
        cout << 0 << endl;
        continue;
      }
      cout << func(start[i], 1) << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <iostream>
#define ll long long int
using namespace std;
ll dp[200005];
ll func(ll cnt, ll mv) {
  if (cnt == 0) {
    return mv;
  }
  ll temp = dp[cnt];
  cnt = cnt % temp;
  return func(cnt, mv + 1);
}
ll fastexp(ll a, ll b, ll m) {
  ll ans = 1;
  while (b) {
    if (b % 2 == 1) {
      ans = (ans * a) % m;
    }

    a = (a * a) % m;
    b = b / 2;
  }
  return ans;
}
int main() {
  ll n;
  string s;
  cin >> n;

  cin >> s;

  for (int i = 0; i <= 200000; i++) {
    ll cnt = 0;
    ll tmp = i;
    while (tmp) {
      if (tmp % 2 == 1)
        cnt++;

      tmp /= 2;
    }

    dp[i] = cnt;
  }

  ll ones = 0;
  ll start[200005];
  ll ans1 = 0, ans2 = 0;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '1') {
      ones++;
    }
  }
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '1') {
      ans1 = (ans1 + fastexp(2, s.length() - 1 - i, ones + 1)) % (ones + 1);
      ans2 = (ans2 + fastexp(2, s.length() - 1 - i, ones - 1)) % (ones - 1);
    }
  }
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '0') {
      ll temp = ans1;
      temp = (temp + fastexp(2, s.length() - 1 - i, ones + 1)) % (ones + 1);
      start[i] = temp;
    } else {
      ll temp = ans2;
      temp = (temp - fastexp(2, s.length() - 1 - i, ones - 1) + ones - 1) %
             (ones - 1);
      start[i] = temp;
    }
  }
  for (int i = 0; i < s.length(); i++) {

    if (s[i] == '0') {
      cout << func(start[i], 1) << endl;
    } else {
      cout << func(start[i], 1) << endl;
    }
  }
}