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

/*
“Yesterday is history, tomorrow is a mystery, today is a gift of God,
which is why we call it the present.” ― Bill Keane
*/
#include <bits/stdc++.h>
#define endl "\\n"
#ifdef ONLINE_JUDGEsj
#define DEBUG(x)
#else
#define DEBUG(x) cout << #x << " : {" << x << "}" << endl;
#endif
using namespace std;
typedef long long int lli;
constexpr int MAX = 3e5;
inline lli add(lli a, lli b, int mod) {
  lli x = a + b;
  if (x >= mod)
    x -= mod;
  return x;
}
inline lli mul(lli a, lli b, int mod) { return ((a % mod) * (b % mod)) % mod; }
inline lli sub(lli a, lli b, int mod) {
  lli x = a - b;
  if (x < 0)
    x += mod;
  return x;
}
// lli powr(int a, unsigned int b, int m){
// 	a%=m;
// 	if(b==0) return 1LL;
// 	else if(b&1) return mul(a, powr(a*a, (b-1)/2, m), m);
// 	else return powr(a*a, b/2, m);
// }
lli powr(lli a, lli b, lli mod) {
  lli ans = 1;
  while (b) {
    if (b & 1ll)
      ans *= a, ans %= mod;
    a *= a;
    a %= mod;
    b >>= 1;
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  string s;
  cin >> s;
  int bitcnt = 0;
  for (char c : s)
    if (c == '1')
      bitcnt++;
  lli u, v;
  u = v = 0;
  for (int i = n - 1; i >= 0; --i) {
    if (s[i] == '1') {
      u = add(u, powr(2, n - i - 1, bitcnt + 1), bitcnt + 1);
      u = u % (bitcnt + 1);
      if (bitcnt - 1 == 0)
        continue;
      v = add(v, powr(2, n - i - 1, bitcnt - 1), bitcnt - 1);
      v = v % (bitcnt - 1);
    }
  }

  int dp[MAX] = {0};
  dp[1] = 1;
  dp[2] = 1;
  for (int i = 3; i < MAX; ++i) {
    dp[i] = 1 + dp[i % __builtin_popcount(i)];
  }
  lli res;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      if (bitcnt - 1 == 0) {
        cout << 0 << endl;
        continue;
      }
      res = sub(v, powr(2, n - i - 1, bitcnt - 1), bitcnt - 1);
      cout << 1 + dp[res % (bitcnt - 1)] << endl;
    } else {
      res = add(u, powr(2, n - i - 1, bitcnt + 1), bitcnt + 1);
      cout << 1 + dp[res % (bitcnt + 1)] << endl;
    }
  }
  return 0;
}


Error: 0

Fixed submission:

/*
“Yesterday is history, tomorrow is a mystery, today is a gift of God,
which is why we call it the present.” ― Bill Keane
*/
#include <bits/stdc++.h>
#define endl "\\n"
#ifdef ONLINE_JUDGE
#define DEBUG(x)
#else
#define DEBUG(x) cout << #x << " : {" << x << "}" << endl;
#endif
#define popcount __builtin_popcount
using namespace std;
typedef long long int lli;
constexpr int MAX = 3e5 + 10;
int dp[MAX];
int calc(int x) {
  if (dp[x] != -1)
    return dp[x];
  int y = 1 + calc(x % popcount(x));
  return y;
}
void go() {
  dp[0] = 0;
  for (int i = 1; i < MAX; ++i)
    dp[i] = calc(i);
}
int powr(int x, unsigned int y, int p) {
  int res = 1; // Initialize result

  x = x % p; // Update x if it is more than or
             // equal to p

  if (x == 0)
    return 0; // In case x is divisible by p;

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res % p;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  memset(dp, -1, sizeof(dp));
  go();
  int n;
  string x;
  cin >> n >> x;
  int ones = 0;
  for (int i = 0; i < n; ++i) {
    if (x[i] == '1')
      ones++;
  }
  int u, v;
  u = v = 0;
  for (int i = n - 1; i >= 0; --i) {
    if (x[i] == '1') {
      u = (u % (ones + 1) + powr(2, n - i - 1, ones + 1) % (ones + 1)) %
          (ones + 1);
      if (ones - 1 != 0)
        v = (v % (ones - 1) + powr(2, n - i - 1, ones - 1) % (ones - 1)) %
            (ones - 1);
    }
  }
  int r;
  for (int i = 0; i < n; ++i) {
    if (x[i] == '1') {
      if (ones - 1 == 0)
        cout << 0 << endl;
      else {
        r = ((v % (ones - 1) - powr(2, n - i - 1, ones - 1) % (ones - 1)) %
                 (ones - 1) +
             (ones - 1)) %
            (ones - 1);
        cout << 1 + dp[r % (ones - 1)] << endl;
      }
    } else {
      r = ((u % (ones + 1)) + (powr(2, n - i - 1, ones + 1) % (ones + 1))) %
          (ones + 1);
      cout << 1 + dp[r % (ones + 1)] << endl;
    }
  }
  return 0;
}