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

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define IOS                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb push_back
#define mci map<char, int>
#define msi map<string, int>
#define pii pair<int, int>
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
// #define mod 1000000007 //998244353
#define mii map<int, int>
#define rep(i, x, y) for (int i = x; i < y; i++)
#define fill(a, b) memset(a, b, sizeof(a))
#define vi vector<int>

// clear adj and visited vector declared globally after each test case
// check for long long overflow
// while adding and subs check if mod becomes -ve
// while using an integer directly in a builtin function add ll
// Mod wale question mein last mein if dalo ie. Ans<0 then ans+=mod;
// Dont keep array name as size or any other key word

const long long N = 200005, INF = 2000000000000000000;
// lld pi = 3.1415926535897932;
int lcm(int a, int b) {
  int g = __gcd(a, b);
  return a / g * b;
}
int power(int a, int b, int p) {
  if (a == 0)
    return 0;
  int res = 1;
  a %= p;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % p;
    b >>= 1;
    a = (a * a) % p;
  }
  return res;
}

int testcaseva;

int sq(int x) { return 1ll * x * x; }

void rec(int n) {
  int p = n;
  if (n == 0)
    return;
  cout << p - n << " ";
  return rec(n - 1);
}
ll n, c;
ll ar[100005];
bool valid(int m, int c, int n) {
  int cnt = 0;
  int pr = INT_MIN;
  for (int i = 0; i < n; i++) {
    if (ar[i] - pr >= m)
      cnt++, pr = ar[i];
  }
  if (cnt >= c)
    return true;
  else
    return false;
}
int num_to_bits[16] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};

unsigned int countSetBitsRec(unsigned int num) {
  int nibble = 0;
  if (0 == num)
    return num_to_bits[0];

  nibble = num & 0xf;

  return num_to_bits[nibble] + countSetBitsRec(num >> 4);
}
int binaryToDecimal(string n) {
  string num = n;
  int dec_value = 0;

  int base = 1;

  int len = num.length();
  for (int i = len - 1; i >= 0; i--) {
    if (num[i] == '1')
      dec_value += base;
    base = base * 2;
  }

  return dec_value;
}
int toggleKthBit(int n, int k) { return (n ^ (1 << (k - 1))); }
ll pw(ll n, ll mod) {
  ll ans = 1;

  ll x = 2, y = n;
  while (y != 0) {
    if (y % 2)
      ans = ((ans % mod) * (x % mod)) % mod;
    x = ((x % mod) * (x % mod)) % mod;
    y /= 2;
  }
  return ans;
}

ll cunt(ll n) {
  ll cnt = 0;
  while (n != 0) {
    if (n % 2)
      cnt++;
    n /= 2;
  }
  return cnt;
}

void func(ll n) {
  ll cnt = 1;
  while (n > 0) {
    ll temp = cunt(n);
    cnt++;
    n = n % temp;
  }
  cout << cnt << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // #ifndef ONLINE_JUDGE
  //   freopen("input.txt", "r", stdin);
  //   freopen("output.txt", "w", stdout);
  // #endif
  //  int t;
  //  cin >> t;
  //  while (t--)
  //  int t=1;
  //  //cin >> t;
  //  while(t--){
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll mn = 0, mx = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1')
      mn++;
  }
  if (mn < n) {
    mx = mn + 1;
    mn--;
  } else {
    mn--;
    mx = mn;
  }
  if (mn == 0 || mx == 0) {
    for (ll i = 0; i < n; i++) {
      if (s[i] == '1')
        cout << 0 << endl;
      else {
        if (i == n - 1)
          cout << 2 << endl;
        else
          cout << 1 << endl;
      }
    }
    return 0;
  }
  ll fwefwef = 0, efefwf = 0;

  reverse(s.begin(), s.end());
  ll temp1 = 1, temp2 = 1;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1') {
      fwefwef = ((fwefwef % mn) + (temp1 % mn)) % mn;
      efefwf = ((efefwf % mx) + (temp2 % mx)) % mx;
    }
    temp1 = ((temp1 % mn) * 2) % mn;
    temp2 = ((temp2 % mx) * 2) % mx;
  }
  vector<ll> ans;
  for (ll i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      ll temp = fwefwef - pw(i, mn);
      temp += mn;
      temp = temp % mn;
      func(temp);
    } else {
      ll temp = efefwf + pw(i, mx);
      temp = temp % mx;
      func(temp);
    }
  }
  for (int i = 0; i < ans.size(); i++)
    cout << ans[i] << endl;

  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define IOS                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb push_back
