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

// Arayi
// #define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <complex>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define fr first
#define sc second
#define MP make_pair
#define ad push_back
#define PB push_back
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define lli long long int
#define y1 arayikhalatyan
#define j1 jigglypuff
#define ld long double
#define itn int
#define pir pair<int, int>
#define all(x) (x).begin(), (x).end()
#define str string
#define enl endl
#define en endl
#define cd complex<long double>
#define vcd vector<cd>
#define vii vector<int>
#define vlli vector<lli>
using namespace std;

lli gcd(lli a, lli b) { return (b == 0LL ? a : gcd(b, a % b)); }
ld dist(ld x, ld y1, ld x2, ld y2) {
  return sqrt((x - x2) * (x - x2) + (y1 - y2) * (y1 - y2));
}
lli S(lli a) { return (a * (a + 1LL)) / 2; }
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
char vow[] = {'a', 'e', 'i', 'o', 'u'};
int dx[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};
int dy[] = {0, 0, 1, -1, -1, 1, -1, 1, 0};

const int N = 3e5 + 30;
lli mod = 36000;
const ld pi = acos(-1);
const int T = 238;
const ld e = 1e-13;

lli bp(lli a, lli b = mod - 2LL) {
  lli ret = 1;
  while (b) {
    if (b & 1)
      ret *= a, ret %= mod;
    a *= a;
    a %= mod;
    b >>= 1;
  }
  return ret;
}
ostream &operator<<(ostream &c, pir a) {
  c << a.fr << " " << a.sc;
  return c;
}
struct pt {
  lli x, y;
  int ind;
  int bl;
  bool operator==(const pt &a) { return ind == a.ind; }
  bool operator<(const pt &b) {
    if (x == b.x)
      return y < b.y;
    return x < b.x;
  }
};

int n;
lli mn1, mn2;
string s;
lli mk;
lli x[N];
lli dp[N];
int f(int x) {
  int sm = 0;
  for (int i = 0; i < 21; i++) {
    if ((1 << i) & x)
      sm++;
  }
  return x % sm;
}
int main() {
  fastio;
  cin >> n >> s;
  reverse(all(s));
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      mk++;
  }
  if (mk == 0) {
    for (int i = 0; i < n; i++) {
      cout << 1 << " ";
    }
    return 0;
  }
  if (mk == 1) {
    reverse(all(s));
    for (int i = 0; i < n; i++) {
      if (s[i] == '1')
        cout << 0 << " ";
      else {
        if (s.back() == '1' || i == n - 1)
          cout << 2 << " ";
        else
          cout << 1 << " ";
      }
    }
    return 0;
  }
  mod = mk + 1LL;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      mn1 += bp(2, i);
    mn1 %= mod;
  }
  mod = mk - 1LL;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      mn2 += bp(2, i);
    mn2 %= mod;
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      mod = mk - 1LL;
      x[i] = mn2 - bp(2, i);
      x[i] %= mod;
      if (x[i] < 0)
        x[i] += mod;
    } else {
      mod = mk + 1LL;
      x[i] = mn1 + bp(2, i);
      x[i] %= mod;
    }
  }
  for (int i = 1; i <= n + 10; i++) {
    dp[i] = 1LL + dp[f(i)];
  }
  for (int i = 0; i < n; i++) {
    cout << dp[x[n - i - 1]] + 1 << " ";
  }
  return 0;
}

/*


  __
*(><)*
  \\/ /
  ||/
--||
  ||
  /\\
 /  \\
/    \\
*/

Error: WA

Fixed submission:

// Arayi
// #define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <complex>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define fr first
#define sc second
#define MP make_pair
#define ad push_back
#define PB push_back
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define lli long long int
#define y1 arayikhalatyan
#define j1 jigglypuff
#define ld long double
#define itn int
#define pir pair<int, int>
#define all(x) (x).begin(), (x).end()
#define str string
#define enl endl
#define en endl
#define cd complex<long double>
#define vcd vector<cd>
#define vii vector<int>
#define vlli vector<lli>
using namespace std;

lli gcd(lli a, lli b) { return (b == 0LL ? a : gcd(b, a % b)); }
ld dist(ld x, ld y1, ld x2, ld y2) {
  return sqrt((x - x2) * (x - x2) + (y1 - y2) * (y1 - y2));
}
lli S(lli a) { return (a * (a + 1LL)) / 2; }
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
char vow[] = {'a', 'e', 'i', 'o', 'u'};
int dx[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};
int dy[] = {0, 0, 1, -1, -1, 1, -1, 1, 0};

const int N = 3e5 + 30;
lli mod = 36000;
const ld pi = acos(-1);
const int T = 238;
const ld e = 1e-13;

lli bp(lli a, lli b = mod - 2LL) {
  lli ret = 1;
  while (b) {
    if (b & 1)
      ret *= a, ret %= mod;
    a *= a;
    a %= mod;
    b >>= 1;
  }
  return ret;
}
ostream &operator<<(ostream &c, pir a) {
  c << a.fr << " " << a.sc;
  return c;
}
struct pt {
  lli x, y;
  int ind;
  int bl;
  bool operator==(const pt &a) { return ind == a.ind; }
  bool operator<(const pt &b) {
    if (x == b.x)
      return y < b.y;
    return x < b.x;
  }
};

int n, mn1, mn2;
string s;
lli mk;
int x[N];
int dp[N];
int f(int x) {
  int sm = 0;
  for (int i = 0; i < 20; i++) {
    if ((1 << i) & x)
      sm++;
  }
  return x % sm;
}
int main() {
  fastio;
  cin >> n >> s;
  reverse(all(s));
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      mk++;
  }
  if (mk == 0) {
    for (int i = 0; i < n; i++) {
      cout << 1 << " ";
    }
    return 0;
  }
  if (mk == 1) {
    assert(0);
    reverse(all(s));
    for (int i = 0; i < n; i++) {
      if (s[i] == '1')
        cout << 0 << " ";
      else {
        if (s.back() == '1')
          cout << 2 << " ";
        else
          cout << 1 << " ";
      }
    }
    return 0;
  }
  mod = mk + 1LL;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      mn1 += bp(2, i);
    mn1 %= mod;
  }
  mod = mk - 1LL;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      mn2 += bp(2, i);
    mn2 %= mod;
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      mod = mk - 1LL;
      x[i] = mn2 - bp(2, i);
      x[i] %= mod;
      if (x[i] < 0)
        x[i] += mod;
    } else {
      mod = mk + 1LL;
      x[i] = mn1 + bp(2, i);
      x[i] %= mod;
    }
  }
  for (int i = 1; i <= n; i++) {
    dp[i] = 1 + f(i);
  }
  for (int i = 0; i < n; i++) {
    cout << dp[x[n - i - 1]] + 1 << " ";
  }
  return 0;
}

/*


  __
*(><)*
  \\/ /
  ||/
--||
  ||
  /\\
 /  \\
/    \\
*/