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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define FI first
#define SE second
#define pb push_back
#define eb emplace_back
// #define mod 1000000007
#define all(c) (c).begin(), (c).end()
#define LB lower_bound
#define UB upper_bound
#define max3(a, b, c) max(c, max(a, b))
#define min3(a, b, c) min(c, min(a, b))
#define mems(s, n) memset(s, n, sizeof(s))
#define NINF -1e18
#define INF 1e18
#define int ll int
#define double long double
#define OOK order_of_key  // no of elements less than
#define FBO find_by_order // iterator pointing kth element;indexing starts from
                          // 0
#define CK3(x, y, z) cout << (x) << " " << (y) << " " << (z) << endl
#define CK4(a, b, c, d)                                                        \\
  cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl

typedef pair<int, int> PII;
typedef pair<pair<int, int>, int> PPII;
typedef pair<int, pair<int, int>> PIPI;
typedef map<int, int> MII;
typedef vector<int> VI;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int power(int a, int b, int mod) {
  int x = 1, y = a;
  while (b > 0) {
    if (b % 2) {
      x = (x * y) % mod;
    }
    y = (y * y) % mod;
    b /= 2;
  }
  return x % mod;
}

int getMod(string str, int n, int k) {

  // pwrTwo[i] will store ((2^i) % k)
  int pwrTwo[n];
  pwrTwo[0] = 1 % k;
  for (int i = 1; i < n; i++) {
    pwrTwo[i] = pwrTwo[i - 1] * (2 % k);
    pwrTwo[i] %= k;
  }

  // To store the result
  int res = 0;
  int i = 0, j = n - 1;
  while (i < n) {

    // If current bit is 1
    if (str[j] == '1') {

      // Add the current power of 2
      res += (pwrTwo[i]);
      res %= k;
    }
    i++;
    j--;
  }
  return res;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T = 1, T1 = 0; // cin>>T;
  while (T1++ < T) { // cout<<"Case #"<<T1<<": ";
    int n;
    cin >> n;
    VI dp(n + 1);
    int ans = 0;
    string x;
    cin >> x;
    for (int i = 0; i < n; i++)
      if (x[i] == '1')
        ans++;
    int a = getMod(x, n, ans + 1);
    int b = 0;
    if (ans != 1)
      b = getMod(x, n, ans - 1);
    for (int i = 0; i < n; i++) {
      if (i == 0)
        dp[i] = 0;
      else {
        int ss = 0;
        for (int j = 0; j < 32; j++)
          if ((i >> j) & 1)
            ss++;
        dp[i] = 1 + dp[i % ss];
      }
    }
    for (int i = 0; i < n; i++) {
      if (x[i] == '0') {
        int mod = ans + 1;
        int ab = power(2, n - i - 1, ans + 1);
        ab = a + ab;
        ab %= mod;
        ab += mod;
        ab %= mod;
        cout << dp[ab] + 1 << endl;
      } else {
        int mod = ans - 1;
        if (ans == 1) {
          cout << 0 << endl;
          continue;
        }
        int ab = power(2, n - i - 1, ans - 1);
        ab = b - ab;
        ab %= mod;
        ab += mod;
        ab %= mod;
        cout << dp[ab] + 1 << endl;
      }
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define FI first
#define SE second
#define pb push_back
#define eb emplace_back
// #define mod 1000000007
#define all(c) (c).begin(), (c).end()
#define LB lower_bound
#define UB upper_bound
#define max3(a, b, c) max(c, max(a, b))
#define min3(a, b, c) min(c, min(a, b))
#define mems(s, n) memset(s, n, sizeof(s))
#define NINF -1e18
#define INF 1e18
#define int ll int
#define double long double
#define OOK order_of_key  // no of elements less than
#define FBO find_by_order // iterator pointing kth element;indexing starts from
                          // 0
#define CK3(x, y, z) cout << (x) << " " << (y) << " " << (z) << endl
#define CK4(a, b, c, d)                                                        \\
  cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl

typedef pair<int, int> PII;
typedef pair<pair<int, int>, int> PPII;
typedef pair<int, pair<int, int>> PIPI;
typedef map<int, int> MII;
typedef vector<int> VI;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int power(int a, int b, int mod) {
  int x = 1, y = a;
  while (b > 0) {
    if (b % 2) {
      x = (x * y) % mod;
    }
    y = (y * y) % mod;
    b /= 2;
  }
  return x % mod;
}

int getMod(string str, int n, int k) {

  // pwrTwo[i] will store ((2^i) % k)
  int pwrTwo[n];
  pwrTwo[0] = 1 % k;
  for (int i = 1; i < n; i++) {
    pwrTwo[i] = pwrTwo[i - 1] * (2 % k);
    pwrTwo[i] %= k;
  }

  // To store the result
  int res = 0;
  int i = 0, j = n - 1;
  while (i < n) {

    // If current bit is 1
    if (str[j] == '1') {

      // Add the current power of 2
      res += (pwrTwo[i]);
      res %= k;
    }
    i++;
    j--;
  }
  return res;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T = 1, T1 = 0; // cin>>T;
  while (T1++ < T) { // cout<<"Case #"<<T1<<": ";
    int n;
    cin >> n;
    VI dp(n + 1);
    int ans = 0;
    string x;
    cin >> x;
    for (int i = 0; i < n; i++)
      if (x[i] == '1')
        ans++;
    int a = getMod(x, n, ans + 1);
    int b = getMod(x, n, ans - 1);
    for (int i = 0; i < n; i++) {
      if (i == 0)
        dp[i] = 0;
      else {
        int ss = 0;
        for (int j = 0; j < 32; j++)
          if ((i >> j) & 1)
            ss++;
        dp[i] = 1 + dp[i % ss];
      }
    }
    for (int i = 0; i < n; i++) {
      if (x[i] == '0') {
        int mod = ans + 1;
        int ab = power(2, n - i - 1, ans + 1);
        ab = a + ab;
        ab %= mod;
        ab += mod;
        ab %= mod;
        cout << dp[ab] + 1 << endl;
      } else {
        int mod = ans - 1;
        int ab = power(2, n - i - 1, ans - 1);
        ab = b - ab;
        ab %= mod;
        ab += mod;
        ab %= mod;
        cout << dp[ab] + 1 << endl;
      }
    }
  }

  return 0;
}