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

#include <bits/stdc++.h>
using namespace std;

#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) #VariableName
#define LOG(x)                                                                 \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << endl;
#define LOG2(x, y)                                                             \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << endl;
#define LOG3(x, y, z)                                                          \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << " " \\
         << TO_STRING(z) << "=" << z << endl;
#define LOG4(w, x, y, z)                                                       \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(w) << "=" << w << " " << TO_STRING(x) << "=" << x << " " \\
         << TO_STRING(y) << "=" << y << " " << TO_STRING(z) << "=" << z        \\
         << endl;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

// a^n mod を計算する
lli modpow(lli a, lli n, lli mod) {
  lli res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

// a^-1
lli modinv(lli a, lli mod) {
  lli b = mod, u = 1, v = 0;
  while (b) {
    lli t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= mod;
  if (u < 0)
    u += mod;
  return u;
}

lli popCount(lli now) {
  lli cnt = 0;
  while (now > 0) {
    if (now % 2 == 1) {
      cnt++;
    }
    now /= 2;
  }
  return cnt;
}

void func(long long N, std::string X) {

  lli start = 0;
  REP(i, 0, N) {
    if (X[i] == '1')
      start++;
  }

  lli modP = start + 1;
  lli modL = start - 1;

  lli baseP = 0;
  lli two = 1;

  for (lli i = N - 1; i >= 0; i--) {
    if (modP <= 0)
      break;
    if (X[i] == '1') {
      baseP += two;
      baseP %= modP;
    }
    two *= 2;
    two %= modP;
  }

  lli baseL = 0;
  two = 1;
  for (lli i = N - 1; i >= 0; i--) {
    if (modL <= 0)
      break;
    if (X[i] == '1') {
      baseL += two;
      baseL %= modL;
    }
    two *= 2;
    two %= modL;
  }

  LOG2(baseP, modP);
  LOG2(baseL, modL);
  REP(i, 0, N) {
    lli now;
    if (X[i] == '0') {
      if (modP <= 0) {
        cout << 0 << endl;
        continue;
      }
      now = baseP;
      now += modpow(2, (N - 1 - i), modP);
      now %= modP;
    } else {
      if (modL <= 0) {
        cout << 0 << endl;
        continue;
      }
      now = baseL;
      now = now + modL - modpow(2, (N - 1 - i), modL);
      now %= modL;
    }
    LOG2(i, now);
    if (now == 0) {
      cout << 1 << endl;
      continue;
      ;
    }
    lli cnt = 1;
    while (now != 0) {
      LOG2(i, now);
      now = now % popCount(now);
      cnt++;
    }
    cout << cnt << endl;
  }
}

int main() {
  // cout << fixed << setprecision(5);

  long long N;
  scanf("%lld", &N);
  std::string X;
  std::cin >> X;
  func(N, X);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) #VariableName
#define LOG(x)                                                                 \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << endl;
#define LOG2(x, y)                                                             \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << endl;
#define LOG3(x, y, z)                                                          \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << " " \\
         << TO_STRING(z) << "=" << z << endl;
#define LOG4(w, x, y, z)                                                       \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(w) << "=" << w << " " << TO_STRING(x) << "=" << x << " " \\
         << TO_STRING(y) << "=" << y << " " << TO_STRING(z) << "=" << z        \\
         << endl;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

// a^n mod を計算する
lli modpow(lli a, lli n, lli mod) {
  lli res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

// a^-1
lli modinv(lli a, lli mod) {
  lli b = mod, u = 1, v = 0;
  while (b) {
    lli t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= mod;
  if (u < 0)
    u += mod;
  return u;
}

lli popCount(lli now) {
  lli cnt = 0;
  while (now > 0) {
    if (now % 2 == 1) {
      cnt++;
    }
    now /= 2;
  }
  return cnt;
}

void func(long long N, std::string X) {

  lli start = 0;
  REP(i, 0, N) {
    if (X[i] == '1')
      start++;
  }

  lli modP = start + 1;
  lli modL = start - 1;

  lli baseP = 0;
  lli two = 1;

  for (lli i = N - 1; i >= 0; i--) {
    if (X[i] == '1') {
      baseP += two;
      baseP %= modP;
    }
    two *= 2;
    two %= modP;
  }

  lli baseL = 0;
  two = 1;
  for (lli i = N - 1; i >= 0; i--) {
    if (X[i] == '1') {
      baseL += two;
      baseL %= modL;
    }
    two *= 2;
    two %= modL;
  }

  LOG2(baseP, modP);
  LOG2(baseL, modL);
  REP(i, 0, N) {
    lli now;
    if (X[i] == '0') {
      if (modP <= 0) {
        cout << 0 << endl;
        continue;
      }
      now = baseP;
      now += modpow(2, (N - 1 - i), modP);
      now %= modP;
    } else {
      if (modL <= 0) {
        cout << 0 << endl;
        continue;
      }
      now = baseL;
      now = now + modL - modpow(2, (N - 1 - i), modL);
      now %= modL;
    }
    LOG2(i, now);
    if (now == 0) {
      cout << 1 << endl;
      continue;
      ;
    }
    lli cnt = 1;
    while (now != 0) {
      LOG2(i, now);
      now = now % popCount(now);
      cnt++;
    }
    cout << cnt << endl;
  }
}

int main() {
  // cout << fixed << setprecision(5);

  long long N;
  scanf("%lld", &N);
  std::string X;
  std::cin >> X;
  func(N, X);
  return 0;
}