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

#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define mod 1000000007
#define FT()                                                                   \\
  int t;                                                                       \\
  scanf("%d", &t);                                                             \\
  while (t--)
#define pb push_back
#define nl printf("\\n")
#define fi(i, start, end) for (int i = start; i < (int)end; ++i)
#define ip(n) scanf("%d", &n)
#define mz(a) memset(a, 0, sizeof(a))
#define inpArr(A, n) fi(i, 0, n) ip(A[i]);
#define print(a)                                                               \\
  for (auto i : a)                                                             \\
    cout << i << " ";                                                          \\
  nl;
#define Fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
using namespace std;
ll max(ll a, ll b) { return a > b ? a : b; }
ll min(ll a, ll b) { return a < b ? a : b; }

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
// freopen("output.txt","w",stdout);
#endif

  int n;
  cin >> n;
  string x;
  cin >> x;

  ll smb = 0, sma = 0;
  ll pb = 1, pa = 1;
  int c = 0;
  ll dpb[n], dpa[n];
  mz(dpa);
  mz(dpb);
  for (int i = n - 1; i >= 0; --i) {
    c += (x[i] == '1');
  }
  for (int i = n - 1; i >= 0; --i) {
    if (x[i] == '1') {
      smb += pb;
      sma += pa;
      if (c > 1)
        smb %= (c - 1);
      sma %= (c + 1);
    }
    if (c > 1)
      dpb[i] = pb % (c - 1);
    dpa[i] = pa % (c + 1);

    pa *= 2;
    pa %= (c + 1);
    if (c > 1) {
      pb *= 2;
      pb %= (c - 1);
    }
  }
  if (c != 1)
    for (int i = 0; i < n; i++) {
      if (x[i] == '0') {
        ll smt = sma + dpa[i];
        smt %= (c + 1);
        int count = 1;
        while (smt != 0) {
          ll temp = smt;
          int cc = 0;
          while (temp) {
            cc += (temp & 1);
            temp >>= 1;
          }
          if (cc == 0)
            break;
          smt = smt % cc;
          count++;
        }
        cout << count << "\\n";
      } else if (x[i] == '1') {
        ll smt = smb - dpb[i];
        if (smt < 0)
          smt += (c - 1);
        else
          smt %= (c - 1);
        int count = 1;
        while (smt > 0) {
          ll temp = smt;
          int cc = 0;
          while (temp) {
            cc += (temp & 1);
            temp >>= 1;
          }
          if (cc == 0)
            break;
          smt = smt % cc;
          count++;
        }
        cout << count << "\\n";
      }
    }

  else {
    for (int i = 0; i < n; i++) {
      if (x[i] == '1')
        cout << "0\\n";
      else {
        if (i == n - 1)
          cout << "2\\n";
        else
          cout << "1\\n";
      }
    }
  }

#ifndef ONLINE_JUDGE
  cout << "\\nTime Elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " s";
#endif

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define mod 1000000007
#define FT()                                                                   \\
  int t;                                                                       \\
  scanf("%d", &t);                                                             \\
  while (t--)
#define pb push_back
#define nl printf("\\n")
#define fi(i, start, end) for (int i = start; i < (int)end; ++i)
#define ip(n) scanf("%d", &n)
#define mz(a) memset(a, 0, sizeof(a))
#define inpArr(A, n) fi(i, 0, n) ip(A[i]);
#define print(a)                                                               \\
  for (auto i : a)                                                             \\
    cout << i << " ";                                                          \\
  nl;
#define Fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
using namespace std;
ll max(ll a, ll b) { return a > b ? a : b; }
ll min(ll a, ll b) { return a < b ? a : b; }

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
// freopen("output.txt","w",stdout);
#endif

  int n;
  cin >> n;
  string x;
  cin >> x;

  ll smb = 0, sma = 0;
  ll pb = 1, pa = 1;
  int c = 0;
  ll dpb[n], dpa[n];
  mz(dpa);
  mz(dpb);
  for (int i = n - 1; i >= 0; --i) {
    c += (x[i] == '1');
  }
  for (int i = n - 1; i >= 0; --i) {
    if (x[i] == '1') {
      smb += pb;
      sma += pa;
      if (c)
        smb %= (c - 1);
      sma %= (c + 1);
    }
    if (c)
      dpb[i] = pb % (c - 1);
    dpa[i] = pa % (c + 1);

    pa *= 2;
    pa %= (c + 1);
    if (c) {
      pb *= 2;
      pb %= (c - 1);
    }
  }
  /*
  fi(i,0,n)
    cout<<dpa[i]<<" ";
  nl;
  fi(i,0,n)
    cout<<dpb[i]<<" ";
  nl;
  */
  // cout<<smb<<" "<<sm<<" "<<sma<<"\\n";
  for (int i = 0; i < n; i++) {
    if (x[i] == '0') {
      ll smt = sma + dpa[i];
      smt %= (c + 1);
      int count = 1;
      while (smt != 0) {
        ll temp = smt;
        int cc = 0;
        while (temp) {
          cc += (temp & 1);
          temp >>= 1;
        }
        if (cc == 0)
          break;
        smt = smt % cc;
        count++;
      }
      cout << count << "\\n";
    } else if (x[i] == '1') {
      if (c == 1) {
        cout << 1 << "\\n";
        continue;
      }

      ll smt = smb - dpb[i];
      if (smt < 0)
        smt += (c - 1);
      else
        smt %= (c - 1);
      int count = 1;
      while (smt != 0) {
        ll temp = smt;
        int cc = 0;
        while (temp) {
          cc += (temp & 1);
          temp >>= 1;
        }
        if (cc == 0)
          break;
        smt = smt % cc;
        count++;
      }
      cout << count << "\\n";
    }
  }

#ifndef ONLINE_JUDGE
  cout << "\\nTime Elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " s";
#endif

  return 0;
}