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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
using namespace std;
using namespace __gnu_pbds;
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define int long long
// int dx[]={-1,1,0,0}; int dy[]={0,0,1,-1};
// int dx[]={2,2,-2,-2,1,1,-1,-1}; int dy[]={1,-1,1,-1,2,-2,2,-2};
const long long mod = 1e9 + 7;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int, int> ii;
typedef vector<pair<int, int>> vii;
typedef map<int, int> mii;
typedef pair<int, ii> pip;
typedef pair<ii, int> ppi;
#define arrinp(arr, init, final, size, type)                                   \\
  type *arr = new type[size];                                                  \\
  for (int i = init; i < final; i++)                                           \\
    cin >> arr[i];
#define cr2d(arr, n, m, t)                                                     \\
  t **arr = new t *[n];                                                        \\
  for (int i = 0; i < n; i++)                                                  \\
    arr[i] = new t[m];
#define w(t)                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define takeInp(n)                                                             \\
  int n;                                                                       \\
  cin >> n;
#define fr(i, init, final) for (int i = init; i < final; i++)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(), (c).end()
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    nds;
vi init(string s) {
  istringstream sin(s);
  int n;
  vi arr;
  while (sin >> n)
    arr.push_back(n);
  return arr;
}
int pow(int x, int y, int m) {
  if (y == 0)
    return 1;
  int u = pow(x, y / 2, m);
  u = (u * u) % m;
  if (y % 2)
    u = (x * u) % m;
  return u;
}
int gcd(int a, int b) {
  if (a < b)
    return gcd(b, a);
  return (b == 0 ? a : (a % b ? gcd(b, a % b) : b));
}
int gcd_e(int a, int b, int &x, int &y) {

  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  int x1, y1;
  int p = gcd_e(b, a % b, x1, y1);
  x = y1;
  y = x1 - (a / b) * y1;
  return p;
}
signed main() {
  IOS int n;
  string s;
  cin >> n >> s;
  int dp[1000000];
  dp[0] = 0;
  fr(i, 1, 1000000) dp[i] = 1 + dp[i % __builtin_popcount(i)];
  int ans1 = 0, ans2 = 0, ans3 = 0;
  int c = 0;
  fr(i, 0, n) c += (s[i] == '1');
  if (c != 0) {
    fr(i, 0, n) if (s[i] == '1') {
      ans1 = (ans1 + pow(2, n - 1 - i, c)) % c;
      ans2 = (ans2 + pow(2, n - 1 - i, c + 1)) % (c + 1);
      if (c != 1)
        ans3 = (ans3 + pow(2, n - 1 - i, c - 1)) % (c - 1);
    }
  }
  fr(i, 0, n) {
    if (s[i] == '0')
      cout << 1 + dp[(ans2 + pow(2, n - 1 - i, c + 1)) % (c + 1)] << endl;
    else
      cout << (c == 1 ? dp[0]
                      : 1 + dp[(c - 1 + ans3 - (pow(2, n - 1 - i, c - 1))) %
                               (c - 1)])
           << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
using namespace std;
using namespace __gnu_pbds;
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define int long long
// int dx[]={-1,1,0,0}; int dy[]={0,0,1,-1};
// int dx[]={2,2,-2,-2,1,1,-1,-1}; int dy[]={1,-1,1,-1,2,-2,2,-2};
const long long mod = 1e9 + 7;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int, int> ii;
typedef vector<pair<int, int>> vii;
typedef map<int, int> mii;
typedef pair<int, ii> pip;
typedef pair<ii, int> ppi;
#define arrinp(arr, init, final, size, type)                                   \\
  type *arr = new type[size];                                                  \\
  for (int i = init; i < final; i++)                                           \\
    cin >> arr[i];
#define cr2d(arr, n, m, t)                                                     \\
  t **arr = new t *[n];                                                        \\
  for (int i = 0; i < n; i++)                                                  \\
    arr[i] = new t[m];
#define w(t)                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define takeInp(n)                                                             \\
  int n;                                                                       \\
  cin >> n;
#define fr(i, init, final) for (int i = init; i < final; i++)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(), (c).end()
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    nds;
vi init(string s) {
  istringstream sin(s);
  int n;
  vi arr;
  while (sin >> n)
    arr.push_back(n);
  return arr;
}
int pow(int x, int y, int m) {
  if (y == 0)
    return 1;
  int u = pow(x, y / 2, m);
  u = (u * u) % m;
  if (y % 2)
    u = (x * u) % m;
  return u;
}
int gcd(int a, int b) {
  if (a < b)
    return gcd(b, a);
  return (b == 0 ? a : (a % b ? gcd(b, a % b) : b));
}
int gcd_e(int a, int b, int &x, int &y) {

  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  int x1, y1;
  int p = gcd_e(b, a % b, x1, y1);
  x = y1;
  y = x1 - (a / b) * y1;
  return p;
}
signed main() {
  IOS int n;
  string s;
  cin >> n >> s;
  int dp[1000000];
  dp[0] = 0;
  fr(i, 1, 1000000) dp[i] = 1 + dp[i % __builtin_popcount(i)];
  int ans1 = 0, ans2 = 0, ans3 = 0;
  int c = 0;
  fr(i, 0, n) c += (s[i] == '1');
  if (c != 0) {
    fr(i, 0, n) if (s[i] == '1') {
      ans1 = (ans1 + pow(2, n - 1 - i, c)) % c;
      ans2 = (ans2 + pow(2, n - 1 - i, c + 1)) % (c + 1);
      if (c != 0)
        ans3 = (ans3 + pow(2, n - 1 - i, c - 1)) % (c - 1);
    }
  }
  fr(i, 0, n) {
    if (s[i] == '0')
      cout << 1 + dp[(ans2 + pow(2, n - 1 - i, c + 1)) % (c + 1)] << endl;
    else
      cout << 1 + dp[(c - 1 + ans3 - (pow(2, n - 1 - i, c - 1))) % (c - 1)]
           << endl;
  }
}