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

#include <bits/stdc++.h>
using namespace std;

#define ll long long int
// ll mod=1e9+7;

ll modd(string num, ll a) {
  // Initialize result
  ll res = 0;

  // One by one process all digits of 'num'
  for (ll i = 0; i < num.length(); i++)
    res = (res * 2 + (ll)num[i] - '0') % a;

  return res;
}

ll modular_exponentiation(ll base, ll power, ll mod) {
  ll ans = 1;
  base = base % mod;
  while (power > 0) {
    if (power & 1) {
      ans = (ans * base) % mod;
    }

    power /= 2;
    base = (base * base) % mod;
  }
  return ans;
}

int main() {
  /* Faster input ouput */
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);
  // cout.tie(NULL);

  /* To read input from a file and output to a file */
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

  ll t = 1;
  // cin >> t;
  ll i, j;
  ll arr[200005];
  arr[0] = 0;
  for (i = 1; i < 200005; i++) {
    ll temp = i;
    ll ops = 0;
    while (temp > 0) {
      ll bits = __builtin_popcount(temp);
      temp %= bits;
      ops++;
    }

    arr[i] = ops;
  }

  while (t--) {

    ll n;
    string x;
    cin >> n;
    cin >> x;

    ll nn = n;
    ll popc = 0;

    if (n == 1) {
      if (x == "0") {
        cout << 1 << '\\n';
      } else {
        cout << 0 << '\\n';
      }
      continue;
    }

    for (i = 0; i < n; i++) {
      if (x[i] == '1')
        popc++;
    }

    string temp = x;
    ll popci = popc;

    if (popci == 0) {
      // cout << 1 << '\\n';
      for (i = 0; i < n; i++) {
        cout << 1 << '\\n';
      }
      continue;
    }

    ll num = modd(temp, popci);
    ll numm1;
    if (popci > 1) {
      numm1 = modd(temp, popci - 1);
    }

    ll numm2 = modd(temp, popci + 1);

    // cout << arr[num] + 1 << '\\n';

    for (i = 0; i < n; i++) {

      if (temp[i] == '0') {
        // add this power
        // popci increased by 1
        ll newbits = popci + 1;
        ll add = modular_exponentiation(2, (n - i - 1), (newbits));
        ll fnum = (numm2 + add) % (newbits);
        cout << arr[fnum] + 1 << '\\n';

      } else {
        if (popci == 1) {
          cout << 0 << '\\n';
        } else {
          ll newbits = popci - 1;
          ll sub = modular_exponentiation(2, (n - i - 1), (popci - 1));
          ll fnum = ((numm1 - sub)) % (newbits);
          fnum += (newbits);
          fnum %= (newbits);
          cout << arr[fnum] + 1 << '\\n';
        }
      }
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define ll long long int
ll mod = 1e9 + 7;

int modd(string num, int a) {
  // Initialize result
  int res = 0;

  // One by one process all digits of 'num'
  for (int i = 0; i < num.length(); i++)
    res = (res * 2 + (int)num[i] - '0') % a;

  return res;
}

int modular_exponentiation(int base, int power, int mod) {
  int ans = 1;
  base = base % mod;
  while (power > 0) {
    if (power & 1) {
      ans = (ans * base) % mod;
    }

    power /= 2;
    base = (base * base) % mod;
  }
  return ans;
}

int main() {
  /* Faster input ouput */
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);
  // cout.tie(NULL);

  /* To read input from a file and output to a file */
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

  ll t = 1;
  // cin >> t;
  ll i, j;
  ll arr[100005];
  arr[0] = 0;
  for (i = 1; i < 100005; i++) {
    ll temp = i;
    ll ops = 0;
    while (temp > 0) {
      ll bits = __builtin_popcount(temp);
      temp %= bits;
      ops++;
    }

    arr[i] = ops;
  }

  while (t--) {

    ll n;
    string x;
    cin >> n;
    cin >> x;

    ll nn = n;
    ll popc = 0;

    for (i = 0; i < n; i++) {
      if (x[i] == '1')
        popc++;
    }

    string temp = x;
    ll popci = popc;

    ll num = modd(temp, popci);
    ll numm1 = modd(temp, popci - 1);
    ll numm2 = modd(temp, popci + 1);

    // cout << arr[num] + 1 << '\\n';

    for (i = 0; i < n; i++) {
      if (temp[i] == '0') {
        // add this power
        // popci increased by 1
        int add = modular_exponentiation(2, (n - i - 1), (popci + 1));
        int fnum = (numm2 + add) % (popci + 1);
        cout << arr[fnum] + 1 << '\\n';

      } else {
        int sub = modular_exponentiation(2, (n - i - 1), (popci - 1));
        int fnum = ((numm1 - sub)) % (popci - 1);
        fnum += (popci - 1);
        fnum %= (popci - 1);
        cout << arr[fnum] + 1 << '\\n';
      }
    }
  }

  return 0;
}