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

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define DIV 1000000007
#define TE 2e5
using namespace std;
using ll = long long;
using ldb = long double;

ll func(ll n, vector<ll> &p) {
  if (n == 0)
    return 0;
  if (p[n] != 0)
    return p[n];
  ll temp = n, ones = 0;
  while (temp) {
    if (temp % 2 == 1)
      ones++;
    temp /= 2;
  }
  p[n] = func(n % ones, p) + 1;
  return p[n];
}

int main() {
  int N;
  cin >> N;
  string x;
  cin >> x;
  ll ones = 0;
  for (int i = N - 1; i >= 0; i--) {
    if (x[i] == '1')
      ones++;
  }

  if (ones == 0)
    for (int i = 0; i < N; i++)
      cout << 1 << endl;

  else if (ones == 1) {
    for (int i = 0; i < N; i++) {
      if (x[i] == '1')
        cout << 0 << endl;
      else {
        if (i != N - 1)
          cout << 1 << endl;
        else
          cout << 2 << endl;
      }
    }
  }

  else {
    vector<ll> bi0(N, 1), bi1(N, 1);
    ll m0 = 0, m1 = 0;
    for (int i = N - 1; i >= 0; i--) {
      bi0[i] = ((i == N - 1 ? 1 : bi0[i + 1] * 2)) % (ones + 1);
      bi1[i] = ((i == N - 1 ? 1 : bi1[i + 1] * 2)) % (ones - 1);
      if (x[i] == '1') {
        m0 = (m0 + bi0[i]) % (ones + 1);
        m1 = (m1 + bi1[i]) % (ones - 1);
      }
      // cout << "i= " << i << " bi0=" << bi0[i] << " bi1=" << bi1[i] << endl;
    }
    // cout << "ones+1="<<ones + 1 << " ones-1=" << ones - 1 << endl;
    // cout << m0 << " " << m1 << endl;
    vector<ll> p(TE);
    for (int i = 0; i < N; i++) {
      ll n;
      if (x[i] == '0') {
        ll temp = m0 + bi0[i];
        n = temp % (ones + 1);
        // cout << temp;
      } else {
        ll temp = m1 - bi1[i];
        n = (temp + (ones - 1)) % (ones - 1);
        // cout << m1 + bi1[i] + (ones - 1);
      }
      // cout << " n=" << n << " ";
      cout << func(n, p) + 1 << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define DIV 1000000007
#define TE 2e5
using namespace std;
using ll = long long;
using ldb = long double;

ll func(ll n, vector<ll> &p) {
  if (n == 0)
    return 0;
  if (p[n] != 0)
    return p[n];
  ll temp = n, ones = 0;
  while (temp) {
    if (temp % 2 == 1)
      ones++;
    temp /= 2;
  }
  p[n] = func(n % ones, p) + 1;
  return p[n];
}

int main() {
  int N;
  cin >> N;
  string x;
  cin >> x;
  ll ones = 0;
  for (int i = N - 1; i >= 0; i--) {
    if (x[i] == '1')
      ones++;
  }

  vector<ll> bi0(TE + 1, 1), bi1(TE + 1, 1);
  ll m0 = 0, m1 = 0, two0 = 1, two1 = 1;
  for (int i = N - 1; i >= 0; i--) {
    if (x[i] == '1') {
      m0 = (m0 + two0) % (ones + 1);
      m1 = (m1 + two1) % (ones - 1);
    }
    two0 = (two0 * 2) % (ones + 1);
    two1 = (two1 * 2) % (ones - 1);
    bi0[i] = two0, bi1[i] = two1;
  }

  vector<ll> ans(N);
  vector<ll> p(TE + 1);
  for (int i = 0; i < N; i++) {
    ll n;
    if (x[i] == '0')
      n = (two0 + bi0[i] + (ones + 1)) % (ones + 1);
    else
      n = (two1 - bi1[i] + (ones - 1)) % (ones - 1);

    // cout << "n=" << n << endl;
    ans[i] = func(n, p) + 1;
  }

  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
}