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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
typedef struct {
  ll a, b, c;
} T;
using P = pair<int, int>;
ll N, K, H, W;
ll ans;
ll A[200005];
ll B[200005];
ll C[200005];
string S;
ll M = 1000000007;
ll cnt;
ll a;
ll b;
bool operator<(const T &T1, const T &T2) { return T2.a < T1.a; };
priority_queue<T> s;

int main() {
  cin >> N;
  cin >> S;
  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      A[i] = 1;
    }
    if (A[i] == 1) {
      cnt++;
    }
  }
  T t;
  t.a = 0;
  t.b = 0;
  t.c = 1;
  s.push(t);
  t.a = 1;
  t.b = 1;
  t.c = 1;
  s.push(t);
  ll n = 20;
  while (!s.empty()) {
    T p = s.top();
    s.pop();
    if (p.c >= n) {
      continue;
    }
    if (p.b != 0) {
      if (p.a <= 200005) {
        B[p.a] = B[p.a % p.b] + 1;
      }
    }
    t.a = p.a + (1 << p.c);
    t.b = p.b + 1;
    t.c = p.c + 1;
    s.push(t);
    t.a = p.a;
    t.b = p.b;
    t.c = p.c + 1;
    s.push(t);
  }
  ll k = 1;
  ll l = 1;
  for (ll i = N - 1; i >= 0; i--) {
    if (cnt > 1) {
      a = (a + k * A[i]) % (cnt - 1);
      k = (k * 2) % (cnt - 1);
    }
    b = (b + l * A[i]) % (cnt + 1);
    l = (l * 2) % (cnt + 1);
  }

  k = 1;
  l = 1;
  for (ll i = N - 1; i >= 0; i--) {
    if (A[i] == 0) {
      C[i] = B[(b + l) % (cnt + 1)];
    } else {
      if (cnt > 1) {
        C[i] = B[(a - k + cnt - 1) % (cnt - 1)];
      } else {
        C[i] = -1;
      }
    }
    if (cnt > 1) {
      k = (k * 2) % (cnt - 1);
    }
    l = (l * 2) % (cnt + 1);
  }
  for (int i = 0; i < N; i++) {
    cout << C[i] + 1 << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
typedef struct {
  ll a, b, c;
} T;
using P = pair<int, int>;
ll N, K, H, W;
ll ans;
int A[200005];
ll B[400005];
ll C[200005];
ll M = 1000000007;
string S;
P minminus = P(M, 0);
P maxminus = P(M, 0);
P minplus = P(M, 0);
P maxplus = P(M, 0);
bool found = true;
ll cnt;
ll a;
ll b;
stack<T> s;

int main() {
  cin >> N;
  cin >> S;
  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      A[i] = 1;
    }
    if (A[i] == 1) {
      cnt++;
    }
  }
  T t;
  t.a = 0;
  t.b = 0;
  t.c = 1;
  s.push(t);
  t.a = 1;
  t.b = 1;
  t.c = 1;
  s.push(t);
  ll n = 20;
  while (!s.empty()) {
    T p = s.top();
    s.pop();
    if (p.c >= n) {
      continue;
    }
    if (p.b != 0) {
      B[p.a] = B[p.a % p.b] + 1;
    }
    t.a = p.a + (1 << p.c);
    t.b = p.b + 1;
    t.c = p.c + 1;
    s.push(t);
    t.a = p.a;
    t.b = p.b;
    t.c = p.c + 1;
    s.push(t);
  }
  ll k = 1;
  ll l = 1;
  for (ll i = N - 1; i >= 0; i--) {
    if (cnt > 1) {
      a = (a + k * A[i]) % (cnt - 1);
      k = (k * 2) % (cnt - 1);
    }
    b = (b + l * A[i]) % (cnt + 1);
    l = (l * 2) % (cnt + 1);
  }

  k = 1;
  l = 1;
  for (ll i = N - 1; i >= 0; i--) {
    if (A[i] == 0) {
      C[i] = B[(b + l) % (cnt + 1)];
    } else {
      if (cnt > 1) {
        C[i] = B[(a - k + cnt - 1) % (cnt - 1)];
      } else {
        C[i] = -1;
      }
    }
    if (cnt > 1) {
      k = (k * 2) % (cnt - 1);
    }
    l = (l * 2) % (cnt + 1);
  }
  for (int i = 0; i < N; i++) {
    cout << C[i] + 1 << endl;
  }
}