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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
long long mo = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <class A> void PR(A a, ll n) {
  rep(i, n) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  cout << "\\n";
}
ld PI = 3.14159265358979323846;

string S;
ll N;
vector<ll> dp(200100, -1);
ll dfs(ll n) {
  ll cnt = 0;
  if (dp[n] != -1)
    return 0;
  rep(i, 30) {
    if ((n >> i) & 1LL)
      cnt++;
  }
  ll m = n % cnt;
  if (m == 0) {
    dp[n] = 1;
    return 0;
  } else {
    dfs(m);
    dp[n] = dp[m] + 1;
    return 0;
  }
}

int main() {
  cin >> N;
  cin >> S;
  vector<ll> memo(N), memom(N), memop(N);
  ll cnt = 0;
  rep(i, N) {
    if (S[i] == '1')
      cnt++;
  }
  if (cnt == 0) {
    rep(i, N) { cout << 1 << endl; }
    return 0;
  } else if (cnt == 1) {
    if (S[N - 1] == '1') {
      rep(i, N - 1) { cout << 2 << endl; }
      cout << 0 << endl;
    } else {
      rep(i, N - 1) {
        if (S[i] == '0') {
          cout << 1 << endl;
        } else {
          cout << 0 << endl;
        }
      }
      cout << 2 << endl;
    }
    return 0;
  }
  ll t = 1 % cnt;
  memom[0] = 1 % (cnt - 1);
  memo[0] = 1 % cnt; // caution.
  memop[0] = 1 % (cnt + 1);
  rep(i, N - 1) {
    memom[i + 1] = (memom[i] * 2) % (cnt - 1);
    memo[i + 1] = (memo[i] * 2) % cnt;
    memop[i + 1] = (memop[i] * 2) % (cnt + 1);
  }
  reverse(memo.begin(), memo.end());
  reverse(memom.begin(), memom.end());
  reverse(memop.begin(), memop.end());
  dp[0] = 0;
  REP(i, 1, 200010) {
    if (dp[i] != -1)
      continue;
    dfs(i);
  }
  // PR(dp, 15);
  // PR(memo,N);
  ll h = 0, hm = 0, hp = 0;
  rep(i, N) {
    if (S[i] == '1') {
      h += memo[i];
      h %= cnt;
      hm += memom[i];
      hm %= (cnt - 1);
      hp += memop[i];
      hp %= (cnt + 1);
    }
  }
  // cout << h << " a " <<endl;
  rep(i, N) {
    ll tmp;
    if (S[i] == '1') {
      tmp = hm - memom[i];
      tmp += (cnt - 1);
      tmp %= (cnt - 1);
      // cout <<"a "<< tmp <<endl;
      cout << dp[tmp] + 1 << endl;
    } else {
      tmp = hp + memop[i];
      tmp %= (cnt + 1);
      // cout <<"b "<< tmp <<endl;
      cout << dp[tmp] + 1 << endl;
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
long long mo = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <class A> void PR(A a, ll n) {
  rep(i, n) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  cout << "\\n";
}
ld PI = 3.14159265358979323846;

string S;
ll N;
vector<ll> dp(200100, -1);
ll dfs(ll n) {
  ll cnt = 0;
  if (dp[n] != -1)
    return 0;
  rep(i, 30) {
    if ((n >> i) & 1LL)
      cnt++;
  }
  ll m = n % cnt;
  if (m == 0) {
    dp[n] = 1;
    return 0;
  } else {
    dfs(m);
    dp[n] = dp[m] + 1;
    return 0;
  }
}

int main() {
  cin >> N;
  cin >> S;
  vector<ll> memo(N), memom(N), memop(N);
  ll cnt = 0;
  rep(i, N) {
    if (S[i] == '1')
      cnt++;
  }
  if (cnt == 0) {
    rep(i, N) { cout << 1 << endl; }
  } else if (cnt == 1) {
    if (S[N - 1] == '1') {
      rep(i, N - 1) { cout << 2 << endl; }
      cout << 0 << endl;
    } else {
      rep(i, N - 1) {
        if (S[i] == '0') {
          cout << 1 << endl;
        } else {
          cout << 0 << endl;
        }
      }
      cout << 2 << endl;
    }
  }
  ll t = 1 % cnt;
  memom[0] = 1 % (cnt - 1);
  memo[0] = 1 % cnt; // caution.
  memop[0] = 1 % (cnt + 1);
  rep(i, N - 1) {
    memom[i + 1] = (memom[i] * 2) % (cnt - 1);
    memo[i + 1] = (memo[i] * 2) % cnt;
    memop[i + 1] = (memop[i] * 2) % (cnt + 1);
  }
  reverse(memo.begin(), memo.end());
  reverse(memom.begin(), memom.end());
  reverse(memop.begin(), memop.end());
  dp[0] = 0;
  REP(i, 1, 200010) {
    if (dp[i] != -1)
      continue;
    dfs(i);
  }
  // PR(dp, 15);
  // PR(memo,N);
  ll h = 0, hm = 0, hp = 0;
  rep(i, N) {
    if (S[i] == '1') {
      h += memo[i];
      h %= cnt;
      hm += memom[i];
      hm %= (cnt - 1);
      hp += memop[i];
      hp %= (cnt + 1);
    }
  }
  // cout << h << " a " <<endl;
  rep(i, N) {
    ll tmp;
    if (S[i] == '1') {
      tmp = hm - memom[i];
      tmp += (cnt - 1);
      tmp %= (cnt - 1);
      // cout <<"a "<< tmp <<endl;
      cout << dp[tmp] + 1 << endl;
    } else {
      tmp = hp + memop[i];
      tmp %= (cnt + 1);
      // cout <<"b "<< tmp <<endl;
      cout << dp[tmp] + 1 << endl;
    }
  }
}