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

#include <bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

#define INF 1000000000
#define fastIO                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cout.tie(0);                                                                 \\
  cin.tie(0);
#define endl '\\n'
#define foreach(v, c)                                                          \\
  for (typeof((c).begin()) v = (c).begin(); v != (c).end(); ++v)
#define all(a) a.begin(), a.end()
#define in(a, b) ((b).find(a) != (b).end())
#define fill(a, v) memset(a, v, sizeof a)
#define sz(a) ((int)(a.size()))
#define mp make_pair
#define pb push_back

int pwrTwo1[200010];
int pwrTwo2[200010];
int ans[200010];
int getMod(string str, int n, int 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 cnt(string s) {
  int c = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '1')
      c++;
  }
  return c;
}
string decToBinary(int n) {
  // array to store binary number
  int binaryNum[32];
  string ans = "";
  // counter for binary array
  int i = 0;
  while (n > 0) {

    // storing remainder in binary array
    binaryNum[i] = n % 2;
    n = n / 2;
    i++;
  }

  // printing binary array in reverse order
  for (int j = i - 1; j >= 0; j--)
    if (binaryNum[j])
      ans.append("1");
    else
      ans.append("0");
  return ans;
}
int flag[200010];
int main() {
  int n;
  string s;
  cin >> n >> s;
  int k = cnt(s);
  pwrTwo1[0] = 1 % (k + 1);
  int res1 = 0;
  int res2 = 0;
  for (int i = 1; i < n; i++) {
    pwrTwo1[i] = pwrTwo1[i - 1] * (2 % (k + 1));
    pwrTwo1[i] %= (k + 1);
  }
  if (k >= 2) {
    pwrTwo2[0] = 1 % (k - 1);
    for (int i = 1; i < n; i++) {
      pwrTwo2[i] = pwrTwo2[i - 1] * (2 % (k - 1));
      pwrTwo2[i] %= (k - 1);
    }
  }
  int i = 0, j = n - 1;
  while (i < n) {

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

      // Add the current power of 2
      res1 += (pwrTwo1[i]);
      res1 %= (k + 1);
    }
    i++;
    j--;
  }

  i = 0, j = n - 1;
  if (k >= 2) {
    while (i < n) {

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

        // Add the current power of 2
        res2 += (pwrTwo2[i]);
        res2 %= (k - 1);
      }
      i++;
      j--;
    }
  }
  i = 0, j = n - 1;
  while (i < n) {

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

      // Add the current power of 2
      if (k >= 2) {
        ans[j] = res2 - (pwrTwo2[i]) + k - 1;
        ans[j] %= (k - 1);
      } else {
        flag[j] = 1;
        ans[j] = 0;
      }
    } else {
      // Add the current power of 2
      ans[j] = res1 + (pwrTwo1[i]);
      ans[j] %= (k + 1);
    }
    i++;
    j--;
  }
  for (i = 0; i < n; i++) {
    int x = ans[i];
    int cnt1 = 0;
    string s1 = decToBinary(x);
    // cout<<x<<endl;
    while (cnt(s1) != 0) {
      x = getMod(s1, s1.size(), cnt(s1));
      s1 = decToBinary(x);
      cnt1++;
    }
    if (!flag[i])
      ans[i] = 1 + cnt1;
  }
  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

#define INF 1000000000
#define fastIO                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cout.tie(0);                                                                 \\
  cin.tie(0);
#define endl '\\n'
#define foreach(v, c)                                                          \\
  for (typeof((c).begin()) v = (c).begin(); v != (c).end(); ++v)
#define all(a) a.begin(), a.end()
#define in(a, b) ((b).find(a) != (b).end())
#define fill(a, v) memset(a, v, sizeof a)
#define sz(a) ((int)(a.size()))
#define mp make_pair
#define pb push_back

int pwrTwo1[200010];
int pwrTwo2[200010];
int ans[200010];
int getMod(string str, int n, int 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 cnt(string s) {
  int c = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '1')
      c++;
  }
  return c;
}
string decToBinary(int n) {
  // array to store binary number
  int binaryNum[32];
  string ans = "";
  // counter for binary array
  int i = 0;
  while (n > 0) {

    // storing remainder in binary array
    binaryNum[i] = n % 2;
    n = n / 2;
    i++;
  }

  // printing binary array in reverse order
  for (int j = i - 1; j >= 0; j--)
    if (binaryNum[j])
      ans.append("1");
    else
      ans.append("0");
  return ans;
}

int main() {
  int n;
  string s;
  cin >> n >> s;
  int k = cnt(s);
  pwrTwo1[0] = 1 % (k + 1);
  int res1 = 0;
  int res2 = 0;
  for (int i = 1; i < n; i++) {
    pwrTwo1[i] = pwrTwo1[i - 1] * (2 % (k + 1));
    pwrTwo1[i] %= (k + 1);
  }
  pwrTwo2[0] = 1 % (k - 1);
  for (int i = 1; i < n; i++) {
    pwrTwo2[i] = pwrTwo2[i - 1] * (2 % (k - 1));
    pwrTwo2[i] %= (k - 1);
  }

  int i = 0, j = n - 1;
  while (i < n) {

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

      // Add the current power of 2
      res1 += (pwrTwo1[i]);
      res1 %= (k + 1);
    }
    i++;
    j--;
  }

  i = 0, j = n - 1;
  while (i < n) {

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

      // Add the current power of 2
      res2 += (pwrTwo2[i]);
      res2 %= (k - 1);
    }
    i++;
    j--;
  }
  i = 0, j = n - 1;
  while (i < n) {

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

      // Add the current power of 2
      ans[j] = res2 - (pwrTwo2[i]) + k - 1;
      ans[j] %= (k - 1);
    } else {
      // Add the current power of 2
      ans[j] = res1 + (pwrTwo1[i]);
      ans[j] %= (k + 1);
    }
    i++;
    j--;
  }
  for (i = 0; i < n; i++) {
    int x = ans[i];
    int cnt1 = 0;
    string s1 = decToBinary(x);
    // cout<<x<<endl;
    while (cnt(s1) != 0) {
      x = getMod(s1, s1.size(), cnt(s1));
      s1 = decToBinary(x);
      cnt1++;
    }
    ans[i] = 1 + cnt1;
  }
  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;
}