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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = (ll)1e9 + 7;
const ll INF = (ll)1e18 + 10;
const double PI = acos(-1);

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 (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int dy[] = {-1, 1, 0, 0};
int dx[] = {0, 0, -1, 1};

int ans[200010];
int mods[200010][2];

void calc_mods(int lim, int N, int type) {
  mods[0][type] = 1;
  if (N == 0)
    return;
  for (int i = 1; i < lim; ++i) {
    mods[i][type] = (mods[i - 1][type] << 1) % N;
  }
}

int calc_val(string S, int type, int mod) {
  int ret = 0;
  if (mod == 0)
    return 0;
  // reverse(S.begin(),S.end());
  for (int i = 0; i < S.length(); ++i) {
    if (S[i] == '1') {
      (ret += mods[i][type]) %= mod;
    }
  }
  return ret;
}

int calc(int a) {
  if (a == 0)
    return 0;
  int cnt = 0;
  int tmp = a;
  while (a > 0) {
    if (a & 1)
      ++cnt;
    a >>= 1;
  }

  return calc(tmp % cnt) + 1;
}

int main() {
  memset(ans, -1, sizeof(ans));
  int N;
  cin >> N;
  string S;
  cin >> S;
  int cnt_init = 0;
  for (auto c : S) {
    if (c == '1')
      ++cnt_init;
  }
  reverse(S.begin(), S.end());
  calc_mods(N, cnt_init - 1, 0);
  calc_mods(N, cnt_init + 1, 1);
  int val1_0 = calc_val(S, 0, cnt_init - 1);
  int val0_1 = calc_val(S, 1, cnt_init + 1);
  // cout<<val0_1<<endl;
  // return 0;
  vector<int> an;

  for (int i = 0; i < N; ++i) {
    if (S[i] == '1') { // 1to0
      int base = cnt_init - 1;
      if (base == 0) {
        an.push_back(0);
        continue;
      }
      int val = (val1_0 - mods[i][0] + base) % base;
      an.push_back(calc(val) + 1);
    } else {
      int base = cnt_init + 1;
      int val = (val0_1 + mods[i][1]) % base;
      // cout<<calc(val)+1<<endl;
      an.push_back(calc(val) + 1);
    }
  }
  // cout<<"----"<<endl;
  for (int i = an.size() - 1; i >= 0; --i) {
    cout << an[i] << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = (ll)1e9 + 7;
const ll INF = (ll)1e18 + 10;
const double PI = acos(-1);

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 (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int dy[] = {-1, 1, 0, 0};
int dx[] = {0, 0, -1, 1};

int ans[200010];
int mods[200010][2];

void calc_mods(int lim, int N, int type) {
  mods[0][type] = 1;
  for (int i = 1; i < lim; ++i) {
    mods[i][type] = (mods[i - 1][type] << 1) % N;
  }
}

ll modpow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      (res *= a) %= MOD;
    (a *= a) %= MOD;
    n >>= 1;
  }
  return res;
}

int calc_val(string S, int type, int mod) {
  int ret = 0;
  // reverse(S.begin(),S.end());
  for (int i = 0; i < S.length(); ++i) {
    if (S[i] == '1') {
      (ret += mods[i][type]) %= mod;
    }
  }
  return ret;
}

int calc(int a) {
  if (a == 0)
    return 0;
  int cnt = 0;
  int tmp = a;
  while (a > 0) {
    if (a & 1)
      ++cnt;
    a >>= 1;
  }

  return calc(tmp % cnt) + 1;
}

int main() {
  memset(ans, -1, sizeof(ans));
  int N;
  cin >> N;
  string S;
  cin >> S;
  int cnt_init = 0;
  for (auto c : S) {
    if (c == '1')
      ++cnt_init;
  }
  reverse(S.begin(), S.end());
  calc_mods(N, cnt_init - 1, 0);
  calc_mods(N, cnt_init + 1, 1);
  int val1_0 = calc_val(S, 0, cnt_init - 1);
  int val0_1 = calc_val(S, 1, cnt_init + 1);
  // cout<<val0_1<<endl;
  // return 0;
  vector<int> an;

  for (int i = 0; i < N; ++i) {
    if (S[i] == '1') { // 1to0
      int base = cnt_init - 1;
      int val = (val1_0 - mods[i][0] + base) % base;
      an.push_back(calc(val) + 1);
    } else {
      int base = cnt_init + 1;
      int val = (val0_1 + mods[i][1]) % base;
      // cout<<calc(val)+1<<endl;
      an.push_back(calc(val) + 1);
    }
  }
  // cout<<"----"<<endl;
  for (int i = an.size() - 1; i >= 0; --i) {
    cout << an[i] << endl;
  }

  return 0;
}