Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

#define pb push_back
#define mp make_pair
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define ren(i, a, b) for (int i = a; i >= b; i--)
#define ff first
#define ss second
#define pll pair<long long int, long long int>
#define pii pair<int, int>
#define vll vector<long long int>
#define vii vector<int>
#define gi(n) scanf("%d", &n)
#define gll(n) scanf("%lld", &n)
#define gstr(n) scanf("%s", n)
#define gl(n) cin >> n
#define oi(n) printf("%d", n)
#define oll(n) printf("%lld", n)
#define ostr(n) printf("%s", n)
#define ol(n) cout << n
#define os cout << " "
#define on cout << "\n"
#define o2(a, b) cout << a << " " << b
#define all(n) n.begin(), n.end()
#define present(s, x) (s.find(x) != s.end())
#define cpresent(s, x) (find(all(s), x) != s.end())
#define tr(container, it)                                                      \
  for (__typeof(container.begin()) it = container.begin();                     \
       it != container.end(); it++)
using namespace std;

typedef unsigned long long int ull;
typedef long long int ll;
typedef long double ld;
typedef vector<vector<ll>> mat;

const int N = 1e6 + 5;

int dp[N];

int p(int a, int b, int mod) {
  int r = 1;
  while (b) {
    if (b % 2)
      r = (r * a) % mod;
    a = (a * a) % mod;
    b /= 2;
  }
  return r;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  dp[0] = 0;
  rep(i, 1, N - 1) dp[i] = 1 + dp[i % __builtin_popcount(i)];
  int n;
  string s;
  cin >> n >> s;
  int cnt = 0;
  rep(i, 0, n - 1) {
    if (s[i] == '1')
      cnt++;
  }
  int a1 = 0, a2 = 0;

  rep(i, 0, n - 1) {
    if (s[i] == '1') {
      a1 = (a1 + p(2, n - 1 - i, cnt + 1)) % (cnt + 1);
      if (cnt > 1)
        a2 = (a2 + p(2, n - 1 - i, cnt - 1)) % (cnt - 1);
    }
  }

  rep(i, 0, n - 1) {
    if (s[i] == '1') {
      if (cnt == 1) {
        ol("0\n");
      } else {
        int kk = (a2 - p(2, n - 1 - i, cnt - 1)) % (cnt - 1);
        if (kk < 0)
          kk += cnt - 1;
        ol(1 + dp[kk]);
        on;
      }
    } else {
      int kk = (a1 + p(2, n - 1 - i, cnt + 1)) % (cnt + 1);
      ol(1 + dp[kk]);
      on;
      // ol(kk);on;
    }
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

#define pb push_back
#define mp make_pair
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define ren(i, a, b) for (int i = a; i >= b; i--)
#define ff first
#define ss second
#define pll pair<long long int, long long int>
#define pii pair<int, int>
#define vll vector<long long int>
#define vii vector<int>
#define gi(n) scanf("%d", &n)
#define gll(n) scanf("%lld", &n)
#define gstr(n) scanf("%s", n)
#define gl(n) cin >> n
#define oi(n) printf("%d", n)
#define oll(n) printf("%lld", n)
#define ostr(n) printf("%s", n)
#define ol(n) cout << n
#define os cout << " "
#define on cout << "\n"
#define o2(a, b) cout << a << " " << b
#define all(n) n.begin(), n.end()
#define present(s, x) (s.find(x) != s.end())
#define cpresent(s, x) (find(all(s), x) != s.end())
#define tr(container, it)                                                      \
  for (__typeof(container.begin()) it = container.begin();                     \
       it != container.end(); it++)
using namespace std;

typedef unsigned long long int ull;
typedef long long int ll;
typedef long double ld;
typedef vector<vector<ll>> mat;

const int N = 1e6 + 5;

int dp[N];

int p(ll a, ll b, ll mod) {
  ll r = 1;
  while (b) {
    if (b % 2)
      r = (r * a) % mod;
    a = (a * a) % mod;
    b /= 2;
  }
  return r;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  dp[0] = 0;
  rep(i, 1, N - 1) dp[i] = 1 + dp[i % __builtin_popcount(i)];
  int n;
  string s;
  cin >> n >> s;
  int cnt = 0;
  rep(i, 0, n - 1) {
    if (s[i] == '1')
      cnt++;
  }
  int a1 = 0, a2 = 0;

  rep(i, 0, n - 1) {
    if (s[i] == '1') {
      a1 = (a1 + p(2, n - 1 - i, cnt + 1)) % (cnt + 1);
      if (cnt > 1)
        a2 = (a2 + p(2, n - 1 - i, cnt - 1)) % (cnt - 1);
    }
  }

  rep(i, 0, n - 1) {
    if (s[i] == '1') {
      if (cnt == 1) {
        ol("0\n");
      } else {
        int kk = (a2 - p(2, n - 1 - i, cnt - 1)) % (cnt - 1);
        if (kk < 0)
          kk += cnt - 1;
        ol(1 + dp[kk]);
        on;
      }
    } else {
      int kk = (a1 + p(2, n - 1 - i, cnt + 1)) % (cnt + 1);
      ol(1 + dp[kk]);
      on;
      // ol(kk);on;
    }
  }

  return 0;
}