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

// #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() { printf("yes\\n"); }
void no() { printf("no\\n"); }
void Yes() { printf("Yes\\n"); }
void No() { printf("No\\n"); }
void YES() { printf("YES\\n"); }
void NO() { printf("NO\\n"); }

ll Pow(ll A, ll N, ll mod) {
  if (N == 0)
    return 1;
  if (N % 2 == 0)
    return Pow(A * A % mod, N / 2, mod);
  else
    return Pow(A * A % mod, N / 2, mod) * A % mod;
}

ll DP[int(2e5 + 5)];
ll Solve(int num) {
  if (num == 0)
    return DP[0] = 0;
  if (DP[num] > 0)
    return DP[num];

  int cnt = 0, now = num;
  while (now > 0) {
    if (now % 2 != 0)
      cnt++;
    now /= 2;
  }
  return DP[num] = Solve(num % cnt) + 1;
}

ll ans1, ans2;

int main() {
  int N;
  string S;
  cin >> N >> S;

  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == '1')
      cnt++;
  }

  ll now = 1;
  for (int i = N - 1; i >= 0; i--) {
    if (S[i] == '1') {
      ans1 += now;
      ans1 %= cnt + 1;
    }
    now *= 2;
    now %= cnt + 1;
  }

  if (cnt > 1) {
    now = 1;
    for (int i = N - 1; i >= 0; i--) {
      if (S[i] == '1') {
        ans2 += now;
        ans2 %= cnt - 1;
      }
      now *= 2;
      now %= cnt - 1;
    }
  }

  for (int i = 0; i < N; i++) {
    if (S[i] == '0') {
      now = ans1;
      now += Pow(2, N - 1 - i, cnt + 1);
      now %= cnt + 1;
    } else {
      now = ans2;
      if (cnt == 1) {
        cout << 0 << endl;
        continue;
      }
      now -= Pow(2, N - 1 - i, cnt - 1);
      now %= cnt - 1;
      if (now < 0)
        now += cnt - 1;
    }

    cout << Solve(now) + 1 << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

// #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() { printf("yes\\n"); }
void no() { printf("no\\n"); }
void Yes() { printf("Yes\\n"); }
void No() { printf("No\\n"); }
void YES() { printf("YES\\n"); }
void NO() { printf("NO\\n"); }

ll Pow(ll A, ll N, ll mod) {
  if (N == 0)
    return 1;
  if (N % 2 == 0)
    return Pow(A * A % mod, N / 2, mod);
  else
    return Pow(A * A % mod, N / 2, mod) * A % mod;
}

int DP[int(2e5 + 5)];

int Solve(int num) {
  if (num == 0)
    return DP[0] = 0;
  if (DP[num] > 0)
    return DP[num];

  int cnt = 0, now = num;
  while (now > 0) {
    if (now % 2 != 0)
      cnt++;
    now /= 2;
  }
  return DP[num] = Solve(num % cnt) + 1;
}

ll ans1, ans2;

int main() {
  int N;
  string S;
  cin >> N >> S;

  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == '1')
      cnt++;
  }

  ll now = 1;
  for (int i = N - 1; i >= 0; i--) {
    if (S[i] == '1') {
      ans1 += now;
      ans1 %= cnt + 1;
    }
    now *= 2;
    now %= cnt + 1;
  }

  if (cnt > 0) {
    now = 1;
    for (int i = N - 1; i >= 0; i--) {
      if (S[i] == '1') {
        ans2 += now;
        ans2 %= cnt - 1;
      }
      now *= 2;
      now %= cnt - 1;
    }
  }

  for (int i = 0; i < N; i++) {
    if (S[i] == '0') {
      now = ans1;
      now += Pow(2, N - 1 - i, cnt + 1);
      now %= cnt + 1;
      cout << Solve(now) + 1 << endl;
    } else {
      now = ans2;
      now -= Pow(2, N - 1 - i, cnt - 1);
      now %= cnt - 1;
      if (now < 0)
        now += cnt - 1;
      cout << Solve(now) + 1 << endl;
    }
  }

  return 0;
}