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

/* Bismillahirrahmanirrahim */
/* Author : Jehian Norman Saviero (@Reiva5) */
#include <bits/stdc++.h>

// MACROS SAMPAH
#define Jehian using
#define Norman namespace
#define Saviero std

Jehian Norman Saviero;

// MACROS TYPE
typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;

// MACROS FOR PRINT
#define nl printf("\\n")
#define sp printf(" ")

// MACROS FOR ATTRIBUTE
#define fi first
#define se second

// MACROS FOR FUNCTION AND METHOD
#define eb emplace_back
#define mp make_pair
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define ALL(x) (x).begin(), (x).end()
#define ALLN(x, n) (x), (x) + (n)
#define CUBE(x) (SQR(x) * (x))
#define SET(x, y)                                                              \\
  for (int _qwe = sizeof(x) / sizeof(x[0]); _qwe;)                             \\
  x[--_qwe] = (y)
#define SQR(x) ((x) * (x))
#define SSORT(x, sz) sort(ALLN(x, sz))
#define RESET(x) memset((x), 0, sizeof(x))
#define VSORT(x) sort(ALL(x))

// MACROS READ INPUT
#define sci(x) scanf("%d", &x)
#define scl(x) scanf("%ld", &x)
#define scll(x) scanf("%lld", &x)
#define scul(x) scl(x)
#define scull(x) scll(x)

ll MOD = 1e9 + 7;

inline ll MUL(ll a, ll b, const ll MD = MOD) {
  if (MD == 0)
    return -1;
  return (a * b) % MD;
}

inline ll ADD(ll a, ll b, const ll MD = MOD) {
  if (MD == 0)
    return -1;
  return (a + b) % MD;
}

inline ll SUB(ll a, ll b, const ll MD = MOD) {
  if (MD == 0)
    return -1;
  a %= MD;
  b %= MD;
  return ((a - b) % MD + MD) % MD;
}

inline ll powMod(ll a, ll p, const ll MD = MOD) {
  if (MD == 0)
    return -1;
  ll res = 1;
  for (; p; p >>= 1, a = MUL(a, a, MD)) {
    if (p & 1)
      res = MUL(res, a, MD);
  }
  return res;
}

ll eval(ll val) {
  if (val == 0)
    return 0;
  return 1LL + eval(val % __builtin_popcountll(val));
}

const int sz = 3e5 + 5;
pll curr[sz];

int main() {
  int N;
  sci(N);
  string s;
  cin >> s;
  int hit = 0;
  for (int i = 0; i < s.size(); ++i) {
    hit += s[i] == '1';
  }
  ll gede = 0;
  ll kecil = 0;
  for (int i = 0; i < s.size(); ++i) {
    pll tmp = {powMod(2, s.size() - 1 - i, hit + 1),
               powMod(2, s.size() - 1 - i, hit - 1)};
    if (s[i] == '1') {
      gede = ADD(gede, tmp.fi, hit + 1);
      kecil = ADD(kecil, tmp.se, hit - 1);
    }
  }
  for (int i = 0; i < s.size(); ++i) {
    ll tmp;
    if (s[i] == '0') {
      tmp = ADD(gede, powMod(2, s.size() - 1 - i, hit + 1), hit + 1);
    } else {
      tmp = SUB(kecil, powMod(2, s.size() - 1 - i, hit - 1), hit - 1);
    }
    if (tmp >= 0) {
      printf("%lld\\n", 1 + eval(tmp));
    } else {
      printf("0\\n");
    }
  }
  return 0;
}

Error: 0

Fixed submission:

/* Bismillahirrahmanirrahim */
/* Author : Jehian Norman Saviero (@Reiva5) */
#include <bits/stdc++.h>

// MACROS SAMPAH
#define Jehian using
#define Norman namespace
#define Saviero std

Jehian Norman Saviero;

// MACROS TYPE
typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;

// MACROS FOR PRINT
#define nl printf("\\n")
#define sp printf(" ")

// MACROS FOR ATTRIBUTE
#define fi first
#define se second

// MACROS FOR FUNCTION AND METHOD
#define eb emplace_back
#define mp make_pair
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define ALL(x) (x).begin(), (x).end()
#define ALLN(x, n) (x), (x) + (n)
#define CUBE(x) (SQR(x) * (x))
#define SET(x, y)                                                              \\
  for (int _qwe = sizeof(x) / sizeof(x[0]); _qwe;)                             \\
  x[--_qwe] = (y)
#define SQR(x) ((x) * (x))
#define SSORT(x, sz) sort(ALLN(x, sz))
#define RESET(x) memset((x), 0, sizeof(x))
#define VSORT(x) sort(ALL(x))

// MACROS READ INPUT
#define sci(x) scanf("%d", &x)
#define scl(x) scanf("%ld", &x)
#define scll(x) scanf("%lld", &x)
#define scul(x) scl(x)
#define scull(x) scll(x)

ll MOD = 1e9 + 7;

inline ll MUL(ll a, ll b, const ll MD = MOD) { return (a * b) % MD; }

inline ll ADD(ll a, ll b, const ll MD = MOD) { return (a + b) % MD; }

inline ll SUB(ll a, ll b, const ll MD = MOD) {
  a %= MD;
  b %= MD;
  return ((a - b) % MD + MD) % MD;
}

inline ll powMod(ll a, ll p, const ll MD = MOD) {
  ll res = 1;
  for (; p; p >>= 1, a = MUL(a, a, MD)) {
    if (p & 1)
      res = MUL(res, a, MD);
  }
  return res;
}

ll eval(ll val) {
  if (val == 0)
    return 0;
  return 1 + eval(val % __builtin_popcountll(val));
}

int main() {
  int N;
  sci(N);
  string s;
  cin >> s;
  int hit = 0;
  for (int i = 0; i < s.size(); ++i) {
    hit += s[i] == '1';
  }
  pll curr[N + 1];
  ll gede = 0;
  ll kecil = 0;
  for (int i = 0; i < s.size(); ++i) {
    curr[s.size() - 1 - i] = {powMod(2, s.size() - 1 - i, hit + 1),
                              powMod(2, s.size() - 1 - i, hit - 1)};
    if (s[i] == '1') {
      gede = ADD(gede, curr[s.size() - i - 1].fi, hit + 1);
      kecil = ADD(kecil, curr[s.size() - i - 1].se, hit - 1);
    }
  }
  for (int i = 0; i < s.size(); ++i) {
    ll tmp;
    if (s[i] == '0') {
      tmp = ADD(gede, powMod(2, s.size() - 1 - i, hit + 1), hit + 1);
    } else {
      tmp = SUB(kecil, powMod(2, s.size() - 1 - i, hit - 1), hit - 1);
    }
    printf("%lld\\n", 1 + eval(tmp));
  }
  return 0;
}