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

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define MOD 998244353
#define INF 1000000010
#define EPS 1e-9
#define F first
#define S second

#define debug(x) cout << x << endl;
#define repi(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) repi(i, 0, n)
#define lp(i, n) repi(i, 0, n)
#define repn(i, n) for (int i = n; i >= 0; i--)
// #define int long long
#define endl "\\n"

typedef pair<int, int> PII;
typedef pair<int, string> PIS;
typedef pair<string, int> PSI;

signed main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);
  int n;
  string x;
  cin >> n >> x;
  int totalpop = 0;
  int modlow, modhigh;
  int lowsum = 0, highsum = 0;
  repn(i, n - 1) {
    if (x[i] == '1') {
      totalpop++;
    }
  }
  modlow = totalpop - 1;
  modhigh = totalpop + 1;
  // if(totalpop==1) modlow=1;
  int table[totalpop + 5];
  table[0] = 0;
  repi(i, 1, totalpop + 5) {
    int xx = 0, X = i;
    while (X != 0) {
      if (X % 2 == 1)
        xx++;
      X /= 2;
    }
    table[i] = i % xx;
  }
  int lownow = 1, highnow = 1;
  // cout<<x<<endl;
  repn(i, n - 1) {
    if (x[i] == '1') {
      if (modlow != 0) {
        lowsum += (lownow % modlow);
        lowsum %= modlow;
      }
      highsum += (highnow % modhigh);
      highsum %= modhigh;
      // cout<<lowsum<<" "<<highsum<<endl;
    }

    lownow *= 2;
    highnow *= 2;
    if (modlow != 0)
      lownow %= modlow;
    highnow %= modhigh;
  }
  // cout<<lowsum<<" "<<highsum<<endl;
  lownow = 1, highnow = 1;
  int now = 1;
  vector<int> anss;
  repn(i, n - 1) {
    int num = 0;
    if (x[i] == '0') {
      num = highsum;
      num += (highnow % modhigh);
      num %= modhigh;
    } else {
      num = lowsum;
      if (modlow != 0) {
        num -= (lownow % modlow);
        while (num < 0) {

          num += modlow;
        }
        num %= modlow;
      }
    }
    // cout<<num<<endl;
    int ans = 0, nextmod = 0;
    while (num != 0) {
      // cout<<'a'<<endl;
      num = table[num];
      ans++;
    }
    if (x[i] == '1' && modlow == 0)
      ans = -1;
    anss.push_back(ans + 1);

    lownow *= 2;
    highnow *= 2;
    if (modlow != 0)
      lownow %= modlow;
    highnow %= modhigh;
  }
  repn(i, n - 1) { cout << anss[i] << endl; }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1000000010
#define EPS 1e-9
#define F first
#define S second

#define debug(x) cout << x << endl;
#define repi(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) repi(i, 0, n)
#define lp(i, n) repi(i, 0, n)
#define repn(i, n) for (int i = n; i >= 0; i--)
// #define int long long
#define endl "\\n"

typedef pair<int, int> PII;
typedef pair<int, string> PIS;
typedef pair<string, int> PSI;

int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);
  int n;
  string x;
  cin >> n >> x;
  int totalpop = 0;
  int modlow, modhigh;
  int lowsum = 0, highsum = 0;
  repn(i, n - 1) {
    if (x[i] == '1') {
      totalpop++;
    }
  }
  modlow = totalpop - 1;
  modhigh = totalpop + 1;

  int table[totalpop + 5];
  table[0] = 0;
  repi(i, 1, totalpop + 5) {
    int xx = 0, X = i;
    while (X != 0) {
      if (X % 2 == 1)
        xx++;
      X /= 2;
    }
    table[i] = i % xx;
  }
  int lownow = 1, highnow = 1;
  // cout<<x<<endl;
  repn(i, n - 1) {
    if (x[i] == '1') {
      lowsum += (lownow % modlow);
      lowsum %= modlow;
      highsum += (highnow % modhigh);
      highsum %= modhigh;
      // cout<<lowsum<<" "<<highsum<<endl;
    }

    lownow *= 2;
    highnow *= 2;
    lownow %= modlow;
    highnow %= modhigh;
  }
  // cout<<lowsum<<" "<<highsum<<endl;
  lownow = 1, highnow = 1;
  int now = 1;
  vector<int> anss;
  repn(i, n - 1) {
    int num = 0;
    if (x[i] == '0') {
      num = highsum;
      num += (highnow % modhigh);
      num %= modhigh;
    } else {
      num = lowsum;
      num -= (lownow % modlow);
      while (num < 0) {

        num += modlow;
      }
      num %= modlow;
    }
    // cout<<num<<endl;
    int ans = 0, nextmod = 0;
    while (num != 0) {
      // cout<<'a'<<endl;
      num = table[num];
      ans++;
    }
    anss.push_back(ans + 1);

    lownow *= 2;
    highnow *= 2;
    lownow %= modlow;
    highnow %= modhigh;
  }
  repn(i, n - 1) { cout << anss[i] << endl; }

  return 0;
}