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

/***************************************** START OF TEMPLATE
 * *********************************************/

#include <bits/stdc++.h>

using namespace std;

#define SPEED                                                                  \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout << fixed;                                                               \\
  cout << setprecision(10);
#define randomINIT                                                             \\
  mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define foo(i, a, n) for (ll i = (a); i <= n; i++)
#define frr(i, a, n) for (ll i = (a); i >= n; i--)
#define fo(i, n) for (ll i = 0; i < n; i++)
#define newl cout << "\\n";
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define sym(s) s = "#" + s + "#";
#define sqr(v) v *v;
#define all(x) (x).begin(), (x).end()
#define alll(x, n) x + 1, x + n + 1
#define mset(x, val) memset(x, val, sizeof(x))
#define mset1d(a, n, v) foo(i, 0, n + 5) a[i] = v;
#define mset2d(a, n, m, v) foo(i, 0, n + 5) foo(j, 0, m + 5) a[i][j] = v;
#define INP(a, n) foo(i, 1, n) cin >> a[i];
#define deb1d(a, n)                                                            \\
  foo(i, 1, n) cout << a[i] << " ";                                            \\
  newl;
#define deb2d(a, n, m)                                                         \\
  foo(i, 1, n) {                                                               \\
    foo(j, 1, m) { cout << a[i][j] << " "; }                                   \\
    newl;                                                                      \\
  }
#define debm(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << "{" << it.f << "," << it.s << "}, ";                               \\
  newl;
#define debp(a, n)                                                             \\
  foo(i, 1, n) cout << "{" << a[i].f << "," << a[i].s << "}, ";                \\
  newl;
#define debv(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << it << " ";                                                         \\
  newl;
#define deb1(a)                                                                \\
  cout << a << " ";                                                            \\
  newl;
#define deb2(a, b)                                                             \\
  cout << a << " " << b;                                                       \\
  newl;
#define deb3(a, b, c)                                                          \\
  cout << a << " " << b << " " << c;                                           \\
  newl;
#define deb4(a, b, c, d)                                                       \\
  cout << a << " " << b << " " << c << " " << d;                               \\
  newl;
#define ll long long
#define ld long double
#define pll pair<ll, ll>
#define vll vector<ll>
#define vpll vector<pll>

const ld PI = 3.14159265358979323846;
const ll MOD = 1e+9 + 7;
const ll INF = LLONG_MAX;
const int INFi = INT_MAX;
const ll N = 2e+5 + 8;

vll adj[N];
ll vis[N];
ll dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};

ll test_cases = 1;
randomINIT;

/*********************************************************************************************************/
/***************************************** VARIABLE DECLARATIONS
 * *****************************************/
ll n, p;
string s;
ll sum1, sum2;
ll p2[N][2];
/***************************************** FUNCTION IMPLEMENTATIONS
 * **************************************/
void init(ll &sum, ll p, ll id) {
  p2[0][id] = 1;
  foo(i, 1, N - 1) p2[i][id] = p2[i - 1][id] * 2 % p;
  frr(i, n, 1) {
    if (s[i] == '1') {
      sum += p2[n - i][id];
      sum %= p;
    }
  }
}

ll solve(ll n) {
  ll cnt = 0;
  while (n > 0) {
    n %= __builtin_popcount(n);
    cnt++;
  }
  return n + cnt + 1;
}
/***************************************** START OF MAIN FUNCTION
 * ****************************************/
void MAIN(ll tc) {
  cin >> n >> s;
  sym(s);
  foo(i, 1, n) p += (s[i] == '1');
  if (p != 1)
    init(sum1, p - 1, 0);
  init(sum2, p + 1, 1);
  foo(i, 1, n) {
    ll nxt;
    if (s[i] == '1') {
      if (p != 1)
        nxt = (sum1 - p2[n - i][0] + (p - 1)) % (p - 1);
      else
        nxt = -1;
    } else {
      nxt = (sum2 + p2[n - i][1]) % (p + 1);
    }
    deb1(solve(nxt));
  }
}

int main() {
  SPEED;

  // cin>>test_cases;
  foo(i, 1, test_cases) { MAIN(i); }
}

