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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (n); i++)
typedef pair<ll, ll> P;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

ll mod = (ll)(1e9 + 7);

ll solve(ll x, ll cnt) {

  ll n = x;
  ll res = 0;
  ll tmp = cnt;
  while (true) {
    if (n % tmp == 0) {
      res++;
      break;
    } else {
      n = n % tmp;
      tmp = __builtin_popcount(n);
      res++;
    }
  }

  return res;
}

int main() {
  ll n;
  cin >> n;

  string x;
  cin >> x;
  bool flag = false;

  ll cnt = 0;
  rep(i, n) {
    if (x[i] == '1') {
      cnt++;
    } else {
      continue;
    }
  }

  if (cnt == 1) {
    flag = true;
  }

  vector<ll> add(n, 0);
  vector<ll> sub(n, 0);
  ll base = 0;
  ll two_add = 1;
  ll two_sub = 1;
  rep(i, n) {
    add[n - i - 1] = two_add % (cnt + 1);
    two_add *= 2;
    two_add %= cnt + 1;
  }

  if (flag == false) {
    rep(i, n) {
      sub[n - i - 1] = two_sub % (cnt - 1);
      two_sub *= 2;
      two_sub %= cnt - 1;
    }
  }

  ll sum_add = 0;
  ll sum_sub = 0;
  rep(i, n) {
    if (x[i] == '1') {
      sum_add += add[i];
      sum_sub += sub[i];
    } else {
      continue;
    }
  }

  rep(i, n) {
    // sub
    if (x[i] == '1') {
      if (flag) {
        cout << 0 << endl;
        continue;
      }
      ll tmp = sum_sub;
      tmp -= sub[i];
      tmp %= cnt - 1;
      // cout << sum_sub << "  " << tmp << endl;
      cout << solve(tmp, cnt - 1) << endl;
    } else {
      // add
      ll tmp = sum_add;
      tmp += add[i];
      tmp %= cnt + 1;
      // cout << sum_add << "  " <<tmp << endl;
      cout << solve(tmp, cnt + 1) << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (n); i++)
typedef pair<ll, ll> P;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

ll mod = (ll)(1e9 + 7);
vector<ll> dic(200005, -1);

ll solve(ll x, ll cnt) {

  ll n = x;
  ll res = 0;
  ll tmp = cnt;
  while (true) {
    if (n % tmp == 0) {
      res++;
      break;
    } else {
      n = n % tmp;
      tmp = __builtin_popcount(n);
      res++;
    }
  }

  return res;
}

int main() {
  ll n;
  cin >> n;

  string x;
  cin >> x;

  ll cnt = 0;
  rep(i, n) {
    if (x[i] == '1') {
      cnt++;
    } else {
      continue;
    }
  }

  vector<ll> add(n, 0);
  vector<ll> sub(n, 0);
  ll base = 0;
  ll two_add = 1;
  ll two_sub = 1;
  rep(i, n) {
    add[n - i - 1] = two_add % (cnt + 1);
    sub[n - i - 1] = two_sub % (cnt - 1);
    two_add *= 2;
    two_sub *= 2;
    two_add %= cnt + 1;
    two_sub %= cnt - 1;
  }

  ll sum_add = 0;
  ll sum_sub = 0;
  rep(i, n) {
    if (x[i] == '1') {
      sum_add += add[i];
      sum_sub += sub[i];
    } else {
      continue;
    }
  }

  rep(i, n) {
    // sub
    if (x[i] == '1') {
      ll tmp = sum_sub;
      tmp -= sub[i];
      tmp %= cnt - 1;
      // cout << sum_sub << "  " << tmp << endl;
      cout << solve(tmp, cnt - 1) << endl;
    } else {
      // add
      ll tmp = sum_add;
      tmp += add[i];
      tmp %= cnt + 1;
      // cout << sum_add << "  " <<tmp << endl;
      cout << solve(tmp, cnt + 1) << endl;
    }
  }

  return 0;
}