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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;

#define MOD 1000000007
#define INF 1001001001
#define MIN -1001001001
#define rep(i, k, N) for (int i = k; i < N; i++)
#define MP make_pair
#define MT make_tuple // tie,make_tuple は別物
#define PB push_back
#define PF push_front
#define all(x) (x).begin(), (x).end()

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

ll calc(ll x, ll n) {
  if (x == 0)
    return n;

  ll c = x;
  ll ones = 0;
  rep(i, 0, 20) {
    if (((c >> i) % 2) == 1) {
      ones++;
    }
  }
  return calc(x % ones, n + 1);
}

int main() {
  ll N;
  cin >> N;
  string X;
  cin >> X;

  reverse(all(X));
  ll ones = 0;

  rep(i, 0, N) {
    if (X[i] == '1')
      ones++;
  }
  ll sum_a = 0, sum_b = 0;
  ll a, b;

  if (ones == 1) {
    a = 2;
    b = INF;
  } else {
    a = ones + 1;
    b = ones - 1;
  }
  ll now_a = 1, now_b = 1;
  rep(i, 0, N) {
    if (X[i] == '1') {
      sum_a += now_a;
      sum_a %= a;
      sum_b += now_b;
      sum_b %= b;
    }
    now_a *= 2;
    now_a %= a;
    now_b *= 2;
    now_b %= b;
  }

  now_a = 1, now_b = 1;
  v ans;
  rep(i, 0, N) {
    if (X[i] == '1') {
      if (b == INF)
        ans.push_back(0);
      else
        ans.push_back(calc((sum_b - now_b + b) % b, 1));
    } else {
      ans.push_back(calc((sum_a + now_a + a) % a, 1));
    }
    now_a *= 2;
    now_a %= a;
    now_b *= 2;
    now_b %= b;
  }

  rep(i, 0, N) cout << ans[N - 1 - i] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;

#define MOD 1000000007
#define INF 1001001001
#define MIN -1001001001
#define rep(i, k, N) for (int i = k; i < N; i++)
#define MP make_pair
#define MT make_tuple // tie,make_tuple は別物
#define PB push_back
#define PF push_front
#define all(x) (x).begin(), (x).end()

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

ll calc(ll x, ll n) {
  if (x == 0)
    return n;

  ll c = x;
  ll ones = 0;
  rep(i, 0, 20) {
    if (((c >> i) % 2) == 1) {
      ones++;
    }
  }
  return calc(x % ones, n + 1);
}

int main() {
  ll N;
  cin >> N;
  string X;
  cin >> X;

  reverse(all(X));
  ll ones = 0;

  rep(i, 0, N) {
    if (X[i] == '1')
      ones++;
  }
  ll sum_a = 0, sum_b = 0;
  ll a, b;
  if (ones == N) {
    a = -1;
    b = N - 1;
  } else if (ones == 1) {
    a = 2;
    b = INF;
  } else {
    a = ones + 1;
    b = ones - 1;
  }
  ll now_a = 1, now_b = 1;
  rep(i, 0, N) {
    if (X[i] == '1') {
      sum_a += now_a;
      sum_a %= a;
      sum_b += now_b;
      sum_b %= b;
    }
    now_a *= 2;
    now_a %= a;
    now_b *= 2;
    now_b %= b;
  }

  now_a = 1, now_b = 1;
  v ans;
  rep(i, 0, N) {
    if (X[i] == '1') {
      if (b == INF)
        ans.push_back(0);
      else
        ans.push_back(calc((sum_b - now_b + b) % b, 1));
    } else {
      ans.push_back(calc((sum_a + now_a + a) % a, 1));
    }
    now_a *= 2;
    now_a %= a;
    now_b *= 2;
    now_b %= b;
  }

  rep(i, 0, N) cout << ans[N - 1 - i] << endl;
  return 0;
}