Error: WA

Fixed submission:

/***************************************** START OF TEMPLATE
 * *********************************************/

#include <bits/stdc++.h>

using namespace std;

#define SPEED                                                                  \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout << fixed;                                                               \\
  cout << setprecision(10);
#define randomINIT                                                             \\
  mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define foo(i, a, n) for (ll i = (a); i <= n; i++)
#define frr(i, a, n) for (ll i = (a); i >= n; i--)
#define fo(i, n) for (ll i = 0; i < n; i++)
#define newl cout << "\\n";
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define sym(s) s = "#" + s + "#";
#define sqr(v) v *v;
#define all(x) (x).begin(), (x).end()
#define alll(x, n) x + 1, x + n + 1
#define mset(x, val) memset(x, val, sizeof(x))
#define mset1d(a, n, v) foo(i, 0, n + 5) a[i] = v;
#define mset2d(a, n, m, v) foo(i, 0, n + 5) foo(j, 0, m + 5) a[i][j] = v;
#define INP(a, n) foo(i, 1, n) cin >> a[i];
#define deb1d(a, n)                                                            \\
  foo(i, 1, n) cout << a[i] << " ";                                            \\
  newl;
#define deb2d(a, n, m)                                                         \\
  foo(i, 1, n) {                                                               \\
    foo(j, 1, m) { cout << a[i][j] << " "; }                                   \\
    newl;                                                                      \\
  }
#define debm(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << "{" << it.f << "," << it.s << "}, ";                               \\
  newl;
#define debp(a, n)                                                             \\
  foo(i, 1, n) cout << "{" << a[i].f << "," << a[i].s << "}, ";                \\
  newl;
#define debv(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << it << " ";                                                         \\
  newl;
#define deb1(a)                                                                \\
  cout << a << " ";                                                            \\
  newl;
#define deb2(a, b)                                                             \\
  cout << a << " " << b;                                                       \\
  newl;
#define deb3(a, b, c)                                                          \\
  cout << a << " " << b << " " << c;                                           \\
  newl;
#define deb4(a, b, c, d)                                                       \\
  cout << a << " " << b << " " << c << " " << d;                               \\
  newl;
#define ll long long
#define ld long double
#define pll pair<ll, ll>
#define vll vector<ll>
#define vpll vector<pll>

const ld PI = 3.14159265358979323846;
const ll MOD = 1e+9 + 7;
const ll INF = LLONG_MAX;
const int INFi = INT_MAX;
const ll N = 2e+5 + 8;

vll adj[N];
ll vis[N];
ll dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};

ll test_cases = 1;
randomINIT;

/*********************************************************************************************************/
/***************************************** VARIABLE DECLARATIONS
 * *****************************************/
ll n, p;
string s;
ll sum1, sum2;
ll p2[N][2];
/***************************************** FUNCTION IMPLEMENTATIONS
 * **************************************/
void init(ll &sum, ll p, ll id) {
  p2[0][id] = 1;
  foo(i, 1, N - 1) p2[i][id] = p2[i - 1][id] * 2 % p;
  frr(i, n, 1) {
    if (s[i] == '1') {
      sum += p2[n - i][id];
      sum %= p;
    }
  }
}

ll solve(ll n) {
  ll cnt = 0;
  while (n > 0) {
    n %= __builtin_popcount(n);
    cnt++;
  }
  return cnt + 1;
}
/***************************************** START OF MAIN FUNCTION
 * ****************************************/
void MAIN(ll tc) {
  cin >> n >> s;
  sym(s);
  foo(i, 1, n) p += (s[i] == '1');
  init(sum1, p - 1, 0);
  init(sum2, p + 1, 1);
  foo(i, 1, n) {
    ll nxt;
    if (s[i] == '1')
      nxt = (sum1 - p2[n - i][0] + (p - 1)) % (p - 1);
    else
      nxt = (sum2 + p2[n - i][1]) % (p + 1);
    deb1(solve(nxt));
  }
}

int main() {
  SPEED;

  // cin>>test_cases;
  foo(i, 1, test_cases) { MAIN(i); }
}