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

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i <= (b); ++i)
#define per(i, a, b) for (int i = a; i >= (b); --i)
#define mem(a, x) memset(a, x, sizeof(a))
#define pb emplace_back
#define pii pair<int, int>
#define mk make_pair
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll read() {
  ll x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return w == 1 ? x : -x;
}
const int N = 1e6 + 10;
char s[N];
int n;
ll f[N], g[N], mod, mod1;
ll f1(ll x) {
  ll ans = 0;
  while (x) {
    if (x & 1)
      ans++;
    x = x / 2;
  }
  return ans;
}
void run(ll x) {
  ll ans = 1;

  while (x) {
    ll mod = f1(x);
    // printf("mod:%lld\\n", mod);
    x = x % mod;
    ans++;
  }
  printf("%lld\\n", ans);
}
int main() {
  // run(10);
  n = read();
  scanf("%s", s + 1);
  ll sum = 0;
  for (int i = 1; i <= n; ++i)
    sum += s[i] - '0';
  mod = sum + 1;
  mod1 = sum - 1;
  f[0] = g[0] = 1;
  if (mod1 == -1) {
    for (int i = 1; i <= n; ++i)
      puts("1");
    return 0;
  }
  if (mod1 == 0) {
    if (s[n] == '0') {
      for (int i = 1; i < n; ++i) {
        if (s[i] == '0')
          puts("1");
        else
          puts("0");
      }
      puts("2");
    } else {
      for (int i = 1; i < n; ++i)
        puts("2");
      puts("0");
    }
    return 0;
    // for(int i = 1; i <= n; ++i) f[i] = f[i-1] * 2 % mod;
  }

  for (int i = 1; i <= n; ++i)
    f[i] = f[i - 1] * 2 % mod;
  for (int i = 1; i <= n; ++i)
    g[i] = g[i - 1] * 2 % mod1;

  ll res = 0, res1 = 0;
  for (int i = 1; i <= n; ++i)
    if (s[i] == '1') {
      res = (res + f[n - i]) % mod;
      // printf("i:%d %lld\\n", i, res);
      res1 = (res1 + g[n - i]) % mod1;
    }

  // printf("res:%lld res1:%lld\\n", res, res1);

  for (int i = 1; i <= n; ++i) {
    ll ans;
    if (s[i] == '1') {
      if (mod1 == 0) {
        puts("0");
        continue;
      }

      ans = ((res1 - g[n - i]) % mod1 + mod1) % mod1;
      run(ans);
    } else {
      if (mod == 0) {
        puts("0");
        continue;
      }
      ans = (res + f[n - i]) % mod;
      run(ans);
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i <= (b); ++i)
#define per(i, a, b) for (int i = a; i >= (b); --i)
#define mem(a, x) memset(a, x, sizeof(a))
#define pb emplace_back
#define pii pair<int, int>
#define mk make_pair
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll read() {
  ll x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return w == 1 ? x : -x;
}
const int N = 1e6 + 10;
char s[N];
int n;
ll f[N], g[N], mod, mod1;
ll f1(ll x) {
  ll ans = 0;
  while (x) {
    if (x & 1)
      ans++;
    x = x / 2;
  }
  return ans;
}
void run(ll x) {
  ll ans = 1;

  while (x) {
    ll mod = f1(x);
    // printf("mod:%lld\\n", mod);
    x = x % mod;
    ans++;
  }
  printf("%lld\\n", ans);
}
int main() {
  // run(10);

  n = read();
  scanf("%s", s + 1);
  ll sum = 0;
  for (int i = 1; i <= n; ++i)
    sum += s[i] - '0';
  mod = sum + 1;
  mod1 = sum - 1;
  f[0] = g[0] = 1;
  for (int i = 1; i <= n; ++i)
    f[i] = f[i - 1] * 2 % mod;
  for (int i = 1; i <= n; ++i)
    g[i] = g[i - 1] * 2 % mod1;

  ll res = 0, res1 = 0;
  for (int i = 1; i <= n; ++i)
    if (s[i] == '1') {
      res = (res + f[n - i]) % mod;
      // printf("i:%d %lld\\n", i, res);
      res1 = (res1 + g[n - i]) % mod1;
    }

  // printf("res:%lld res1:%lld\\n", res, res1);

  for (int i = 1; i <= n; ++i) {
    ll ans;
    if (s[i] == '1') {
      if (mod1 == 0) {
        puts("0");
        continue;
      }

      ans = (res1 - g[n - i] + mod1) % mod1;
      //            if(i == 3){
      //                printf("ans:%lld g:%lld\\n", ans, g[n - i]);
      //            }
      run(ans);
    } else {
      ans = (res + f[n - i]) % mod;
      // printf("i:%d ans:%lld\\n", i, ans);

      run(ans);
    }
    // printf("%lld\\n", ans);
  }
}