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

#define ll long long
#define dd long double
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mp make_pair
#define mt make_tuple
#define fo(i, n) for (ll i = 0; i < n; i++)
// #include<bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
dd pi = acos(-1);
ll z = 1000000007;
ll inf = 100000000000000000;
ll p1 = 37;
ll p2 = 53;
ll mod1 = 202976689;
ll mod2 = 203034253;
ll fact[100];
ll gdp(ll a, ll b) { return (a - (a % b)); }
ll ld(ll a, ll b) {
  if (a < 0)
    return -1 * gdp(abs(a), b);
  if (a % b == 0)
    return a;
  return (a + (b - a % b));
} // least number >=a divisible by b
ll gd(ll a, ll b) {
  if (a < 0)
    return (-1 * ld(abs(a), b));
  return (a - (a % b));
} // greatest number <= a divisible by b
ll gcd(ll a, ll b) {
  if (b > a)
    return gcd(b, a);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll e_gcd(ll a, ll b, ll &x, ll &y) {
  if (b > a)
    return e_gcd(b, a, y, x);
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll x1, y1;
  e_gcd(b, a % b, x1, y1);
  x = y1;
  y = (x1 - ((a / b) * y1));
  return e_gcd(b, a % b, x1, y1);
}
ll power(ll a, ll b, ll p) {
  if (b == 0)
    return 1;
  ll c = power(a, b / 2, p);
  if (b % 2 == 0)
    return ((c * c) % p);
  else
    return ((((c * c) % p) * a) % p);
}
ll inverse(ll a, ll n) { return power(a, n - 2, n); }
ll max(ll a, ll b) {
  if (a > b)
    return a;
  return b;
}
ll min(ll a, ll b) {
  if (a < b)
    return a;
  return b;
}
ll left(ll i) { return ((2 * i) + 1); }
ll right(ll i) { return ((2 * i) + 2); }
ll ncr(ll n, ll r) {
  if (n < r || (n < 0) || (r < 0))
    return 0;
  return ((((fact[n] * inverse(fact[r], z)) % z) * inverse(fact[n - r], z)) %
          z);
}
void swap(ll &a, ll &b) {
  ll c = a;
  a = b;
  b = c;
  return;
}
// ios_base::sync_with_stdio(0);
// cin.tie(0); cout.tie(0);
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(val)  no. of elements strictly less than val
// s.find_by_order(i)  itertor to ith element (0 indexed)
//__builtin_popcount(n) -> returns number of set bits in n
// ll seed;
// mt19937 rnd(seed=chrono::steady_clock::now().time_since_epoch().count()); //
// include bits
ll ans[200001];

void solve() {
  ll n;
  string x;
  cin >> n;
  cin >> x;

  ll curr = 0;
  fo(i, n) {
    if (x[i] == '1')
      curr++;
  }

  ll val_1 = 0, val_2 = 0, k_1 = 1, k_2 = 1;

  for (ll i = x.size() - 1; i >= 0; i--) {
    val_1 += (x[i] - '0') * (k_1);
    val_2 += (x[i] - '0') * (k_2);

    k_2 *= 2;
    if (curr > 1) {
      k_1 *= 2;
      k_1 %= (curr - 1);
      val_1 %= (curr - 1);
    }
    k_2 %= (curr + 1);
    val_2 %= (curr + 1);
  }
  vector<ll> anss;
  k_1 = 1;
  k_2 = 1;
  for (ll i = x.size() - 1; i >= 0; i--) {

    if (x[i] == '0') {
      ll val = (val_2 + k_2) % (curr + 1);
      anss.pub(1 + ans[val]);

    } else {
      if (curr == 1) {
        // cout << "here" << endl ;
        anss.pub(0);
        continue;
      }
      ll val = (val_1 - k_1) % (curr - 1);
      val += (curr - 1);
      val %= (curr - 1);
      anss.pub(1 + ans[val]);
    }

    k_2 *= 2;
    if (curr > 1) {
      k_1 *= 2;
      k_1 %= (curr - 1);
    }
    k_2 %= (curr + 1);
  }
  reverse(anss.begin(), anss.end());
  fo(i, anss.size()) {
    // anss[i] = ans[anss[i]] ;
    cout << anss[i] << '\\n';
  }
  return;
}

int main() {
  /* ios_base::sync_with_stdio(0);
   cin.tie(0); cout.tie(0);
   #ifndef ONLINE_JUDGE
   freopen("inputf.txt" , "r" , stdin) ;
   freopen("outputf.txt" , "w" , stdout) ;
   freopen("error.txt" , "w" , stderr) ;
   #endif*/

  ll t;
  // cin >> t ;
  t = 1;

  ans[0] = 0;
  for (ll i = 1; i <= 200000; i++) {
    ll setb = __builtin_popcountll(i);
    ll curr = i % setb;
    ans[i] = (1 + ans[curr]);
  }

  while (t--) {
    solve();
  }
  // cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC <<
  // "ms\\n";

  return 0;
}

Error: 0

Fixed submission:

#define ll long long
#define dd long double
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mp make_pair
#define mt make_tuple
#define fo(i, n) for (ll i = 0; i < n; i++)
// #include<bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
dd pi = acos(-1);
ll z = 1000000007;
ll inf = 100000000000000000;
ll p1 = 37;
ll p2 = 53;
ll mod1 = 202976689;
ll mod2 = 203034253;
ll fact[100];
ll gdp(ll a, ll b) { return (a - (a % b)); }
ll ld(ll a, ll b) {
  if (a < 0)
    return -1 * gdp(abs(a), b);
  if (a % b == 0)
    return a;
  return (a + (b - a % b));
} // least number >=a divisible by b
ll gd(ll a, ll b) {
  if (a < 0)
    return (-1 * ld(abs(a), b));
  return (a - (a % b));
} // greatest number <= a divisible by b
ll gcd(ll a, ll b) {
  if (b > a)
    return gcd(b, a);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll e_gcd(ll a, ll b, ll &x, ll &y) {
  if (b > a)
    return e_gcd(b, a, y, x);
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll x1, y1;
  e_gcd(b, a % b, x1, y1);
  x = y1;
  y = (x1 - ((a / b) * y1));
  return e_gcd(b, a % b, x1, y1);
}
ll power(ll a, ll b, ll p) {
  if (b == 0)
    return 1;
  ll c = power(a, b / 2, p);
  if (b % 2 == 0)
    return ((c * c) % p);
  else
    return ((((c * c) % p) * a) % p);
}
ll inverse(ll a, ll n) { return power(a, n - 2, n); }
ll max(ll a, ll b) {
  if (a > b)
    return a;
  return b;
}
ll min(ll a, ll b) {
  if (a < b)
    return a;
  return b;
}
ll left(ll i) { return ((2 * i) + 1); }
ll right(ll i) { return ((2 * i) + 2); }
ll ncr(ll n, ll r) {
  if (n < r || (n < 0) || (r < 0))
    return 0;
  return ((((fact[n] * inverse(fact[r], z)) % z) * inverse(fact[n - r], z)) %
          z);
}
void swap(ll &a, ll &b) {
  ll c = a;
  a = b;
  b = c;
  return;
}
// ios_base::sync_with_stdio(0);
// cin.tie(0); cout.tie(0);
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(val)  no. of elements strictly less than val
// s.find_by_order(i)  itertor to ith element (0 indexed)
//__builtin_popcount(n) -> returns number of set bits in n
// ll seed;
// mt19937 rnd(seed=chrono::steady_clock::now().time_since_epoch().count()); //
// include bits
ll ans[200001];

void solve() {
  ll n;
  string x;
  cin >> n;
  cin >> x;

  ll curr = 0;
  fo(i, n) {
    if (x[i] == '1')
      curr++;
  }

  ll val_1 = 0, val_2 = 0, k_1 = 1, k_2 = 1;

  for (ll i = x.size() - 1; i >= 0; i--) {
    val_1 += (x[i] - '0') * (k_1);
    val_2 += (x[i] - '0') * (k_2);

    k_2 *= 2;
    if (curr > 1) {
      k_1 *= 2;
      k_1 %= (curr - 1);
      val_1 %= (curr - 1);
    }
    k_2 %= (curr + 1);
    val_2 %= (curr + 1);
  }
  vector<ll> anss;
  k_1 = 1;
  k_2 = 1;
  for (ll i = x.size() - 1; i >= 0; i--) {
    if (x[i] == '0') {
      ll val = (val_2 + k_2) % (curr + 1);
      anss.pub(val);
    } else {
      ll val = (val_1 - k_1) % (curr - 1);
      val += (curr - 1);
      val %= (curr - 1);
      anss.pub(val);
    }

    k_2 *= 2;
    if (curr > 1) {
      k_1 *= 2;
      k_1 %= (curr - 1);
    }
    k_2 %= (curr + 1);
  }
  reverse(anss.begin(), anss.end());
  fo(i, anss.size()) {
    anss[i] = ans[anss[i]];
    cout << anss[i] + 1 << '\\n';
  }
  return;
}

int main() {
  /*ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  #ifndef ONLINE_JUDGE
  freopen("inputf.txt" , "r" , stdin) ;
  freopen("outputf.txt" , "w" , stdout) ;
  freopen("error.txt" , "w" , stderr) ;
  #endif*/

  ll t;
  // cin >> t ;
  t = 1;

  ans[0] = 0;
  for (ll i = 1; i <= 200000; i++) {
    ll setb = __builtin_popcountll(i);
    ll curr = i % setb;
    ans[i] = (1 + ans[curr]);
  }

  while (t--) {
    solve();
  }
  // cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC <<
  // "ms\\n";

  return 0;
}