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

// Problem : D - Anything Goes to Zero
// Contest : AtCoder - AIsing Programming Contest 2020
// URL : https://atcoder.jp/contests/aising2020/tasks/aising2020_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb push_back
#define all(x) begin(x), end(x)
#define setbits(x) __builtin_popcount(x)
int mod = 1e9 + 7;
int inf = 4e18;
const int N = 5e5 + 5;
int pwrTwo[N];
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;
}
int help(int n) {
  int ct = 0;
  while (n != 0) {
    int xxx = setbits(n);
    n = n % xxx;
    ++ct;
  }
  return ct;
}
signed main() {
  io int tt = 1;
  // cin>>tt;
  while (tt--) {
    int n;
    cin >> n;
    string s;
    cin >> s;
    int ct = 0;
    for (int i = 0; i < n; ++i) {
      if (s[i] == '1')
        ++ct;
    }
    int pwrmore[n], pwrless[n];
    pwrmore[0] = 1 % (ct + 1);
    if (ct != 1)
      pwrless[0] = 1 % (ct - 1);
    for (int i = 1; i < n; ++i) {
      pwrmore[i] = pwrmore[i - 1] * (2 % (ct + 1));
      pwrmore[i] %= (ct + 1);
    }
    if (ct != 1)
      for (int i = 1; i < n; ++i) {
        pwrless[i] = pwrless[i - 1] * (2 % (ct - 1));
        pwrless[i] %= (ct - 1);
      }
    int more = getMod(s, n, ct + 1);
    int less = 0;
    if (ct != 1)
      less = getMod(s, n, ct - 1);
    // cout<<more<<" "<<less<<"\\n";
    for (int i = 0; i < n; ++i) {
      if (s[i] == '1') {
        if (ct == 1) {
          cout << 0 << "\\n";
          continue;
        } else {
          int xxx = (less + ct - 1 - pwrless[n - 1 - i]) % (ct - 1);
          cout << help(xxx) + 1 << "\\n";
        }
      } else // '0'
      {
        int xxx = (more + pwrmore[n - 1 - i]) % (ct + 1);
        cout << help(xxx) + 1 << "\\n";
      }
    }
  }
  return 0;
}


Error: 0

Fixed submission:


// Problem : D - Anything Goes to Zero
// Contest : AtCoder - AIsing Programming Contest 2020
// URL : https://atcoder.jp/contests/aising2020/tasks/aising2020_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb push_back
#define all(x) begin(x), end(x)
#define setbits(x) __builtin_popcount(x)
int mod = 1e9 + 7;
int inf = 4e18;
const int N = 5e5 + 5;
int pwrTwo[N];
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;
}
int help(int n) {
  int ct = 0;
  while (n != 0) {
    int xxx = setbits(n);
    n = n % xxx;
    ++ct;
  }
  return ct;
}
signed main() {
  io int tt = 1;
  // cin>>tt;
  while (tt--) {
    int n;
    cin >> n;
    string s;
    cin >> s;
    int ct = 0;
    for (int i = 0; i < n; ++i) {
      if (s[i] == '1')
        ++ct;
    }
    int pwrmore[n], pwrless[n];
    pwrmore[0] = 1 % (ct + 1);
    pwrless[0] = 1 % (ct - 1);
    for (int i = 1; i < n; ++i) {
      pwrmore[i] = pwrmore[i - 1] * (2 % (ct + 1));
      pwrmore[i] %= (ct + 1);
    }
    for (int i = 1; i < n; ++i) {
      pwrless[i] = pwrless[i - 1] * (2 % (ct - 1));
      pwrless[i] %= (ct - 1);
    }
    int more = getMod(s, n, ct + 1);
    int less = getMod(s, n, ct - 1);
    // cout<<more<<" "<<less<<"\\n";
    for (int i = 0; i < n; ++i) {
      if (s[i] == '1') {
        if (ct == 1) {
          cout << 0 << "\\n";
          continue;
        } else {
          int xxx = (less + ct - 1 - pwrless[n - 1 - i]) % (ct - 1);
          cout << help(xxx) + 1 << "\\n";
        }
      } else // '0'
      {
        int xxx = (more + pwrmore[n - 1 - i]) % (ct + 1);
        cout << help(xxx) + 1 << "\\n";
      }
    }
  }
  return 0;
}