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

#include <bits/stdc++.h>

using namespace std;

int dp[200001];
long long int memo[2];
long long int mypow(long long int x, long long int n, long long int mod) {
  long long int res = 1;
  while (n > 0) {
    if (n % 2 == 1) {
      res = ((x % mod) * (res % mod) % mod) % mod;
      res %= mod;
    }
    x = ((x % mod) * (x % mod) % mod);
    x %= mod;
    n /= 2;
  }
  return res;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, cnt = 0;
  string s;

  cin >> n;
  cin >> s;

  if (n == 1) {
    if (s[0] == '0') {
      cout << 1 << '\\n';
      return 0;
    } else {
      cout << 0 << '\\n';
      return 0;
    }
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      cnt++;
    }
  }

  dp[0] = 0;
  for (int i = 1; i <= 200000; i++) {
    int bitcnt = 0;
    int val = i;
    while (val > 0) {
      if (val % 2 == 1) {
        bitcnt++;
      }
      val /= 2;
    }
    dp[i] = dp[i % bitcnt] + 1;
  }

  if (cnt > 1) {
    long long int temp = 0;
    for (int i = n - 1; i >= 0; i--) {
      int mod = cnt - 1;
      if (s[i] == '1') {
        temp = ((temp % mod) + (mypow(2, n - 1 - i, mod) % mod) % mod);
        temp %= mod;
      }
    }
    memo[0] = temp;
  }
  long long int temp = 0;
  for (int i = n - 1; i >= 0; i--) {
    int mod = cnt + 1;
    if (s[i] == '1') {
      temp = ((temp % mod) + (mypow(2, n - 1 - i, mod) % mod) % mod);
      temp %= mod;
    }
  }
  memo[1] = temp;

  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      long long int value = memo[1];
      int mod = cnt + 1;
      value = ((value % mod) + (mypow(2, n - 1 - i, mod) % mod) % mod);
      value %= mod;
      cout << dp[value] + 1 << '\\n';
    } else {
      if (cnt == 1) {
        cout << 0 << '\\n';
      } else {
        long long int value = memo[0];
        int mod = cnt - 1;
        value = ((value % mod) - (mypow(2, n - 1 - i, mod) % mod) % mod);
        value %= mod;
        if (value < 0) {
          value += mod;
        }
        cout << dp[value] + 1 << '\\n';
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int dp[200001];
long long int memo[2];
long long int mypow(int x, int n, int mod) {
  long long int res = 1;
  while (n > 0) {
    if (n % 2 == 1) {
      res = ((x % mod) * (res % mod) % mod) % mod;
      res %= mod;
    }
    x = ((x % mod) * (x % mod) % mod);
    x %= mod;
    n /= 2;
  }
  return res;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, cnt = 0;
  string s;

  cin >> n;
  cin >> s;

  if (n == 1) {
    if (s[0] == '0') {
      cout << 1 << '\\n';
      return 0;
    } else {
      cout << 0 << '\\n';
      return 0;
    }
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      cnt++;
    }
  }
  if (cnt == 0) {
    for (int i = 0; i < n; i++) {
      cout << 1 << '\\n';
    }
    return 0;
  }
  dp[0] = 0;
  for (int i = 1; i <= 200000; i++) {
    int bitcnt = 0;
    int val = i;
    while (val > 0) {
      if (val % 2 == 1) {
        bitcnt++;
      }
      val /= 2;
    }
    dp[i] = dp[i % bitcnt] + 1;
  }

  if (cnt > 1) {
    long long int temp = 0;
    for (int i = n - 1; i >= 0; i--) {
      int mod = cnt - 1;
      if (s[i] == '1') {
        temp = ((temp % mod) + (mypow(2, n - 1 - i, mod) % mod) % mod);
        temp %= mod;
      }
    }
    memo[0] = temp;
  }
  long long int temp = 0;
  for (int i = n - 1; i >= 0; i--) {
    int mod = cnt + 1;
    if (s[i] == '1') {
      temp = ((temp % mod) + (mypow(2, n - 1 - i, mod) % mod) % mod);
      temp %= mod;
    }
  }
  memo[1] = temp;

  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      long long int value = memo[1];
      int mod = cnt + 1;
      value = ((value % mod) + (mypow(2, n - 1 - i, mod) % mod) % mod);
      value %= mod;
      cout << dp[value] + 1 << '\\n';
    } else {
      if (cnt == 1) {
        cout << 0 << '\\n';
      } else {
        long long int value = memo[0];
        int mod = cnt - 1;
        value = ((value % mod) - (mypow(2, n - 1 - i, mod) % mod) % mod);
        value %= mod;
        if (value < 0) {
          value += mod;
        }
        cout << dp[value] + 1 << '\\n';
      }
    }
  }
  return 0;
}