#define mci map<char, int>
#define msi map<string, int>
#define pii pair<int, int>
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
// #define mod 1000000007 //998244353
#define mii map<int, int>
#define rep(i, x, y) for (int i = x; i < y; i++)
#define fill(a, b) memset(a, b, sizeof(a))
#define vi vector<int>

// clear adj and visited vector declared globally after each test case
// check for long long overflow
// while adding and subs check if mod becomes -ve
// while using an integer directly in a builtin function add ll
// Mod wale question mein last mein if dalo ie. Ans<0 then ans+=mod;
// Dont keep array name as size or any other key word

const long long N = 200005, INF = 2000000000000000000;
// lld pi = 3.1415926535897932;
int lcm(int a, int b) {
  int g = __gcd(a, b);
  return a / g * b;
}
int power(int a, int b, int p) {
  if (a == 0)
    return 0;
  int res = 1;
  a %= p;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % p;
    b >>= 1;
    a = (a * a) % p;
  }
  return res;
}

int testcaseva;

int sq(int x) { return 1ll * x * x; }

void rec(int n) {
  int p = n;
  if (n == 0)
    return;
  cout << p - n << " ";
  return rec(n - 1);
}
ll n, c;
ll ar[100005];
bool valid(int m, int c, int n) {
  int cnt = 0;
  int pr = INT_MIN;
  for (int i = 0; i < n; i++) {
    if (ar[i] - pr >= m)
      cnt++, pr = ar[i];
  }
  if (cnt >= c)
    return true;
  else
    return false;
}
int num_to_bits[16] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};

unsigned int countSetBitsRec(unsigned int num) {
  int nibble = 0;
  if (0 == num)
    return num_to_bits[0];

  nibble = num & 0xf;

  return num_to_bits[nibble] + countSetBitsRec(num >> 4);
}
int binaryToDecimal(string n) {
  string num = n;
  int dec_value = 0;

  int base = 1;

  int len = num.length();
  for (int i = len - 1; i >= 0; i--) {
    if (num[i] == '1')
      dec_value += base;
    base = base * 2;
  }

  return dec_value;
}
int toggleKthBit(int n, int k) { return (n ^ (1 << (k - 1))); }
ll pw(ll n, ll mod) {
  ll ans = 1;

  ll x = 2, y = n;
  while (y != 0) {
    if (y % 2)
      ans = ((ans % mod) * (x % mod)) % mod;
    x = ((x % mod) * (x % mod)) % mod;
    y /= 2;
  }
  return ans;
}

ll cunt(ll n) {
  ll cnt = 0;
  while (n != 0) {
    if (n % 2)
      cnt++;
    n /= 2;
  }
  return cnt;
}

void func(ll n) {
  ll cnt = 1;
  while (n > 0) {
    ll temp = cunt(n);
    cnt++;
    n = n % temp;
  }
  cout << cnt << endl;
}

int main() {
  // #ifndef ONLINE_JUDGE
  //   freopen("input.txt", "r", stdin);
  //   freopen("output.txt", "w", stdout);
  // #endif
  //  int t;
  //  cin >> t;
  //  while (t--)
  int t = 1;
  // cin >> t;
  while (t--) {
    ll n;
    cin >> n;
    string s;
    cin >> s;
    ll mn = 0, mx = 0;
    for (ll i = 0; i < n; i++) {
      if (s[i] == '1')
        mn++;
    }
    if (mn < n) {
      mx = mn + 1;
      mn--;
    } else {
      mn--;
      mx = mn;
    }
    if (mn == 0 || mx == 0) {
      for (ll i = 0; i < n; i++) {
        if (s[i] == '1')
          cout << 0 << endl;
        else {
          if (i == n - 1)
            cout << 2 << endl;
          else
            cout << 1 << endl;
        }
      }
      return 0;
    }
    ll fwefwef = 0, efefwf = 0;

    reverse(s.begin(), s.end());
    ll temp1 = 1, temp2 = 1;
    for (ll i = 0; i < n; i++) {
      if (s[i] == '1') {
        fwefwef = ((fwefwef % mn) + (temp1 % mn)) % mn;
        efefwf = ((efefwf % mx) + (temp2 % mx)) % mx;
      }
      temp1 = ((temp1 % mn) * 2) % mn;
      temp2 = ((temp2 % mx) * 2) % mx;
    }
    vector<ll> ans;
    for (ll i = n - 1; i >= 0; i--) {
      if (s[i] == '1') {
        ll temp = fwefwef - pw(i, mn);
        temp += mn;
        temp = temp % mn;
        func(temp);
      } else {
        ll temp = efefwf + pw(i, mx);
        temp = temp % mx;
        func(temp);
      }
    }
    for (int i = 0; i < n; i++)
      cout << ans[i] << endl;
  }
  return 0;
}