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

// is there any case of -1 or any special corner cases ?
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int, int>
#define all(v) v.begin(), v.end()
#define zeros(arr) memset(arr, 0, sizeof(arr));
#define precise(num)                                                           \\
  cout << fixed;                                                               \\
  cout << std::setprecision(num)
#define nl cout << "\\n";
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
const int maxx = 3 * 1e5, limit = 9 * 1e18, MOD = 1e9 + 7;
string binary(int n) {
  bitset<60> b(n);
  string p = b.to_string();
  return p;
}
int decimal(string n) {
  int dec_value = 0;
  int base = 1;
  for (int i = n.size() - 1; i >= 0; i--) {
    if (n[i] == '1')
      dec_value += base;
    base = base * 2;
  }
  return dec_value;
}
bool isPrime(int n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;
  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;
  return true;
}
int pow(int x, int n, int mod) {
  if (n == 0)
    return 1;
  int res = pow(x, n / 2, mod);
  res = (res * res) % mod;
  if (n % 2)
    res = (res * x) % mod;
  return res;
}
#define boost                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl '\\n';
int count(int num) {
  int pc = 0;
  while (num) {
    if (num % 2)
      pc++;
    num /= 2;
  }
  return pc;
}
int find(int n1, int n2) {

  int res = 0;
  while (1) {
    res++;
    int val = n1 % n2;
    n1 = val;
    n2 = count(n1);
    if (val == 0)
      break;
  }
  return res;
}
int32_t main() {
  boost;
  int n;
  cin >> n;
  string s;
  cin >> s;

  int one = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      one++;
  }
  vector<int> v1;
  vector<int> v2;
  vector<int> v3;
  int sum1 = 0;
  int sum2 = 0;
  int sum3 = 0;

  for (int i = 0; i < n; i++) {

    //  int num1 = pow(2,n-i-1 , one);
    int num2 = pow(2, n - i - 1, one + 1);

    v2.pb(num2);

    if (s[i] == '1') {
      sum2 += num2;
      //   sum3+=num3;
      //   sum1%=one;
      sum2 %= (one + 1);
    }

    if (one - 1 > 0) {
      int num3 = pow(2, n - i - 1, one - 1);
      v3.pb(num3);
      if (s[i] == '1') {
        sum3 += num3;
        sum3 %= (one - 1);
      }
    }
  }
  for (int i = 0; i < s.size(); i++) {

    if (s[i] == '1') {

      if (one - 1 == 0) {
        cout << "0\\n";
        continue;
      }
      int val = sum3 - v3[i];
      while (val < 0)
        val = val + (one - 1);
      cout << find(val, one - 1) << endl;
    } else {
      int val = sum2 + v2[i];
      cout << find(val, one + 1) << endl;
    }
  }
}

Error: 0

Fixed submission:

// is there any case of -1 or any special corner cases ?
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int, int>
#define all(v) v.begin(), v.end()
#define zeros(arr) memset(arr, 0, sizeof(arr));
#define precise(num)                                                           \\
  cout << fixed;                                                               \\
  cout << std::setprecision(num)
#define nl cout << "\\n";
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
const int maxx = 3 * 1e5, limit = 9 * 1e18, MOD = 1e9 + 7;
string binary(int n) {
  bitset<60> b(n);
  string p = b.to_string();
  return p;
}
int decimal(string n) {
  int dec_value = 0;
  int base = 1;
  for (int i = n.size() - 1; i >= 0; i--) {
    if (n[i] == '1')
      dec_value += base;
    base = base * 2;
  }
  return dec_value;
}
bool isPrime(int n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;
  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;
  return true;
}
int pow(int x, int n, int mod) {
  if (n == 0)
    return 1;
  int res = pow(x, n / 2, mod);
  res = (res * res) % mod;
  if (n % 2)
    res = (res * x) % mod;
  return res;
}
#define boost                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl '\\n';
int count(int num) {
  int pc = 0;
  while (num) {
    if (num % 2)
      pc++;
    num /= 2;
  }
  return pc;
}
int find(int n1, int n2) {

  int res = 0;
  while (1) {
    res++;
    int val = n1 % n2;
    n1 = val;
    n2 = count(n1);
    if (val == 0)
      break;
  }
  return res;
}
int32_t main() {
  boost;
  int n;
  cin >> n;
  string s;
  cin >> s;

  int one = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      one++;
  }
  vector<int> v1;
  vector<int> v2;
  vector<int> v3;
  int sum1 = 0;
  int sum2 = 0;
  int sum3 = 0;

  for (int i = 0; i < n; i++) {

    //  int num1 = pow(2,n-i-1 , one);
    int num2 = pow(2, n - i - 1, one + 1);
    int num3 = pow(2, n - i - 1, one - 1);

    v2.pb(num2);
    v3.pb(num3);

    if (s[i] == '1') {
      sum2 += num2;
      sum3 += num3;
      //   sum1%=one;
      sum2 %= (one + 1);
      sum3 %= (one - 1);
    }
  }
  for (int i = 0; i < s.size(); i++) {

    if (s[i] == '1') {
      int val = sum3 - v3[i];
      while (val < 0)
        val = val + (one - 1);

      cout << find(val, one - 1) << endl;
    } else {
      int val = sum2 + v2[i];
      cout << find(val, one + 1) << endl;
    }
  }
}