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

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

int main() {
  int n, one = 0;
  string xx;
  cin >> n >> xx;
  for (int i = 0; i < xx.size(); i++) {
    if (xx[i] == '1')
      one++;
  }

  int p = 0, q = 0, r = 1, s = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (xx[i] == '1') {
      p = (p + r) % (one + 1);
      if (one != 1)
        q = (q + s) % (one - 1);
    }
    r = (r * 2) % (one + 1);
    if (one != 1)
      s = (s * 2) % (one - 1);
  }
  vector<int> ans(n, 0), now(n);
  int x = 1, y = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (xx[i] == '1') {
      if (one != 1)
        now[i] = (q + (one - 1 - x)) % (one - 1);
      else {
        now[i] = 0;
        ans[i] = -1;
      }
    } else {
      now[i] = (p + y) % (one + 1);
    }
    if (one != 1)
      x = (x * 2) % (one - 1);
    y = (y * 2) % (one + 1);
    if (now[i] != 0)
      ans[i]++;
  }
  for (int i = 0; i < n; i++) {
    if (ans[i] == 1) {
      one = 0;
      for (int j = 0; j < 22; j++) {
        if (now[i] & (1 << j))
          one++;
      }
      now[i] = now[i] % one;
      if (now[i] != 0)
        ans[i]++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (ans[i] == 2) {
      one = 0;
      for (int j = 0; j < 8; j++) {
        if (now[i] & (1 << j))
          one++;
      }
      now[i] = now[i] % one;
      if (now[i] != 0)
        ans[i]++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (ans[i] == 3) {
      one = 0;
      for (int j = 0; j < 5; j++) {
        if (now[i] & (1 << j))
          one++;
      }
      now[i] = now[i] % one;
      if (now[i] != 0)
        ans[i]++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (ans[i] == 4) {
      one = 0;
      for (int j = 0; j < 4; j++) {
        if (now[i] & (1 << j))
          one++;
      }
      now[i] = now[i] % one;
      if (now[i] != 0)
        ans[i]++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (ans[i] == 5) {
      one = 0;
      for (int j = 0; j < 3; j++) {
        if (now[i] & (1 << j))
          one++;
      }
      now[i] = now[i] % one;
      if (now[i] != 0)
        ans[i]++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (ans[i] == 6) {
      one = 0;
      for (int j = 0; j < 2; j++) {
        if (now[i] & (1 << j))
          one++;
      }
      now[i] = now[i] % one;
      if (now[i] != 0)
        ans[i]++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (ans[i] == 7) {
      one = 0;
      for (int j = 0; j < 1; j++) {
        if (now[i] & (1 << j))
          one++;
      }
      now[i] = now[i] % one;
      if (now[i] != 0)
        ans[i]++;
    }
  }

  for (int i = 0; i < n; i++) {
    cout << ans[i] + 1 << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  int n, one = 0;
  string xx;
  cin >> n >> xx;
  for (int i = 0; i < xx.size(); i++) {
    if (xx[i] == '1')
      one++;
  }
  int p = 0, q = 0, r = 1, s = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (xx[i] == '1') {
      p = (p + r) % (one + 1);
      q = (q + s) % (one - 1);
    }
    r = (r * 2) % (one + 1);
    s = (s * 2) % (one - 1);
  }
  vector<int> ans(n, 0), now(n);
  int x = 1, y = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (xx[i] == '1') {
      now[i] = (q + (one - 1 - x)) % (one - 1);
    } else {
      now[i] = (p + y) % (one + 1);
    }
    x = (x * 2) % (one - 1);
    y = (y * 2) % (one + 1);
    if (now[i] != 0)
      ans[i]++;
  }
  for (int i = 0; i < n; i++) {
    if (ans[i] == 1) {
      one = 0;
      for (int j = 0; j < 22; j++) {
        if (now[i] & (1 << j))
          one++;
      }
      now[i] = now[i] % one;
      if (now[i] != 0)
        ans[i]++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (ans[i] == 2) {
      one = 0;
      for (int j = 0; j < 8; j++) {
        if (now[i] & (1 << j))
          one++;
      }
      now[i] = now[i] % one;
      if (now[i] != 0)
        ans[i]++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (ans[i] == 3) {
      one = 0;
      for (int j = 0; j < 5; j++) {
        if (now[i] & (1 << j))
          one++;
      }
      now[i] = now[i] % one;
      if (now[i] != 0)
        ans[i]++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (ans[i] == 4) {
      one = 0;
      for (int j = 0; j < 4; j++) {
        if (now[i] & (1 << j))
          one++;
      }
      now[i] = now[i] % one;
      if (now[i] != 0)
        ans[i]++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (ans[i] == 5) {
      one = 0;
      for (int j = 0; j < 3; j++) {
        if (now[i] & (1 << j))
          one++;
      }
      now[i] = now[i] % one;
      if (now[i] != 0)
        ans[i]++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (ans[i] == 6) {
      one = 0;
      for (int j = 0; j < 2; j++) {
        if (now[i] & (1 << j))
          one++;
      }
      now[i] = now[i] % one;
      if (now[i] != 0)
        ans[i]++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (ans[i] == 7) {
      one = 0;
      for (int j = 0; j < 1; j++) {
        if (now[i] & (1 << j))
          one++;
      }
      now[i] = now[i] % one;
      if (now[i] != 0)
        ans[i]++;
    }
  }

  for (int i = 0; i < n; i++) {
    cout << ans[i] + 1 << endl;
  }
}