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

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

#include <limits.h>

using namespace std;

typedef long long ll;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int calc_bit_count_for_string(const string &S, const int N) {
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      ans++;
    }
  }
  return ans;
}

int calc_bit_count_for_num(ll x) {
  assert(x >= 0);
  int ans = 0;
  while (x != 0) {
    if (x % 2 == 1) {
      ans++;
    }
    x >>= 1;
  }
  return ans;
}

ll mod_pow(const ll base, const ll n, const int mod) {
  assert(n >= 0);
  assert(mod > 0);
  if (n == 0) {
    return 1LL % mod;
  }

  ll ans = mod_pow(base, n / 2, mod);
  ans = (ans * ans) % mod;
  if (n % 2 == 1) {
    ans = (ans * base) % mod;
  }
  return ans;
}

ll calc_first(const int mod, const string &S, const int N) {
  assert(mod > 0);

  ll ans = 0;
  for (int i = 0; i < N; i++) {
    ans = (((ans * 2) % mod) + (S[i] - '0')) % mod;
  }
  return ans;
}

// 11:53
int main(void) {
  int N;
  cin >> N;
  string S;
  cin >> S;

  vector<int> ans(N, 0);

  // for zero
  {
    const int mod = calc_bit_count_for_string(S, N) + 1;

    ll original = calc_first(mod, S, N);
    for (int i = 0; i < N; i++) {
      if (S[i] == '0') {
        ll acc = original;
        int cnt = 0;
        acc = (acc + mod_pow(2, N - 1 - i, mod)) % mod;
        // printf("i: %d, mod: %d, acc: %lld\\n", i, mod, acc);
        cnt++;
        while (acc > 0) {
          acc = acc % calc_bit_count_for_num(acc);
          // printf("i: %d, mod: %d, acc: %lld\\n", i, mod, acc);
          cnt++;
        }
        ans[i] = cnt;
      }
    }
  }

  // for one
  {
    const int mod = calc_bit_count_for_string(S, N) - 1;
    if (mod > 0) {
      ll original = calc_first(mod, S, N);
      for (int i = 0; i < N; i++) {
        if (S[i] == '1') {
          ll acc = original;
          int cnt = 0;
          acc = (acc + mod - mod_pow(2, N - 1 - i, mod)) % mod;
          // printf("i: %d, mod: %d, acc: %lld\\n", i, mod, acc);
          cnt++;
          while (acc > 0) {
            acc = acc % calc_bit_count_for_num(acc);
            // printf("i: %d, mod: %d, acc: %lld\\n", i, mod, acc);
            cnt++;
          }
          ans[i] = cnt;
        }
      }
    }
  }

  for (int i = 0; i < N; i++) {
    printf("%d\\n", ans[i]);
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

#include <limits.h>

using namespace std;

typedef long long ll;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int calc_bit_count_for_string(const string &S, const int N) {
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      ans++;
    }
  }
  return ans;
}

int calc_bit_count_for_num(ll x) {
  assert(x >= 0);
  int ans = 0;
  while (x != 0) {
    if (x % 2 == 1) {
      ans++;
    }
    x >>= 1;
  }
  return ans;
}

ll mod_pow(const ll base, const ll n, const int mod) {
  assert(n >= 0);
  assert(mod > 0);
  if (n == 0) {
    return 1LL % mod;
  }

  ll ans = mod_pow(base, n / 2, mod);
  ans = (ans * ans) % mod;
  if (n % 2 == 1) {
    ans = (ans * base) % mod;
  }
  return ans;
}

ll calc_first(const int mod, const string &S, const int N) {
  assert(mod > 0);

  ll ans = 0;
  for (int i = 0; i < N; i++) {
    ans = (((ans * 2) % mod) + (S[i] - '0')) % mod;
  }
  return ans;
}

// 11:53
int main(void) {
  int N;
  cin >> N;
  string S;
  cin >> S;

  vector<int> ans(N, 0);

  // for zero
  {
    const int mod = calc_bit_count_for_string(S, N) + 1;

    ll original = calc_first(mod, S, N);
    for (int i = 0; i < N; i++) {
      if (S[i] == '0') {
        ll acc = original;
        int cnt = 0;
        acc = (acc + mod_pow(2, N - 1 - i, mod)) % mod;
        // printf("i: %d, mod: %d, acc: %lld\\n", i, mod, acc);
        cnt++;
        while (acc > 0) {
          acc = acc % calc_bit_count_for_num(acc);
          // printf("i: %d, mod: %d, acc: %lld\\n", i, mod, acc);
          cnt++;
        }
        ans[i] = cnt;
      }
    }
  }

  // for one
  {
    const int mod = calc_bit_count_for_string(S, N) - 1;
    if (mod != 0) {
      ll original = calc_first(mod, S, N);
      for (int i = 0; i < N; i++) {
        if (S[i] == '1') {
          ll acc = original;
          int cnt = 0;
          acc = (acc + mod - mod_pow(2, N - 1 - i, mod)) % mod;
          // printf("i: %d, mod: %d, acc: %lld\\n", i, mod, acc);
          cnt++;
          while (acc > 0) {
            acc = acc % calc_bit_count_for_num(acc);
            // printf("i: %d, mod: %d, acc: %lld\\n", i, mod, acc);
            cnt++;
          }
          ans[i] = cnt;
        }
      }
    }
  }

  for (int i = 0; i < N; i++) {
    printf("%d\\n", ans[i]);
  }

  return 0;
}