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

#include <bits/stdc++.h>
#define pb push_back
#define all(a) a.begin(), a.end()
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define forr(i, n) for (int i = int(n - 1); i >= 0; i--)
#define fora(i, a, b) for (int i = int(a); i <= int(b); i++)
#define forb(i, a, b) for (int i = int(a); i >= int(b); i--)
#define mod 1000000007
using namespace std;
using ll = long long int;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<ll> vii;
typedef pair<ll, ll> pii;
const int N = 1e7;
ll ppow(ll a, ll b, ll m) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % m;
    a = (a * a) % m;
    b >>= 1;
  }
  return res % m;
}
int get(ll n) {
  if (n == 0)
    return 0;
  int ans = 0;
  while (n) {
    int pc = __builtin_popcount(n);
    n %= pc;
    ans++;
  }
  return ans;
}
void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int c = count(all(s), '1');
  int m1 = c + 1, m2 = c - 1;
  if (c == 0) {
    forn(i, n) { cout << 1 << endl; }
    return;
  }
  vii v1, v2;
  int x = n - 1;

  while (x != -1) {
    v1.pb(ppow(2, x, m1));
    x--;
  }

  ll ans1 = 0, ans2 = 0;
  forn(i, n) {
    if (s[i] == '1')
      ans1 += v1[i], ans1 %= m1;
  }
  if (m2 == 0) {
    forn(i, n) {
      ll cur = 0;
      int ans = 1;
      if (s[i] == '1') {
        cout << 0 << endl;
        continue;
      } else {
        cur = ans1;
        cur += v1[i];
        cur %= m1;
      }
      ans += get(cur);

      cout << ans << endl;
    }
    return;
  }

  x = n - 1;
  while (x != -1) {
    v2.pb(ppow(2, x, m2));
    x--;
  }

  // for(auto i:v1)cout<<i<<" ";
  //	cout<<endl;
  // for(auto i:v2)cout<<i<<" ";

  forn(i, n) {
    if (s[i] == '1')
      ans2 += v2[i], ans2 %= m2;
  }
  vi aa;
  forn(i, n) {
    ll cur;
    int ans = 1;
    if (s[i] == '1') {
      cur = ans2;
      cur += m2;
      cur -= v2[i];
      cur += m2;
      cur %= m2;
    } else {
      cur = ans1;
      cur += v1[i];
      cur %= m1;
    }
    ans += get(cur);

    aa.pb(ans);
  }

  for (auto i : aa)
    cout << i << endl;
}
int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define all(a) a.begin(), a.end()
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define forr(i, n) for (int i = int(n - 1); i >= 0; i--)
#define fora(i, a, b) for (int i = int(a); i <= int(b); i++)
#define forb(i, a, b) for (int i = int(a); i >= int(b); i--)
#define mod 1000000007
using namespace std;
using ll = long long int;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<ll> vii;
typedef pair<ll, ll> pii;
const int N = 1e7;
ll ppow(ll a, ll b, ll m) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % m;
    a = (a * a) % m;
    b >>= 1;
  }
  return res % m;
}
int get(ll n) {
  if (n == 0)
    return 0;
  int ans = 0;
  while (n) {
    int pc = __builtin_popcount(n);
    n %= pc;
    ans++;
  }
  return ans;
}
void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int c = count(all(s), '1');
  int m1 = c + 1, m2 = c - 1;

  vii v1, v2;
  int x = n - 1;

  while (x != -1) {
    v1.pb(ppow(2, x, m1));
    x--;
  }

  x = n - 1;
  while (x != -1) {
    v2.pb(ppow(2, x, m2));
    x--;
  }

  // for(auto i:v1)cout<<i<<" ";
  //	cout<<endl;
  // for(auto i:v2)cout<<i<<" ";

  ll ans1 = 0, ans2 = 0;

  forn(i, n) {
    if (s[i] == '1')
      ans1 += v1[i], ans1 %= m1;
    if (s[i] == '1')
      ans2 += v2[i], ans2 %= m2;
  }
  vi aa;
  forn(i, n) {
    ll cur;
    int ans = 1;
    if (s[i] == '1') {
      cur = ans2;
      cur += m2;
      cur -= ppow(2, n - i - 1, m2);
      cur += m2;
      cur %= m2;
      ans = 1;
    } else {
      cur = ans1;
      cur += ppow(2, n - i - 1, m1);
      cur %= m1;
    }
    ans += get(cur);

    aa.pb(ans);
  }

  for (auto i : aa)
    cout << i << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}