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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ordered_set                                                            \\
  tree<long long, null_type, less<long long>, rb_tree_tag,                     \\
       tree_order_statistics_node_update>
// s.find_by_order(index) returns iterator to the index-th element
// in O(logn) time.

typedef unsigned long long ull;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vii;
typedef vector<string> vss;
typedef vector<pair<ll, ll>> vpll;
typedef unordered_map<ll, ll> mpll;
typedef unordered_map<int, int> mpii;
typedef unordered_map<char, ll> mpcl;
typedef pair<ll, ll> pll;
typedef complex<double> cd;
typedef vector<complex<double>> vcd;

#define fore(i, n) for (i = 0; i < n; i++)
#define forz(i, s, f) for (i = s; i < f; i++)
#define forer(i, n) for (i = n - 1; i >= 0; i--)
#define forzr(i, s, f) for (i = s; i >= f; i--)
#define mkp make_pair
#define pb push_back
#define eb emplace_back
#define ub upper_bound
#define lb lower_bound
#define ff first
#define ss second
#define pvec(i, vec)                                                           \\
  fore(i, vec.size()) cout << vec[i] << " ";                                   \\
  cout << "\\n";

const ll mod = 1e9 + 7;
const ll modulo = 998244353;
const double pi = 3.14159265358979323846;

ll power(ll x, ll y, ll m) // gives (x^y)%m
{
  ll ans = 1;
  while (y > 0) {
    if (y & 1) {
      ans = (ans * x) % m;
    }
    y = y / 2;
    x = (x * x) % m;
  }
  return ans;
}

bool sortbysec(pll &a, pll &b) { return (a.ss < b.ss); }

ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  else
    return (gcd(b % a, a));
}

ll lcm(ll a, ll b) {
  ll ans = (a * b) / gcd(a, b);
  return ans;
}

long double logn(long double a, long double n) { return (log(n) / log(a)); }

int Remainder(string str, int R) {
  // len is variable to store the
  // length of Number string.
  int len = str.length();

  int Num, Rem = 0;

  // loop that find Remainder
  for (int i = 0; i < len; i++) {

    Num = Rem * 2 + (str[i] - '0');
    Rem = Num % R;
  }

  // Return the remainder
  return Rem;
}

int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll te = 1, test, i, j, n, m, flag, k, p, temp;
  // cin>>te;
  for (test = 1; test <= te; test++) {
    cin >> n;
    string s;
    cin >> s;

    temp = 0;
    fore(i, n) {
      if (s[i] == '1')
        temp++;
    }

    ll plus, minus;
    plus = Remainder(s, temp + 1);
    if (temp > 1)
      minus = Remainder(s, temp - 1);
    vll ans(n);
    fore(i, n) {
      if (s[i] == '1') {
        if (temp == 1) {
          ans[i] = 0;
        } else {
          flag = (minus - power(2, (n - i - 1), (temp - 1)) + (temp - 1)) %
                 (temp - 1);
          ans[i] = 1;
          while (flag > 0) {
            p = __builtin_popcount(flag);
            flag = flag % p;
            ans[i]++;
          }
        }
      } else {
        flag = (plus + power(2, (n - i - 1), (temp + 1))) % (temp + 1);
        ans[i] = 1;
        while (flag > 0) {
          p = __builtin_popcount(flag);
          flag = flag % p;
          ans[i]++;
        }
      }
    }

    fore(i, n) { cout << ans[i] << "\\n"; }
    /*vll a(n);
    fore(i,n)
    {
            cin>>a[i];
    }*/

    // cout<<"hello\\n";
    // cout<<"Case #"<<test<<": ";
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ordered_set                                                            \\
  tree<long long, null_type, less<long long>, rb_tree_tag,                     \\
       tree_order_statistics_node_update>
// s.find_by_order(index) returns iterator to the index-th element
// in O(logn) time.

typedef unsigned long long ull;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vii;
typedef vector<string> vss;
typedef vector<pair<ll, ll>> vpll;
typedef unordered_map<ll, ll> mpll;
typedef unordered_map<int, int> mpii;
typedef unordered_map<char, ll> mpcl;
typedef pair<ll, ll> pll;
typedef complex<double> cd;
typedef vector<complex<double>> vcd;

#define fore(i, n) for (i = 0; i < n; i++)
#define forz(i, s, f) for (i = s; i < f; i++)
#define forer(i, n) for (i = n - 1; i >= 0; i--)
#define forzr(i, s, f) for (i = s; i >= f; i--)
#define mkp make_pair
#define pb push_back
#define eb emplace_back
#define ub upper_bound
#define lb lower_bound
#define ff first
#define ss second
#define pvec(i, vec)                                                           \\
  fore(i, vec.size()) cout << vec[i] << " ";                                   \\
  cout << "\\n";

const ll mod = 1e9 + 7;
const ll modulo = 998244353;
const double pi = 3.14159265358979323846;

ll power(ll x, ll y, ll m) // gives (x^y)%m
{
  ll ans = 1;
  while (y > 0) {
    if (y & 1) {
      ans = (ans * x) % m;
    }
    y = y / 2;
    x = (x * x) % m;
  }
  return ans;
}

bool sortbysec(pll &a, pll &b) { return (a.ss < b.ss); }

ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  else
    return (gcd(b % a, a));
}

ll lcm(ll a, ll b) {
  ll ans = (a * b) / gcd(a, b);
  return ans;
}

long double logn(long double a, long double n) { return (log(n) / log(a)); }

int Remainder(string str, int R) {
  // len is variable to store the
  // length of Number string.
  int len = str.length();

  int Num, Rem = 0;

  // loop that find Remainder
  for (int i = 0; i < len; i++) {

    Num = Rem * 2 + (str[i] - '0');
    Rem = Num % R;
  }

  // Return the remainder
  return Rem;
}

int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll te = 1, test, i, j, n, m, flag, k, p, temp;
  // cin>>te;
  for (test = 1; test <= te; test++) {
    cin >> n;
    string s;
    cin >> s;

    temp = 0;
    fore(i, n) {
      if (s[i] == '1')
        temp++;
    }

    vll ans(n);
    fore(i, n) {
      if (s[i] == '1')
        k = temp - 1;
      else
        k = temp + 1;
      s[i] = '1' - s[i] + '0';
      flag = Remainder(s, k);
      ans[i] = 1;
      while (flag > 0) {
        p = __builtin_popcount(flag);
        flag = flag % p;
        ans[i]++;
      }

      s[i] = '1' - s[i] + '0';
    }

    fore(i, n) { cout << ans[i] << "\\n"; }
    /*vll a(n);
    fore(i,n)
    {
            cin>>a[i];
    }*/

    // cout<<"hello\\n";
    // cout<<"Case #"<<test<<": ";
  }

  return 0;
}