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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
#define forn(x, n) for (int x = 0; x < n; x++)
#define forr(x, n) for (int x = n; x >= 0; x--)
#define for1(x, n) for (int x = 1; x < n; x++)
#define fore(x, a, b, c) for (int x = a; x <= b; x += c)
#define all(container) (container).begin(), container.end()
#define rall(container) (container).rbegin(), container.rend()
#define sz(container) (int)(container).size()
#define pb push_back
#define eb emplace_back
#define fi first
#define sc second

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#ifdef LOCAL_DEFINE
#include "DEBUG.hpp"
#else
#define eprint(...) 0
#endif
template <class T> bool smin(T &a, T b) {
  return a > b ? (a = b, true) : false;
}
template <class T> bool smax(T &a, T b) {
  return a < b ? (a = b, true) : false;
}

const int N = 200010;
char line[N];
ll dp[N];
string num;

ll epow(ll a, ll b, ll Mod) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * a % Mod;
    b >>= 1;
    a = a * a % Mod;
  }
  return res;
}

int main(void) {
  int n;
  scanf("%d", &n);
  scanf("%s", line);
  num = string(line);
  dp[0] = 0;
  dp[1] = 1;
  dp[2] = 1;
  fore(i, 3, N - 1, 1) {
    int tmp = i % __builtin_popcount(i);
    dp[i] = 1 + dp[tmp];
  }

  ll cnt = 0;
  forn(i, n) if (num[i] == '1')++ cnt;
  reverse(all(num));
  ll rem1 = 0;
  ll rem2 = 0;
  vector<ll> ans;
  if (cnt == 1) {
    int pos = -1;
    forn(i, n) if (num[i] == '1') pos = i;
    eprint(pos);
    forn(i, n) {
      ll val = (epow(2, pos, 2) + epow(2, i, 2)) % 2;
      if (num[i] == '0') {
        ans.pb(1 + dp[val]);
      } else
        ans.pb(0);
    }
    reverse(all(ans));
    forn(i, sz(ans)) printf("%d\\n", ans[i]);
    return 0;
  }
  forn(i, n) {
    rem1 =
        (rem1 + (num[i] - '0') * epow(2, i, cnt - 1) % (cnt - 1)) % (cnt - 1);
    rem2 =
        (rem2 + (num[i] - '0') * epow(2, i, cnt + 1) % (cnt + 1)) % (cnt + 1);
  }
  forn(i, n) {
    if (num[i] == '0') {
      ll tmp = (rem2 + epow(2, i, cnt + 1)) % (cnt + 1);
      ans.pb(1 + dp[tmp]);
    } else {
      ll tmp = (cnt - 1 + (rem1 - epow(2, i, cnt - 1)) % (cnt - 1)) % (cnt - 1);
      ans.pb(1 + dp[tmp]);
    }
  }
  reverse(all(ans));
  forn(i, sz(ans)) printf("%lld\\n", ans[i]);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
#define forn(x, n) for (int x = 0; x < n; x++)
#define forr(x, n) for (int x = n; x >= 0; x--)
#define for1(x, n) for (int x = 1; x < n; x++)
#define fore(x, a, b, c) for (int x = a; x <= b; x += c)
#define all(container) (container).begin(), container.end()
#define rall(container) (container).rbegin(), container.rend()
#define sz(container) (int)(container).size()
#define pb push_back
#define eb emplace_back
#define fi first
#define sc second

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#ifdef LOCAL_DEFINE
#include "DEBUG.hpp"
#else
#define eprint(...) 0
#endif
template <class T> bool smin(T &a, T b) {
  return a > b ? (a = b, true) : false;
}
template <class T> bool smax(T &a, T b) {
  return a < b ? (a = b, true) : false;
}

const int N = 200010;
char line[N];
int dp[N];
string num;

int epow(int a, int b, int Mod) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = res * a % Mod;
    b >>= 1;
    a = a * a % Mod;
  }
  return res;
}

int main(void) {
  int n;
  scanf("%d", &n);
  scanf("%s", line);
  num = string(line);
  dp[0] = 0;
  dp[1] = 1;
  dp[2] = 1;
  fore(i, 3, N - 1, 1) {
    int tmp = i % __builtin_popcount(i);
    dp[i] = 1 + dp[tmp];
  }

  int cnt = 0;
  forn(i, n) if (num[i] == '1')++ cnt;
  reverse(all(num));
  int rem1 = 0;
  int rem2 = 0;
  if (cnt == 1) {
    puts("1");
    return 0;
  }
  forn(i, n) {
    rem1 =
        (rem1 + (num[i] - '0') * epow(2, i, cnt - 1) % (cnt - 1)) % (cnt - 1);
    rem2 =
        (rem2 + (num[i] - '0') * epow(2, i, cnt + 1) % (cnt + 1)) % (cnt + 1);
  }
  vector<int> ans;
  forn(i, n) {
    if (num[i] == '0') {
      int tmp = (rem2 + epow(2, i, cnt + 1)) % (cnt + 1);
      ans.pb(1 + dp[tmp]);
    } else {
      int tmp = (cnt - 1 + (rem1 - epow(2, i, cnt - 1))) % (cnt - 1);
      ans.pb(1 + dp[tmp]);
    }
  }
  reverse(all(ans));
  forn(i, sz(ans)) printf("%d\\n", ans[i]);
  return 0;
}