Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <vector>
#define F first
#define S second
#define all(a) a.begin(), a.end()
#define setDP(arr) memset(arr, -1, sizeof arr)
#define Clear(arr) memset(arr, 0, sizeof arr)
#define oo 1000000000
#define inf 1000000000000000000
#define M 998244353
using namespace std;
typedef long long ll;
const int N = 1e6 + 300;
const int SQRTN = 320;
const int LOGN = 20;
const double PI = acos(-1);
const double TAU = 2 * PI;

int n;
int dp[N];
char s[N];
int fast(int x, int y, int mod) {
  if (!y)
    return 1;
  int ret = fast(x, y >> 1, mod);
  ret = (ret * 1ll * ret) % mod;
  if (y & 1)
    ret = ret * 1ll * x % mod;
  return ret;
}

int popcount(int x) {
  int ctr = 0;
  while (x) {
    ctr += x & 1;
    x >>= 1;
  }
  return ctr;
}

inline void solve() {
  cin >> n;
  dp[1] = 1;
  for (int i = 2; i < N; i++) {
    dp[i] = dp[i % (popcount(i))] + 1;
  }
  int ones = 0;
  scanf("%s", s);
  for (int i = 0; i < n; i++) {
    ones += s[i] == '1';
  }
  if (ones == 0) {
    for (int i = 0; i < n; i++) {
      puts("1");
    }
    return;
  }
  int val1, val2, val3;
  val1 = val2 = val3 = 0;
  for (int i = 0; i < n; i++) {
    val1 *= 2;
    val2 *= 2;
    val3 *= 2;
    val1 += s[i] - '0';
    val2 += s[i] - '0';
    val3 += s[i] - '0';
    val1 %= ones - 1;
    val2 %= ones;
    val3 %= ones + 1;
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      printf("%d\n",
             1 + dp[(val3 + fast(2, n - i - 1, ones + 1)) % (ones + 1)]);
    } else {
      if (ones > 1)
        printf("%d\n", 1 + dp[(ones - 1 + val1 - fast(2, n - i - 1, ones - 1)) %
                              (ones - 1)]);
      else {
        cout << 0 << endl;
      }
    }
  }
}

signed main() {
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <vector>
#define F first
#define S second
#define all(a) a.begin(), a.end()
#define setDP(arr) memset(arr, -1, sizeof arr)
#define Clear(arr) memset(arr, 0, sizeof arr)
#define oo 1000000000
#define inf 1000000000000000000
#define M 998244353
using namespace std;
typedef long long ll;
const int N = 1e6 + 300;
const int SQRTN = 320;
const int LOGN = 20;
const double PI = acos(-1);
const double TAU = 2 * PI;

int n;
int dp[N];
char s[N];
int fast(int x, int y, int mod) {
  if (!y)
    return 1;
  int ret = fast(x, y >> 1, mod);
  ret = (ret * 1ll * ret) % mod;
  if (y & 1)
    ret = ret * 1ll * x % mod;
  return ret;
}

int popcount(int x) {
  int ctr = 0;
  while (x) {
    ctr += x & 1;
    x >>= 1;
  }
  return ctr;
}

inline void solve() {
  cin >> n;
  dp[1] = 1;
  for (int i = 2; i < N; i++) {
    dp[i] = dp[i % (popcount(i))] + 1;
  }
  int ones = 0;
  scanf("%s", s);
  for (int i = 0; i < n; i++) {
    ones += s[i] == '1';
  }
  if (ones == 0) {
    for (int i = 0; i < n; i++) {
      puts("1");
    }
    return;
  }
  int val1, val2, val3;
  val1 = val2 = val3 = 0;
  for (int i = 0; i < n; i++) {
    val1 *= 2;
    val2 *= 2;
    val3 *= 2;
    val1 += s[i] - '0';
    val2 += s[i] - '0';
    val3 += s[i] - '0';
    if (ones - 1)
      val1 %= ones - 1;
    val2 %= ones;
    val3 %= ones + 1;
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      printf("%d\n",
             1 + dp[(val3 + fast(2, n - i - 1, ones + 1)) % (ones + 1)]);
    } else {
      if (ones > 1)
        printf("%d\n", 1 + dp[(ones - 1 + val1 - fast(2, n - i - 1, ones - 1)) %
                              (ones - 1)]);
      else {
        cout << 0 << endl;
      }
    }
  }
}

signed main() {
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}