Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>

#define REP(i, n) for (int i = 0; (i) < (n); ++(i))
#define FOR(i, n) for (int i = 1; (i) <= (n); ++(i))
#define dump(x) cout << #x << " = " << (x) << endl;
#define pb push_back
#define int long long

const int INF = 1e18;
const int MOD = 1e9 + 7;
// const lint LINF = 1e18;
const double eps = 0.000000001; // もとの値の10^(-16)まで

using namespace std;

// typedef pair<int, int> P;
// priority_queue< P, vector<P>, greater<P> >
// q;//ダイクストラの時、greaterで小さい順 cout << fixed << setprecision(10) <<
// ans << endl; int gcd(int a,int b){return b?gcd(b,a%b):a;}

vector<int> x;
int ans[300];

int n2[300000];
int n3[300000];
signed main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int ne = 0;
    int i2 = i;
    while (i2 > 0) {
      ne += i2 % 2;
      i2 /= 2;
    }
    ans[i] = ans[i % ne] + 1;
  }

  int wa = 0;
  REP(i, n) {
    char x3;
    cin >> x3;
    int x1 = x3 - '0';
    x.pb(x1);
    wa += x1;
  }
  int ans1 = 0;
  int ans2 = 0;
  n2[n - 1] = 1;
  REP(i, n) {
    if (i != 0) {
      n2[n - 1 - i] = n2[n - i] * 2 % (wa + 1);
    }
    ans1 += x[n - 1 - i] * n2[n - 1 - i] % (wa + 1);
    ans1 %= (wa + 1);
  }
  if (wa == 1) {

  } else {
    n3[n - 1] = 1;
    REP(i, n) {
      if (i != 0) {
        n3[n - 1 - i] = n3[n - i] * 2 % (wa - 1);
      }
      ans2 += x[n - 1 - i] * n3[n - 1 - i] % (wa - 1);
      ans2 %= (wa - 1);
    }
  }
  REP(i, n) {
    if (x[i] == 1) {
      if (wa == 1) {
        cout << 0 << endl;

      } else {
        cout << ans[((wa - 1) + ans2 - n3[i] % (wa - 1)) % (wa - 1)] + 1
             << endl;
      }
    } else {
      cout << ans[((ans1 + n2[i]) % (wa + 1)) % (wa + 1)] + 1 << endl;
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>

#define REP(i, n) for (int i = 0; (i) < (n); ++(i))
#define FOR(i, n) for (int i = 1; (i) <= (n); ++(i))
#define dump(x) cout << #x << " = " << (x) << endl;
#define pb push_back
#define int long long

const int INF = 1e18;
const int MOD = 1e9 + 7;
// const lint LINF = 1e18;
const double eps = 0.000000001; // もとの値の10^(-16)まで

using namespace std;

// typedef pair<int, int> P;
// priority_queue< P, vector<P>, greater<P> >
// q;//ダイクストラの時、greaterで小さい順 cout << fixed << setprecision(10) <<
// ans << endl; int gcd(int a,int b){return b?gcd(b,a%b):a;}

vector<int> x;
int ans[300000];

int n2[300000];
int n3[300000];
signed main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int ne = 0;
    int i2 = i;
    while (i2 > 0) {
      ne += i2 % 2;
      i2 /= 2;
    }
    ans[i] = ans[i % ne] + 1;
  }

  int wa = 0;
  REP(i, n) {
    char x3;
    cin >> x3;
    int x1 = x3 - '0';
    x.pb(x1);
    wa += x1;
  }
  int ans1 = 0;
  int ans2 = 0;
  n2[n - 1] = 1;
  REP(i, n) {
    if (i != 0) {
      n2[n - 1 - i] = n2[n - i] * 2 % (wa + 1);
    }
    ans1 += x[n - 1 - i] * n2[n - 1 - i] % (wa + 1);
    ans1 %= (wa + 1);
  }
  if (wa == 1) {

  } else {
    n3[n - 1] = 1;
    REP(i, n) {
      if (i != 0) {
        n3[n - 1 - i] = n3[n - i] * 2 % (wa - 1);
      }
      ans2 += x[n - 1 - i] * n3[n - 1 - i] % (wa - 1);
      ans2 %= (wa - 1);
    }
  }
  REP(i, n) {
    if (x[i] == 1) {
      if (wa == 1) {
        cout << 0 << endl;

      } else {
        cout << ans[((wa - 1) + ans2 - n3[i] % (wa - 1)) % (wa - 1)] + 1
             << endl;
      }
    } else {
      cout << ans[((ans1 + n2[i]) % (wa + 1)) % (wa + 1)] + 1 << endl;
    }
  }

  return 0;
}