Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

#define rep(pp, nn) for (int ii = pp; ii < nn; ++ii)
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>

string int_to_string(int x) {
  stringstream ss;
  ss << x;
  string ni = ss.str();
  return ni;
}
constexpr int mod = 1e9 + 7;
ll POW(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans = ans * a;
    }
    a = a * a;
    b /= 2;
  }
  return ans;
}

ll power(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans = (ans * a) % mod;
    }
    a = (a * a) % mod;
    b /= 2;
  }
  return ans;
}
ll gcd(ll a, ll b) {
  if (a == 0) {
    return b;
  }
  return gcd(b % a, a);
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
bool isprime(ll n) {
  for (ll i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      return 0;
    }
  }
  return 1;
}

ll power_mod(ll a, ll b, ll mo) {
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans = (ans % mo * a % mo) % mo;
    }
    a = (a % mo * a % mo) % mo;
    b /= 2;
  }
  return ans;
}

ll call(ll cnt, string s) {
  ll p = 0;
  ll x = 0;
  for (int i = (int)s.size() - 1; i >= 0; --i) {
    ll y = 0;
    if (s[i] == '1') {
      y = power_mod(2LL, p, cnt);
    }
    x = (y % cnt + x % cnt) % cnt;
    p++;
  }
  return x;
}

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1')
      cnt++;
  }
  // cout << cnt << endl;
  ll inc_val = call(cnt + 1, s);
  ll dec_val = (cnt > 0) ? call(cnt - 1, s) : inc_val;
  // cout << inc_val << " "<< dec_val << endl;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '0')
      cnt++, s[i] = '1';
    else
      cnt--, s[i] = '0';
    if (cnt == 0) {
      cout << 0 << endl;
      if (s[i] == '1')
        cnt--, s[i] = '0';
      else
        cnt++, s[i] = '1';
      continue;
    }
    ll val = 0;
    if (s[i] == '1') {
      // inc_val
      val = (power_mod(2LL, n - i - 1, cnt) + inc_val + cnt) % cnt;
    } else {
      val = (-power_mod(2LL, n - i - 1, cnt) + dec_val + cnt) % cnt;
    }
    ll step = 1;
    // cout << val << " " << cnt << " ";
    if (s[i] == '1')
      cnt--, s[i] = '0';
    else
      cnt++, s[i] = '1';

    while (val) {
      ll p = 0;
      ll x = val;
      while (x) {
        if (x & 1)
          p++;
        x /= 2;
      }
      val = val % p;
      step++;
    }
    cout << step << endl;
  }
}


Fixed submission:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

#define rep(pp, nn) for (int ii = pp; ii < nn; ++ii)
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>

string int_to_string(int x) {
  stringstream ss;
  ss << x;
  string ni = ss.str();
  return ni;
}
constexpr int mod = 1e9 + 7;
ll POW(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans = ans * a;
    }
    a = a * a;
    b /= 2;
  }
  return ans;
}

ll power(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans = (ans * a) % mod;
    }
    a = (a * a) % mod;
    b /= 2;
  }
  return ans;
}
ll gcd(ll a, ll b) {
  if (a == 0) {
    return b;
  }
  return gcd(b % a, a);
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
bool isprime(ll n) {
  for (ll i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      return 0;
    }
  }
  return 1;
}

ll power_mod(ll a, ll b, ll mo) {
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans = (ans % mo * a % mo) % mo;
    }
    a = (a % mo * a % mo) % mo;
    b /= 2;
  }
  return ans;
}

ll call(ll cnt, string s) {
  ll p = 0;
  ll x = 0;
  for (int i = (int)s.size() - 1; i >= 0; --i) {
    ll y = 0;
    if (s[i] == '1') {
      y = power_mod(2LL, p, cnt);
    }
    x = (y % cnt + x % cnt) % cnt;
    p++;
  }
  return x;
}

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1')
      cnt++;
  }
  // cout << cnt << endl;
  ll inc_val = call(cnt + 1, s);
  ll dec_val = (cnt > 1) ? call(cnt - 1, s) : inc_val;
  // cout << inc_val << " "<< dec_val << endl;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '0')
      cnt++, s[i] = '1';
    else
      cnt--, s[i] = '0';
    if (cnt == 0) {
      cout << 0 << endl;
      if (s[i] == '1')
        cnt--, s[i] = '0';
      else
        cnt++, s[i] = '1';
      continue;
    }
    ll val = 0;
    if (s[i] == '1') {
      // inc_val
      val = (power_mod(2LL, n - i - 1, cnt) + inc_val + cnt) % cnt;
    } else {
      val = (-power_mod(2LL, n - i - 1, cnt) + dec_val + cnt) % cnt;
    }
    ll step = 1;
    // cout << val << " " << cnt << " ";
    if (s[i] == '1')
      cnt--, s[i] = '0';
    else
      cnt++, s[i] = '1';

    while (val) {
      ll p = 0;
      ll x = val;
      while (x) {
        if (x & 1)
          p++;
        x /= 2;
      }
      val = val % p;
      step++;
    }
    cout << step << endl;
  }
}