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

/*
    Author:zeke

    pass System Test!
    GET AC!!
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using ll = long long;
using ld = long double;
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rep3(var, min, max) for (ll(var) = (min); (var) < (max); ++(var))
#define repi3(var, min, max) for (ll(var) = (max)-1; (var) + 1 > (min); --(var))
#define Mp(a, b) make_pair((a), (b))
#define F first
#define S second
#define Icin(s)                                                                \\
  ll(s);                                                                       \\
  cin >> (s);
#define Scin(s)                                                                \\
  ll(s);                                                                       \\
  cin >> (s);
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef vector<V> VV;
typedef vector<P> VP;
ll mod = 1e9 + 7;
ll MOD = 1e9 + 7;
ll INF = 1e18;
// cout << "Case #" << index << " :IMPOSSIBLE";
long long modMult(long long a, long long b, long long m) {
  long long res = 0;
  long long exp = a % m;
  while (b) {
    if (b & 1) {
      res += exp;
      if (res > m)
        res -= m;
    }
    exp <<= 1;
    if (exp > m)
      exp -= m;
    b >>= 1;
  }
  return res;
}
long long powMod(long long a, long long b, long long m) {
  long long res = 1;
  long long exp = a % m;
  while (b) {
    if (b & 1)
      res = modMult(res, exp, m);
    exp = modMult(exp, exp, m);
    b >>= 1;
  }
  return res;
}
// powMod(a,b,MOD)
// O(log(b))?
ll powzeke(ll x, ll y, ll z) {
  ll temp = 1;
  rep(i, y) {
    temp *= x;
    if (temp >= z) {
      return -1;
    }
  }
  return temp;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll sum1 = 0;
  ll sum2 = 0;
  ll num = 0;
  rep(i, n) {
    if (s[i] - '0') {
      num++;
    }
  }
  bool f2 = false;
  ll NUM1 = num - 1;
  ll NUM2 = num + 1;
  if (NUM1 == 0) {
    f2 = true;
    NUM1 = 2;
  }
  reverse(all(s));
  rep(i, n) {
    sum1 += (s[i] - '0') * powMod(2, i, NUM1);
    sum2 += (s[i] - '0') * powMod(2, i, NUM2);
    sum1 %= NUM1;
    sum2 %= NUM2;
  }
  V finalres;
  // cout<<sum<<endl;
  rep(i, n) {
    // cout<<i<<endl;
    if (f2 && s[i] == '1') {
      finalres.push_back(0);
      continue;
    }
    bool f = true;
    ll freg = 0;
    rep(j, n) {
      if (i == j) {
        if (s[j] == '0') {
          if (powzeke(2, j, num + 1) == -1) {
            f = false;
            break;
          }
          freg += powzeke(2, j, num + 1);
        }
      } else {
        if (powzeke(2, j, num + 1) == -1) {
          f = false;
          break;
        }
        freg += (s[j] - '0') * powzeke(2, j, num + 1);
      }
      if (!f) {
        break;
      }
      if ((freg >= NUM1 && s[i] == '1') || (freg >= NUM2 && s[i] == '0')) {
        // cout<<i<<" "<<"over"<<" "<<freg<<endl;
        f = false;
        break;
      }
    }
    // cout<<"hello"<<endl;
    if (s[i] == '1') {
      ll temp = sum1;
      temp += NUM1;
      temp -= powMod(2, i, NUM1);
      temp %= NUM1;
      ll res = 0;
      while (temp != 0) {
        temp = temp % (__builtin_popcount(temp));
        res++;
      }
      // cout<<" "<<res<<endl;
      if (!f)
        res++;
      finalres.push_back(res);
    } else {
      ll temp = sum2;
      temp += NUM2;
      if (s[i] == '0') {
        temp += powMod(2, i, NUM2);
      } else {
        temp -= powMod(2, i, NUM2);
      }
      temp %= NUM2;
      // cout<<" "<<temp<<endl;
      ll res = 0;
      while (temp != 0) {
        temp = temp % (__builtin_popcount(temp));
        res++;
      }
      // cout<<i<<" "<<res<<endl;
      if (!f)
        res++;
      finalres.push_back(res);
    }

    // cout<<res<<endl;
  }
  reverse(all(finalres));
  ll k = 1;
  for (auto i : finalres) {
    cout << i << endl;
    k++;
  }
}


Error: 0

Fixed submission:

/*
    Author:zeke

    pass System Test!
    GET AC!!
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using ll = long long;
using ld = long double;
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rep3(var, min, max) for (ll(var) = (min); (var) < (max); ++(var))
#define repi3(var, min, max) for (ll(var) = (max)-1; (var) + 1 > (min); --(var))
#define Mp(a, b) make_pair((a), (b))
#define F first
#define S second
#define Icin(s)                                                                \\
  ll(s);                                                                       \\
  cin >> (s);
#define Scin(s)                                                                \\
  ll(s);                                                                       \\
  cin >> (s);
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef vector<V> VV;
typedef vector<P> VP;
ll mod = 1e9 + 7;
ll MOD = 1e9 + 7;
ll INF = 1e18;
// cout << "Case #" << index << " :IMPOSSIBLE";
long long modMult(long long a, long long b, long long m) {
  long long res = 0;
  long long exp = a % m;
  while (b) {
    if (b & 1) {
      res += exp;
      if (res > m)
        res -= m;
    }
    exp <<= 1;
    if (exp > m)
      exp -= m;
    b >>= 1;
  }
  return res;
}
long long powMod(long long a, long long b, long long m) {
  long long res = 1;
  long long exp = a % m;
  while (b) {
    if (b & 1)
      res = modMult(res, exp, m);
    exp = modMult(exp, exp, m);
    b >>= 1;
  }
  return res;
}
// powMod(a,b,MOD)
// O(log(b))?
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll sum1 = 0;
  ll sum2 = 0;
  ll num = 0;
  rep(i, n) {
    if (s[i] - '0') {
      num++;
    }
  }
  bool f2 = false;
  ll NUM1 = num - 1;
  ll NUM2 = num + 1;
  if (NUM1 == 0) {
    f2 = true;
    //  NUM1=2;
  }
  reverse(all(s));
  rep(i, n) {
    sum1 += (s[i] - '0') * powMod(2, i, NUM1);
    sum2 += (s[i] - '0') * powMod(2, i, NUM2);
    sum1 %= NUM1;
    sum2 %= NUM2;
  }
  V finalres;
  // cout<<sum<<endl;
  rep(i, n) {
    // cout<<i<<endl;
    if (f2 && s[i] == '1') {
      finalres.push_back(0);
      continue;
    }
    bool f = true;
    ll freg = 0;
    rep(j, n) {
      if (i == j) {
        if (s[j] == '0') {
          freg += pow(2, j);
        }
      } else {
        freg += (s[j] - '0') * pow(2, j);
      }
      if ((freg >= NUM1 && s[i] == '1') || (freg >= NUM2 && s[i] == '0')) {
        // cout<<i<<" "<<"over"<<" "<<freg<<endl;
        f = false;
        break;
      }
    }
    // cout<<"hello"<<endl;
    if (s[i] == '1') {
      ll temp = sum1;
      temp += NUM1;
      temp -= powMod(2, i, NUM1);
      temp %= NUM1;
      ll res = 0;
      while (temp != 0) {
        temp = temp % (__builtin_popcount(temp));
        res++;
      }
      // cout<<" "<<res<<endl;
      if (!f)
        res++;
      finalres.push_back(res);
    } else {
      ll temp = sum2;
      temp += NUM2;
      if (s[i] == '0') {
        temp += powMod(2, i, NUM2);
      } else {
        temp -= powMod(2, i, NUM2);
      }
      temp %= NUM2;
      // cout<<" "<<temp<<endl;
      ll res = 0;
      while (temp != 0) {
        temp = temp % (__builtin_popcount(temp));
        res++;
      }
      // cout<<i<<" "<<res<<endl;
      if (!f)
        res++;
      finalres.push_back(res);
    }

    // cout<<res<<endl;
  }
  reverse(all(finalres));
  ll k = 1;
  for (auto i : finalres) {
    cout << i << endl;
    k++;
  }
}