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

//@author yash2040
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define ll long long
#define int long long
#define pb push_back
#define mod 1000000007
#define fast()                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define deb(X)                                                                 \\
  std::cerr << ">>> deb(" << __LINE__ << \\") " << #X << " = " << (X)           \\
            << std::endl

int power(int x, int y, int mo) {
  if (y < 0 or x < 0 or mo <= 0)
    return 0;
  if (mo == 1)
    return 0;
  int res = 1;
  while (y != 0) {
    if (y % 2 == 1)
      res = (res % mo * x % mo) % mo;
    y = y / 2;
    x = (x % mo * x % mo) % mo;
  }
  return res;
}

int calc(ll n) {
  if (n == 0)
    return 0;
  ll c = __builtin_popcount(n);

  //	cout<<c<<" ";
  return 1 + calc(n % c);
}

int32_t main() {
  fast();
  int t = 1;
  //	cin>>t;
  while (t--) {
    int n;
    cin >> n;
    string s;
    cin >> s;
    int c = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == '1')
        c++;
    }
    reverse(s.begin(), s.end());
    int ans1 = 0;
    int ans2 = 0;
    for (int 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);
      }
    }
    for (int i = n - 1; i >= 0; i--) {
      if (s[i] == '0') {
        int temp = (ans1 + power(2, i, c + 1)) % (c + 1);
        cout << 1 + calc(temp) << "\\n";
      } else {
        if (c != 1) {
          int temp = (ans2 - power(2, i, c - 1) + (c - 1)) % (c - 1);
          cout << 1 + calc(temp) << "\\n";
        } else {
          cout << "0\\n";
        }
      }
    }
  }
}


Error: 0

Fixed submission:

//@author yash2040
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define ll long long
#define int long long
#define pb push_back
#define mod 1000000007
#define fast()                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define deb(X)                                                                 \\
  std::cerr << ">>> deb(" << __LINE__ << \\") " << #X << " = " << (X)           \\
            << std::endl

int power(int x, int y, int mo) {
  int res = 1;
  while (y != 0) {
    if (y % 2 == 1)
      res = (res % mo * x % mo) % mo;
    y = y / 2;
    x = (x % mo * x % mo) % mo;
  }
  return res;
}

int calc(int n) {
  // int c = 0;
  if (n == 0)
    return 0;
  // int temp = n;
  int c = __builtin_popcount(n);
  return 1 + calc(n % c);
}

int32_t main() {
  fast();
  int t = 1;
  //	cin>>t;
  while (t--) {
    int n;
    cin >> n;
    string s;
    cin >> s;
    int c = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == '1')
        c++;
    }
    reverse(s.begin(), s.end());
    int ans1 = 0;
    int ans2 = 0;
    for (int 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);
      }
    }
    for (int i = n - 1; i >= 0; i--) {
      if (s[i] == '0') {
        int temp = (ans1 + power(2, i, c + 1)) % (c + 1);
        cout << 1 + calc(temp) << "\\n";
      } else {
        if (c != 1) {
          int temp = (ans2 - power(2, i, c - 1) + (c - 1)) % (c - 1);
          cout << 1 + calc(temp) << "\\n";
        } else {
          cout << "0\\n";
        }
      }
    }
  }
}