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

#include <bits/stdc++.h>
#define int long long int
#define ii pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
#define output(x)                                                              \\
  for (auto a : x)                                                             \\
    cout << a << " ";                                                          \\
  cout << endl;
#define output2(x)                                                             \\
  for (auto a : x)                                                             \\
    cout << a.fi << " " << a.se << endl;
#define PI 3.14159265
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

// DEBUG TEMPLATE
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

// DEBUG TEMPLATE ENDS

#define MOD 1000000007
int power(int x, int y, int mod = MOD) {
  int ret = 1;
  x = x % mod;
  while (y) {
    if (y & 1)
      ret = (ret * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return ret % mod;
}

int val(int x) {
  int ret = 0;
  while (x != 0) {
    ret++;
    x %= __builtin_popcount(x);
  }
  return ret;
}

void solve() {
  int n;
  string inp;
  cin >> n >> inp;
  int bits_set = 0;
  for (auto x : inp) {
    if (x == '1')
      bits_set++;
  }
  int lb = bits_set - 1;
  lb = max(lb, 1ll);
  int rb = bits_set + 1;
  int lsum = 0;
  int rsum = 0;
  int lcur = 1;
  int rcur = 1;
  for (int i = n - 1; i > -1; i--) {
    if (inp[i] == '1') {
      lsum += lcur;
      rsum += rcur;
    }
    lcur = (lcur * 2) % lb;
    rcur = (rcur * 2) % rb;
    lsum %= lb;
    rsum %= rb;
  }
  debug() << imie(lsum) imie(rsum);
  vector<int> out(n);
  lcur = 1;
  rcur = 1;
  for (int i = n - 1; i > -1; i--) {
    int cur = 0;
    if (inp[i] == '1') {
      cur = ((lsum - lcur) % lb + lb) % lb;
    } else {
      cur = ((rsum + rcur) % rb + rb) % rb;
    }
    debug() << imie(cur);
    out[i] = val(cur) + !(inp[i] == '1' && bits_set == 1);
    lcur = (lcur * 2) % lb;
    rcur = (rcur * 2) % rb;
  }
  for (auto x : out)
    cout << x << endl;
}

/*

*/
signed main() {
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}
// Check MOD values plox :(


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long int
#define ii pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
#define output(x)                                                              \\
  for (auto a : x)                                                             \\
    cout << a << " ";                                                          \\
  cout << endl;
#define output2(x)                                                             \\
  for (auto a : x)                                                             \\
    cout << a.fi << " " << a.se << endl;
#define PI 3.14159265
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

// DEBUG TEMPLATE
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

// DEBUG TEMPLATE ENDS

#define MOD 1000000007
int power(int x, int y, int mod = MOD) {
  int ret = 1;
  x = x % mod;
  while (y) {
    if (y & 1)
      ret = (ret * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return ret % mod;
}

int val(int x) {
  int ret = 0;
  while (x != 0) {
    ret++;
    x %= __builtin_popcount(x);
  }
  return ret;
}

void solve() {
  int n;
  string inp;
  cin >> n >> inp;
  int bits_set = 0;
  for (auto x : inp) {
    if (x == '1')
      bits_set++;
  }
  int lb = bits_set - 1;
  int rb = bits_set + 1;
  int lsum = 0;
  int rsum = 0;
  int lcur = 1;
  int rcur = 1;
  for (int i = n - 1; i > -1; i--) {
    if (inp[i] == '1') {
      lsum += lcur;
      rsum += rcur;
    }
    lcur = (lcur * 2) % lb;
    rcur = (rcur * 2) % rb;
    lsum %= lb;
    rsum %= rb;
  }
  debug() << imie(lsum) imie(rsum);
  vector<int> out(n);
  lcur = 1;
  rcur = 1;
  for (int i = n - 1; i > -1; i--) {
    int cur = 0;
    if (inp[i] == '1') {
      cur = ((lsum - lcur) % lb + lb) % lb;
    } else {
      cur = ((rsum + rcur) % rb + rb) % rb;
    }
    debug() << imie(cur);
    out[i] = val(cur) + 1;
    lcur = (lcur * 2) % lb;
    rcur = (rcur * 2) % rb;
  }
  for (auto x : out)
    cout << x << endl;
}

/*
  5 4 3 2 1

  5

  4 5 -> 4
  4 5 3 -> 7





*/
signed main() {
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}
// Check MOD values plox :(