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

/*
ID: alec3
LANG: C++14
PROG: frac1
CLASSIFICATION: flood fill
*/

#include <bits/stdc++.h>
#define check(x) cout << (#x) << ": " << x << " ";
#define io                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ss second
#define ff first
#define pb push_back
#define lb lower_bound
#define ub upper_bound

typedef long long ll;
typedef unsigned long long ull;

int pct(int x) { return __builtin_popcount(x); }

using namespace std;

int pwr(int a, ll b) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * 1LL * a);
    a = (a * 1LL * a);
    b >>= 1;
  }
  return ans;
}

const int N = 2e5 + 5;
int f[N];
int pw[3][N];

void solve() {
  int n;
  cin >> n;
  // loop through all numbers less than the length of s
  for (int i = 1; i <= n; i++) {
    // compute the number mod popcount
    int j = i % pct(i);
    // save f[i] as f[i % pct(i)] + 1
    f[i] = f[j] + 1;
    assert(j < i);
  }
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  int tot = 0;
  for (char ch : s) {
    // add one to tot if ch is 1
    tot += (ch == '1');
  }

  vector<int> mods;
  if (tot > 0)
    mods.pb(tot);
  if (tot > 1)
    mods.pb(tot - 1);
  if (tot < n)
    mods.pb(tot + 1);
  vector<int> val(mods.size());

  for (int i = 0; i < mods.size(); i++) {
    int md = mods[i];
    pw[i][0] = 1 % md;
    for (int j = 1; j <= n; j++) {
      pw[i][j] = (pw[i][j - 1] << 1) % md;
    }
    for (int j = 0; j < n; j++) {
      if (s[j] == '1')
        val[i] = (val[i] + pw[i][j]) % md;
    }
  }
  vector<int> ans;
  for (int i = 0; i < n; i++) {
    int cur = tot + (s[i] == '0') - (s[i] == '1');
    if (cur == 0) {
      ans.pb(0);
      continue;
    }
    int j = -1;
    for (int k = 0; k < mods.size(); k++) {
      if (mods[k] == cur)
        j = k;
    }
    assert(j != -1);
    int curval = val[j];
    if (s[i] == '0') {
      curval = (curval + pw[j][i]) % cur;
    } else {
      curval = (curval - pw[j][i] + cur) % cur;
    }
    ans.pb(1 + f[curval]);
  }
  reverse(ans.begin(), ans.end());
  for (int x : ans)
    cout << x << endl;
}

int main() {
  // ifstream fin("frac1.in");
  // ofstream fout("frac1.out");
  io;
  solve();

  return 0;
}


Error: 0

Fixed submission:

/*
ID: alec3
LANG: C++14
PROG: frac1
CLASSIFICATION: flood fill
*/

#include <bits/stdc++.h>
#define check(x) cout << (#x) << ": " << x << " ";
#define io                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ss second
#define ff first
#define pb push_back
#define lb lower_bound
#define ub upper_bound

typedef long long ll;
typedef unsigned long long ull;

int pct(int x) { return __builtin_popcount(x); }

using namespace std;

int pwr(int a, ll b) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * 1LL * a);
    a = (a * 1LL * a);
    b >>= 1;
  }
  return ans;
}

const int N = 2e5 + 5;
int f[N];
int pw[3][N];

void solve() {
  int n;
  cin >> n;
  // loop through all numbers less than the length of s
  for (int i = 1; i <= n; i++) {
    // compute the number mod popcount
    int j = i % pct(i);
    // save f[i] as f[i % pct(i)] + 1
    f[i] = f[j] + 1;
    // assert(j < i);
  }
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  int tot = 0;
  for (char ch : s) {
    // add one to tot if ch is 1
    tot += (ch == '1');
  }

  vector<int> mods;
  if (tot > 0)
    mods.pb(tot);
  if (tot > 1)
    mods.pb(tot - 1);
  if (tot < n)
    mods.pb(tot + 1);
  vector<int> val(mods.size());

  for (int i = 0; i < mods.size(); i++) {
    int md = mods[i];
    pw[i][0] = 1 % md;
    for (int j = 1; j <= n; j++) {
      pw[i][j] = (pw[i][j - 1] << 1) % md;
    }
    for (int j = 0; j <= n - 1; j++) {
      if (s[j] == '1')
        val[i] = (val[i] + pw[i][j]) % md;
    }
  }
  vector<int> ans;
  for (int i = 0; i < n; i++) {
    int cur = tot + (s[i] == '0') - (s[i] == '1');
    if (cur == 0) {
      ans.pb(0);
      continue;
    }
    int j = -1;
    for (int k = 0; k < mods.size(); k++) {
      if (mods[k] == cur)
        j = k;
    }
    // assert(j != -1);
    int curval = val[j];
    if (s[i] == '0') {
      curval = (curval + pw[i][j]) % cur;
    } else {
      curval = (curval - pw[j][i] + cur) % cur;
    }
    ans.pb(1 + f[curval]);
  }
  reverse(ans.begin(), ans.end());
  for (int x : ans)
    cout << x << endl;
}

int main() {
  // ifstream fin("frac1.in");
  // ofstream fout("frac1.out");
  io;
  solve();

  return 0;
}