Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
const ll MOD = 1000000007;
const long double PI = 3.14159265358979;
const ll MAX = 0;

ll ff(ll x, ll n, ll a) {
  if (x == 0) {
    return a;
  }
  x %= n;
  ll nx = x;
  ll nn = 0;
  while (nx > 0) {
    if (nx % 2 == 1) {
      nn++;
    }
    nx /= 2;
  }
  return ff(x, nn, a + 1);
}

ll f(string x, ll n) {
  ll x1 = 0, a = 1;
  if (n == 0) {
    return 0;
  }
  for (int i = x.size() - 1; i >= 0; i--) {
    if (x[i] == '1') {
      x1 += a;
    }
    a *= 2;
    a %= n;
    x1 %= n;
  }

  return x1;
}

int main() {
  ll N;
  string X;
  cin >> N >> X;
  ll num = 0;
  ll a[N];
  rep(i, N) {
    if (X[i] == '1') {
      num++;
    }
  }
  ll n1 = f(X, num + 1);
  //  cout<<n1;
  ll n2 = f(X, num - 1);
  ll a1 = 1, a2 = 1;
  for (int i = N - 1; i >= 0; i--) {
    string x = X;
    ll n = num;
    if (X[i] == '0') {
      x[i] = '1';
      n++;
      ll n11 = a1 + n1;
      n11 %= num + 1;
      ll nx = n11;
      ll nn = 0;
      while (nx > 0) {
        if (nx % 2 == 1) {
          nn++;
        }
        nx /= 2;
      }
      a[i] = ff(n11, nn, 1);
    } else {
      x[i] = '0';
      n--;
      ll n22 = n2 - a2;
      if (n == 0) {
        a[i] = 0;
        continue;
      }
      n22 %= num - 1;
      if (n22 < 0) {
        n22 += num - 1;
      }
      ll nx = n22;
      ll nn = 0;
      while (nx > 0) {
        if (nx % 2 == 1) {
          nn++;
        }
        nx /= 2;
      }
      a[i] = ff(n22, nn, 1);
    }

    // cout<<ff(x,n,1)<<endl;
    a1 *= 2;
    a1 %= num + 1;
    a2 *= 2;
    if (n == 0) {
      continue;
    }
    a2 %= num - 1;
  }

  for (int i = 0; i < N; i++) {
    cout << a[i] << endl; // cout<<0%2;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
const ll MOD = 1000000007;
const long double PI = 3.14159265358979;
const ll MAX = 0;

ll ff(ll x, ll n, ll a) {
  if (x == 0) {
    return a;
  }
  x %= n;
  ll nx = x;
  ll nn = 0;
  while (nx > 0) {
    if (nx % 2 == 1) {
      nn++;
    }
    nx /= 2;
  }
  return ff(x, nn, a + 1);
}

ll f(string x, ll n) {
  ll x1 = 0, a = 1;
  if (n == 0) {
    return 0;
  }
  for (int i = x.size() - 1; i >= 0; i--) {
    if (x[i] == '1') {
      x1 += a;
    }
    a *= 2;
    a %= n;
    x1 %= n;
  }

  return x1;
}

int main() {
  ll N;
  string X;
  cin >> N >> X;
  ll num = 0;
  ll a[N];
  rep(i, N) {
    if (X[i] == '1') {
      num++;
    }
  }
  ll n1 = f(X, num + 1);
  //  cout<<n1;
  ll n2 = f(X, num - 1);
  ll a1 = 1, a2 = 1;
  for (int i = N - 1; i >= 0; i--) {
    string x = X;
    ll n = num;
    if (X[i] == '0') {
      x[i] = '1';
      n++;
      ll n11 = a1 + n1;
      n11 %= num + 1;
      ll nx = n11;
      ll nn = 0;
      while (nx > 0) {
        if (nx % 2 == 1) {
          nn++;
        }
        nx /= 2;
      }
      a[i] = ff(n11, nn, 1);
    } else {
      x[i] = '0';
      n--;
      ll n22 = n2 - a2;
      if (n == 0) {
        a[i] = 0;
        continue;
      }
      n22 %= num - 1;
      if (n22 < 0) {
        n22 += num - 1;
      }
      ll nx = n22;
      ll nn = 0;
      while (nx > 0) {
        if (nx % 2 == 1) {
          nn++;
        }
        nx /= 2;
      }
      a[i] = ff(n22, nn, 1);
    }

    // cout<<ff(x,n,1)<<endl;
    a1 *= 2;
    a1 %= num + 1;
    a2 *= 2;
    if (num == 1) {
      continue;
    }
    a2 %= num - 1;
  }

  for (int i = 0; i < N; i++) {
    cout << a[i] << endl; // cout<<0%2;
  }
}