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

/*
 ALLAH is Almighty....
*/
#include <bits/stdc++.h>
using namespace std;

#define pi acos(-1.0)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);                                                              \\
  cout << setprecision(12)
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define per(i, a, n) for (ll i = n - 1; i >= a; i--)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((ll)(x).size())
#define cin1(x) cin >> x
#define cin2(x, y) cin >> x >> y
#define cin3(x, y, z) cin >> x >> y >> z
#define cin4(x, y, z, _) cin >> x >> y >> z >> _
#define cin5(x, y, z, p, q) cin >> x >> y >> z >> p >> q
#define cin6(x, y, z, p, q, r) cin >> x >> y >> z >> p >> q >> r
#define cou(x) cout << x
#define cous(x) cout << x << " "
#define coul(x) cout << x << "\\n"
#define ret(x) return x
#define MAX 1000007
#define start                                                                  \\
  int main() {                                                                 \\
    fastio
#define finish                                                                 \\
  ret(0);                                                                      \\
  }
#define Test                                                                   \\
  ll _t;                                                                       \\
  cin1(_t);                                                                    \\
  rep(w, 0, _t)
#define fans(x)                                                                \\
  if (x)                                                                       \\
    coul("YES");                                                               \\
  else                                                                         \\
    coul("NO")

typedef long long ll;
const ll mx = 3e5 + 7;
const double eps = 1e-7;

// head collected
// Don't use variable name as --> prev, time, _t, w, y1
// Don't use ceil and pow function

ll n, a, b, _, ans, c, _1, _2, m, an, k, need, shuru, _3, _c, _a, _b, way, _0,
    mex, q;
double d;
ll ar[mx], co[mx];
string st, st1, st2, isGood;
vector<pair<ll, ll>> vs;
map<string, ll> mm;
set<string> ss;
pair<ll, ll> pr;
bool ok;
char ch;
vector<ll> res;
ll mod;

ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  ret(res);
}

ll f(ll x) {
  ll tem = 0;
  while (x != 0) {
    tem++;
    x = x % __builtin_popcountll(x);
  }
  return tem;
}

void helpPlz() {
  cin2(n, st);
  reverse(all(st));

  rep(i, 0, n) {
    if (st[i] == '0')
      continue;
    k++;
  }

  rep(i, 0, n) {
    if (st[i] == '0')
      continue;
    mod = k - 1;
    if (mod > 0)
      ar[i] = (powmod(2, i) % mod);
    mod = k + 1;
    co[i] = (powmod(2, i) % mod);
  }

  rep(i, 0, n) {
    _1 += ar[i];
    _2 += co[i];
  }

  rep(i, 0, n) {
    _ = 0;
    if (st[i] == '1' && k > 1) {
      mod = k - 1;
      _ = _1 - powmod(2, i) + mod;
      _ = _ % mod;
    } else if (st[i] == '0') {
      mod = k + 1;
      _ = _2 + powmod(2, i);
      _ = _ % mod;
    }

    if (k == 1 && st[i] == '1')
      res.pb(-1);
    else
      res.pb(f(_));
  }

  per(i, 0, n) coul(res[i] + 1);
}

start; // Md. Inzamam-ul Haque

helpPlz();

finish


Error: 0

Fixed submission:

/*
 ALLAH is Almighty....
*/
#include <bits/stdc++.h>
using namespace std;

#define pi acos(-1.0)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);                                                              \\
  cout << setprecision(12)
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define per(i, a, n) for (ll i = n - 1; i >= a; i--)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((ll)(x).size())
#define cin1(x) cin >> x
#define cin2(x, y) cin >> x >> y
#define cin3(x, y, z) cin >> x >> y >> z
#define cin4(x, y, z, _) cin >> x >> y >> z >> _
#define cin5(x, y, z, p, q) cin >> x >> y >> z >> p >> q
#define cin6(x, y, z, p, q, r) cin >> x >> y >> z >> p >> q >> r
#define cou(x) cout << x
#define cous(x) cout << x << " "
#define coul(x) cout << x << "\\n"
#define ret(x) return x
#define MAX 1000007
#define start                                                                  \\
  int main() {                                                                 \\
    fastio
#define finish                                                                 \\
  ret(0);                                                                      \\
  }
#define Test                                                                   \\
  ll _t;                                                                       \\
  cin1(_t);                                                                    \\
  rep(w, 0, _t)
#define fans(x)                                                                \\
  if (x)                                                                       \\
    coul("YES");                                                               \\
  else                                                                         \\
    coul("NO")

typedef long long ll;
const ll mx = 3e5 + 7;
const double eps = 1e-7;

// head collected
// Don't use variable name as --> prev, time, _t, w, y1
// Don't use ceil and pow function

ll n, a, b, _, ans, c, _1, _2, m, an, k, need, shuru, _3, _c, _a, _b, way, _0,
    mex, q;
double d;
ll ar[mx], co[mx];
string st, st1, st2, isGood;
vector<pair<ll, ll>> vs;
map<string, ll> mm;
set<string> ss;
pair<ll, ll> pr;
bool ok;
char ch;
vector<ll> res;
ll mod;

ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  ret(res);
}

ll f(ll x) {
  ll tem = 0;
  while (x != 0) {
    tem++;
    x = x % __builtin_popcountll(x);
  }
  return tem;
}

void helpPlz() {
  cin2(n, st);
  reverse(all(st));

  rep(i, 0, n) {
    if (st[i] == '0')
      continue;
    k++;
  }

  rep(i, 0, n) {
    if (st[i] == '0')
      continue;
    mod = k - 1;
    if (k)
      ar[i] = (powmod(2, i) % mod);
    mod = k + 1;
    co[i] = (powmod(2, i) % mod);
  }

  rep(i, 0, n) {
    _1 += ar[i];
    _2 += co[i];
  }

  rep(i, 0, n) {
    if (st[i] == '1') {
      mod = k - 1;
      _ = _1 - powmod(2, i) + mod;
      _ = _ % mod;
    } else {
      mod = k + 1;
      _ = _2 + powmod(2, i);
      _ = _ % mod;
    }
    res.pb(f(_));
  }

  per(i, 0, n) coul(res[i] + 1);
}

start; // Md. Inzamam-ul Haque

helpPlz();

finish