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

#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const ll mod = 1000000007;
const ll INF = 1001001001;
const ll LINF = 1001001001001001001;

void prvec(vector<ll> vec) {
  ll n = vec.size();
  rep(i, n) cout << i << " " << vec.at(i) << "\\n";
}

void pr2d(vector<vector<ll>> vvec) {
  ll h = vvec.size();
  ll w = vvec.at(0).size();
  rep(i, h) {
    rep(j, w) { cout << vvec.at(i).at(j) << " "; }
    cout << "\\n";
  }
}

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

ll binary_divide_mod(string s, ll x) {
  if (x <= 0)
    return INF;
  ll ss = s.size();
  ll z = 0;
  rep(i, ss) {
    z = 2 * z + (s.at(i) - '0');
    if (z < x)
      continue;
    z -= x;
  }
  return z;
}

int main() {
  // 下準備
  vector<ll> lst(200002);
  for (int i = 0; i <= 200001; i++) {
    ll x = i;
    ll count = 0;
    while (x != 0) {
      bitset<20> b(x);
      ll y = b.count();
      x = x % y;
      count++;
    }
    // bitset<20> bb(i);
    // cout << i << " " << bb << " " << count << endl;
    lst.at(i) = count;
  }

  ll n;
  cin >> n;
  string x;
  cin >> x;
  ll xs = x.size();
  ll cnt_1 = 0;
  rep(i, xs) if (x.at(i) == '1') cnt_1++;

  ll plus = cnt_1 + 1, minus = cnt_1 - 1;

  // 2進割り算;
  ll mplus = binary_divide_mod(x, plus);
  ll mminus = binary_divide_mod(x, minus);

  rep(i, n) {
    bool pORm = (x.at(i) - '0');
    ll ans;
    if (pORm) { // 上からi桁目が1 つまり1から0に反転する;
      // X-2^(N-i) % A-1を求めたい
      if (mminus == INF) {
        cout << 0 << endl;
        continue;
      }
      ll amari = (mminus - modpow(2, n - i - 1, minus)) % minus;
      if (amari < 0)
        amari += minus;
      ans = 1 + lst.at(amari);
    } else { // 上からi桁目が0 つまり0から1に反転する;
      // X+2^(N-i) % A+1 を求めたい
      ll amari = (mplus + modpow(2, n - i - 1, plus)) % plus;
      ans = 1 + lst.at(amari);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const ll mod = 1000000007;
const ll INF = 1001001001;
const ll LINF = 1001001001001001001;

void prvec(vector<ll> vec) {
  ll n = vec.size();
  rep(i, n) cout << i << " " << vec.at(i) << "\\n";
}

void pr2d(vector<vector<ll>> vvec) {
  ll h = vvec.size();
  ll w = vvec.at(0).size();
  rep(i, h) {
    rep(j, w) { cout << vvec.at(i).at(j) << " "; }
    cout << "\\n";
  }
}

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

ll binary_divide_mod(string s, ll x) {
  ll ss = s.size();
  ll z = 0;
  rep(i, ss) {
    z = 2 * z + (s.at(i) - '0');
    if (z < x)
      continue;
    z -= x;
  }
  return z;
}

int main() {
  // 下準備
  vector<ll> lst(200001);
  for (int i = 0; i <= 200000; i++) {
    ll x = i;
    ll count = 0;
    while (x != 0) {
      bitset<20> b(x);
      ll y = b.count();
      x = x % y;
      count++;
    }
    // bitset<20> bb(i);
    // cout << i << " " << bb << " " << count << endl;
    lst.at(i) = count;
  }

  ll n;
  cin >> n;
  string x;
  cin >> x;
  ll xs = x.size();
  ll cnt_1 = 0;
  rep(i, xs) if (x.at(i) == '1') cnt_1++;

  ll plus = cnt_1 + 1, minus = cnt_1 - 1;

  // 2進割り算;
  ll mplus = binary_divide_mod(x, plus);
  ll mminus = binary_divide_mod(x, minus);

  rep(i, n) {
    bool pORm = (x.at(i) - '0');
    ll ans;
    if (pORm) { // 上からi桁目が1 つまり1から0に反転する;
      // X-2^(N-i) % A-1を求めたい
      ll amari = (mminus - modpow(2, n - i - 1, minus)) % minus;
      if (amari < 0)
        amari += minus;
      ans = 1 + lst.at(amari);
    } else { // 上からi桁目が0 つまり0から1に反転する;
      // X+2^(N-i) % A+1 を求めたい
      ll amari = (mplus + modpow(2, n - i - 1, plus)) % plus;
      ans = 1 + lst.at(amari);
    }
    cout << ans << endl;
  }
  return 0;
}