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

/*
 *
 * 110101
 * po pierwszej zmianie wartosc spada na <= 2 * 10^5
 * log2(
 *
 */
#include <bits/stdc++.h>

using namespace std;

#define LL long long
#define ULL unsigned LL
#define LD long double

const int N = 2e5 + 2137;
int f[N]; // value of f(i) for i <= 2*10^5

// return 2^x mod MOD
LL pot(LL x, LL mod) {
  if (x == 0)
    return 1;
  if (x % 2) {
    return ((LL)2 * pot(x - 1, mod)) % mod;
  } else {
    LL wx = pot(x / 2, mod) % mod;
    return (wx * wx) % mod;
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  f[0] = 0;
  for (int i = 1; i <= 2e5; i++) {
    int x = i;
    int w = 0;
    while (x) {
      x = x % __builtin_popcount(x);
      w++;
    }
    f[i] = w;
  }
  int n;
  cin >> n;
  string s;
  cin >> s;
  LL val[3];
  LL p = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      p++;
  }
  val[0] = val[1] = val[2] = 0;
  for (int i = 0; i < n; i++) {
    if (p > 1)
      val[0] = (val[0] * (LL)2 + (s[i] == '1' ? 1 : 0)) % (p - 1);
    if (p > 0)
      val[1] = (val[1] * (LL)2 + (s[i] == '1' ? 1 : 0)) % p;
    val[2] = (val[2] * (LL)2 + (s[i] == '1' ? 1 : 0)) % (p + 1);
  }
  // cerr<<val[0]<<" "<<val[1]<<" "<<val[2]<<"\\n\\n\\n";
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (p == 1) {
        cout << 0 << "\\n";
        continue;
      }
      // -1
      // val[0]
      LL vx = val[0] - pot(n - i - 1, p - 1);
      // cerr<<"Wartosc vx = "<<vx<<"\\n";
      vx %= ((LL)(p - 1));
      vx += ((LL)(p - 1));
      vx %= ((LL)(p - 1));
      // cerr<<"Wartosc po modulacji, vx = "<<vx<<"\\n";
      cout << 1 + f[vx] << "\\n";
    } else {
      // +1
      // val[2]
      LL vx = val[2] + pot(n - i - 1, p + 1);
      // cerr<<"2 - Wartosc vx = "<<vx<<"\\n";
      vx %= ((LL)(p + 1));
      vx += ((LL)(p + 1));
      vx %= ((LL)(p + 1));
      // cerr<<"2 - Wartosc po modulacji, vx = "<<vx<<"\\n";
      cout << 1 + f[vx] << "\\n";
    }
    // cerr<<"\\n\\n";
  }
}


Error: 0

Fixed submission:

/*
 *
 * 110101
 * po pierwszej zmianie wartosc spada na <= 2 * 10^5
 * log2(
 *
 */
#include <bits/stdc++.h>

using namespace std;

#define LL long long
#define ULL unsigned LL
#define LD long double

const int N = 2e5 + 2137;
int f[N]; // value of f(i) for i <= 2*10^5

// return 2^x mod MOD
LL pot(LL x, LL mod) {
  if (x == 0)
    return 1;
  if (x % 2) {
    return ((LL)2 * pot(x - 1, mod)) % mod;
  } else {
    LL wx = pot(x / 2, mod) % mod;
    return (wx * wx) % mod;
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  f[0] = 0;
  for (int i = 1; i <= 2e5; i++) {
    int x = i;
    int w = 0;
    while (x) {
      x = x % __builtin_popcount(x);
      w++;
    }
    f[i] = w;
  }
  int n;
  cin >> n;
  string s;
  cin >> s;
  LL val[3];
  LL p = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      p++;
  }
  val[0] = val[1] = val[2] = 0;
  for (int i = 0; i < n; i++) {
    if (p > 0)
      val[0] = (val[0] * (LL)2 + (s[i] == '1' ? 1 : 0)) % (p - 1);
    val[1] = (val[1] * (LL)2 + (s[i] == '1' ? 1 : 0)) % p;
    val[2] = (val[2] * (LL)2 + (s[i] == '1' ? 1 : 0)) % (p + 1);
  }
  // cerr<<val[0]<<" "<<val[1]<<" "<<val[2]<<"\\n\\n\\n";
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      // -1
      // val[0]
      LL vx = val[0] - pot(n - i - 1, p - 1);
      // cerr<<"Wartosc vx = "<<vx<<"\\n";
      vx %= ((LL)(p - 1));
      vx += ((LL)(p - 1));
      vx %= ((LL)(p - 1));
      if (vx < 0 || vx > 2e5) {
        cout << "JP2GMD";
        return 0;
      }
      // cerr<<"Wartosc po modulacji, vx = "<<vx<<"\\n";
      cout << 1 + f[vx] << "\\n";
    } else {
      // +1
      // val[2]
      LL vx = val[2] + pot(n - i - 1, p + 1);
      // cerr<<"2 - Wartosc vx = "<<vx<<"\\n";
      vx %= ((LL)(p + 1));
      vx += ((LL)(p + 1));
      vx %= ((LL)(p + 1));
      if (vx < 0 || vx > 2e5) {
        cout << "JP2GMD";
        return 0;
      }
      // cerr<<"2 - Wartosc po modulacji, vx = "<<vx<<"\\n";
      cout << 1 + f[vx] << "\\n";
    }
    // cerr<<"\\n\\n";
  }
}