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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define pb push_back
#define f(i, n) for (int i = 0; i < n; ++i)
#define fi(i, st, n) for (int i = st; i <= n; ++i)
#define veci vector<int>
#define vecp vector<pair<int, int>>
#define vecl vector<ll>
int prime[100000 + 10];

ll lcm(ll a, ll b) { return a * b / __gcd(a, b); }

ll power(ll a, ll n, ll mod) {
  if (n < 0 or a < 0 or mod <= 0)
    return 0;
  if (mod == 1)
    return 0;
  ll res = 1;
  while (n > 0) {
    if (n & 1) {
      res = (res * a) % mod;
    }
    a = (a * a) % mod;
    n = n / 2;
  }
  return res;
}

ll sum(int arr[], int n) {
  ll res = 0;
  f(i, n) { res += arr[i]; }
  return res;
}

void seive() {
  prime[1] = 0;
  for (int i = 2; i <= 100000; i++) {
    prime[i] = 1;
  }
  for (ll i = 2; i <= 100000; i++) {
    if (prime[i]) {
      for (ll j = i * i; j <= 100000; j += i) {
        prime[j] = 0;
      }
    }
  }
}

ll cal(ll n) {
  ll c = 0;
  if (n == 0)
    return 0;
  ll temp = n;
  while (n > 0) {
    if (n % 2)
      c++;
    n /= 2;
  }
  //	cout<<c<<" ";
  return 1 + cal(temp % c);
}

int main() {
  //	ios_base::sync_with_stdio(false);
  //	cin.tie(NULL);
  //	cout.tie(NULL);
  int t = 1;
  //	cin>>t;
  while (t--) {
    int n;
    cin >> n;
    string s;
    cin >> s;
    ll c = 0;
    f(i, n) {
      if (s[i] == '1')
        c++;
    }
    reverse(s.begin(), s.end());
    //		cout<<"-1 ";
    int ans1 = 0, ans2 = 0;
    for (ll i = 0; i < n; i++) {
      if (s[i] == '1') {
        ans1 += power(2, i, c + 1);
        ans2 += power(2, i, c - 1);
        ans1 %= (c + 1);
        if (c != 1)
          ans2 %= (c - 1);
      }
    }
    //		cout<<"-2 ";
    for (int i = n - 1; i >= 0; i--) {
      if (s[i] == '0') {
        ll temp = (ans1 + power(2, i, c + 1)) % (c + 1);
        cout << 1 + cal(temp) << "\\n";
      } else {
        if (c != 1) {
          ll temp = (ans2 - power(2, i, c - 1) + (c - 1)) % (c - 1);
          cout << 1 + cal(temp) << "\\n";
        } else {
          cout << "0\\n";
        }
      }
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define pb push_back
#define f(i, n) for (int i = 0; i < n; ++i)
#define fi(i, st, n) for (int i = st; i <= n; ++i)
#define veci vector<int>
#define vecp vector<pair<int, int>>
#define vecl vector<ll>
int prime[100000 + 10];

ll lcm(ll a, ll b) { return a * b / __gcd(a, b); }

ll power(ll a, ll n, ll mod) {
  if (n < 0 or a < 0 or mod <= 0)
    return 0;
  if (mod == 1)
    return 0;
  ll res = 1;
  while (n > 0) {
    if (n & 1) {
      res = (res * a) % mod;
    }
    a = (a * a) % mod;
    n = n / 2;
  }
  return res;
}

ll sum(int arr[], int n) {
  ll res = 0;
  f(i, n) { res += arr[i]; }
  return res;
}

void seive() {
  prime[1] = 0;
  for (int i = 2; i <= 100000; i++) {
    prime[i] = 1;
  }
  for (ll i = 2; i <= 100000; i++) {
    if (prime[i]) {
      for (ll j = i * i; j <= 100000; j += i) {
        prime[j] = 0;
      }
    }
  }
}

ll cal(ll n) {
  ll c = 0;
  if (n == 0)
    return 0;
  ll temp = n;
  while (n > 0) {
    if (n % 2)
      c++;
    n /= 2;
  }
  //	cout<<c<<" ";
  return 1 + cal(temp % c);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  //	cin>>t;
  while (t--) {
    int n;
    cin >> n;
    string s;
    cin >> s;
    ll c = 0;
    f(i, n) {
      if (s[i] == '1')
        c++;
    }
    reverse(s.begin(), s.end());
    int ans1 = 0, ans2 = 0;
    for (ll i = 0; i < n; i++) {
      if (s[i] == '1') {
        ans1 += power(2, i, c + 1);
        ans2 += power(2, i, c - 1);
        ans1 %= (c + 1);
        ans2 %= (c - 1);
      }
    }
    for (int i = n - 1; i >= 0; i--) {
      if (s[i] == '0') {
        ll temp = (ans1 + power(2, i, c + 1)) % (c + 1);
        cout << 1 + cal(temp) << "\\n";
      } else {
        if (c != 1) {
          ll temp = (ans2 - power(2, i, c - 1) + (c - 1)) % (c - 1);
          cout << 1 + cal(temp) << "\\n";
        } else {
          cout << "0\\n";
        }
      }
    }
  }
  return 0;
}