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

#include <bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef pair<int, int> p;
typedef long long ll;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define pmod 1000000007
#define INF 1000000000000000000
#define pi atan(1) * 4
#define modi 1000000

ll res = 0;

vector<ll> fun(200010, 0);

void solve() {

  ll n;

  string s;

  cin >> n;
  cin >> s;

  ll count = 0, count1, count2;

  ll me = 1;

  for (ll i = 0; i < n; i++) {
    if (s[i] == '1')
      count++;
  }

  count1 = count - 1;
  count2 = count + 1;

  ll me1 = 1, me2 = 1;

  ll total1 = 0, total2 = 0;

  ll val[n];

  for (ll i = n - 1; i >= 0; i--) {

    if (s[i] == '1') {

      if (count1 != 0)
        val[i] = me1 % count1;
      if (count1 != 0)
        total1 = (total1 + me1 % count1);
      total2 = (total2 + me2 % count2) % count2;
    } else {
      val[i] = -1 * (me2 % count2);
    }

    if (count1 != 0)
      me1 = (me1 + me1) % count1;
    me2 = (me2 + me2) % count2;
  }

  for (ll i = 0; i < n; i++) {

    if (s[i] == '1') {

      if (count1 != 0) {
        cout << fun[(total1 - val[i] + count1) % count1] + 1 << endl;
      } else {
        cout << 0 << endl;
      }

    } else {
      cout << fun[(total2 - val[i] + count2) % count2] + 1 << endl;
    }
  }
}

int main() {

  ifstream fin("text.in");
  ofstream fout("text.out");

  ll t = 1;

  ll count = 0, me;

  for (ll i = 1; i < 200000; i++) {

    count = 0, me = i;

    while (me) {

      if (me & 1)
        count++;
      me = (me >> 1);
    }

    fun[i] = fun[i % count] + 1;
  }

  while (t--)
    solve();

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef pair<int, int> p;
typedef long long ll;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define pmod 1000000007
#define INF 1000000000000000000
#define pi atan(1) * 4
#define modi 1000000

ll res = 0;

vector<ll> fun(200010, 0);

void solve() {

  ll n;

  string s;

  cin >> n;
  cin >> s;

  ll count = 0, count1, count2;

  ll me = 1;

  for (ll i = 0; i < n; i++) {
    if (s[i] == '1')
      count++;
  }

  count1 = count - 1;
  count2 = count + 1;

  ll me1 = 1, me2 = 1;

  ll total1 = 0, total2 = 0;

  ll val[n];

  for (ll i = n - 1; i >= 0; i--) {

    if (s[i] == '1') {
      val[i] = me1 % count1;

      total1 = (total1 + me1 % count1);
      total2 = (total2 + me2 % count2) % count2;
    } else {
      val[i] = -1 * (me2 % count2);
    }

    me1 = (me1 + me1) % count1;
    me2 = (me2 + me2) % count2;
  }

  for (ll i = 0; i < n; i++) {

    if (s[i] == '1') {

      if (count1 != 0) {
        cout << fun[(total1 - val[i] + count1) % count1] + 1 << endl;
      } else {
        cout << 0 << endl;
      }

    } else {
      cout << fun[(total2 - val[i] + count2) % count2] + 1 << endl;
    }
  }
}

int main() {

  ifstream fin("text.in");
  ofstream fout("text.out");

  ll t = 1;

  ll count = 0, me;

  for (ll i = 1; i < 200000; i++) {

    count = 0, me = i;

    while (me) {

      if (me & 1)
        count++;
      me = (me >> 1);
    }

    fun[i] = fun[i % count] + 1;
  }

  while (t--)
    solve();

  return 0;
}