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

#include <stdio.h>

#include <algorithm>
#include <bitset>
#include <iostream>
#include <string>
#include <vector>

int64_t solve(int64_t t) {
  int64_t x = t;
  int64_t c = 0;

  while (x != 0) {
    x = x % std::bitset<64>(x).count();
    ++c;
  }
  return c + 1;
}

int main() {
  int64_t ta = solve(0b10110111001011011001110);
  int64_t tb = solve(0b01110111001011011001110);
  int64_t tc = solve(0b00010111001011011001110);
  int64_t td = solve(0b00100111001011011001110);
  int64_t te = solve(0b00111111001011011001110);

  int64_t N;
  std::cin >> N;
  std::vector<char> V(N);
  int64_t s = 0;
  for (int64_t n = 0; n < N; ++n) {
    char c;
    std::cin >> c;
    if (c == '1') {
      ++s;
      V[n] = 1;
    } else {
      V[n] = 0;
    }
  }

  std::reverse(V.begin(), V.end());

  std::vector<int64_t> V2_1(N);
  int64_t s1 = 0;
  if (s != N) {
    int64_t c = 1;
    V2_1[0] = c;
    for (int64_t n = 1; n < N; ++n) {
      c = (c * 2) % (s + 1);
      V2_1[n] = c;
    }

    for (int64_t n = 0; n < N; ++n) {
      if (V[n] == 1) {
        s1 += V2_1[n];
      }
    }
  }

  std::vector<int64_t> V2_2(N);
  int64_t s2 = 0;
  if (s != 1 && s != 0) {
    {
      int64_t c = 1;
      V2_2[0] = c;
      for (int64_t n = 1; n < N; ++n) {
        c = (c * 2) % (s - 1);
        V2_2[n] = c;
      }

      for (int64_t n = 0; n < N; ++n) {
        if (V[n] == 1) {
          s2 += V2_2[n];
        }
      }
    }
  }

  for (int64_t n = N - 1; n >= 0; --n) {
    if (V[n] == 0) {
      std::cout << solve((s1 + s + 1 + V2_1[n]) % (s + 1)) << std::endl;
    } else {
      std::cout << ((s == 1) ? 0 : (solve(((s2 + s - 1 - V2_2[n]) % (s - 1)))))
                << std::endl;
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <stdio.h>

#include <algorithm>
#include <bitset>
#include <iostream>
#include <string>
#include <vector>

int64_t solve(int64_t t) {
  int64_t x = t;
  int64_t c = 0;

  while (x != 0) {
    x = x % std::bitset<64>(x).count();
    ++c;
  }
  return c + 1;
}

int main() {
  int64_t ta = solve(0b10110111001011011001110);
  int64_t tb = solve(0b01110111001011011001110);
  int64_t tc = solve(0b00010111001011011001110);
  int64_t td = solve(0b00100111001011011001110);
  int64_t te = solve(0b00111111001011011001110);

  int64_t N;
  std::cin >> N;
  std::vector<char> V(N);
  int64_t s = 0;
  for (int64_t n = 0; n < N; ++n) {
    char c;
    std::cin >> c;
    if (c == '1') {
      ++s;
      V[n] = 1;
    } else {
      V[n] = 0;
    }
  }
  std::reverse(V.begin(), V.end());

  std::vector<int64_t> V2_1(N);
  int64_t s1 = 0;
  {
    int64_t c = 1;
    V2_1[0] = c;
    for (int64_t n = 1; n < N; ++n) {
      c = (c * 2) % (s + 1);
      V2_1[n] = c;
    }

    for (int64_t n = 0; n < N; ++n) {
      if (V[n] == 1) {
        s1 += V2_1[n];
      }
    }
  }

  std::vector<int64_t> V2_2(N);
  int64_t s2 = 0;
  {
    int64_t c = 1;
    V2_2[0] = c;
    for (int64_t n = 1; n < N; ++n) {
      c = (c * 2) % (s - 1);
      V2_2[n] = c;
    }

    for (int64_t n = 0; n < N; ++n) {
      if (V[n] == 1) {
        s2 += V2_2[n];
      }
    }
  }

  for (int64_t n = N - 1; n >= 0; --n) {
    if (V[n] == 0) {
      std::cout << solve((s1 + s + 1 + V2_1[n]) % (s + 1)) << std::endl;
    } else {
      std::cout << solve((s2 + s - 1 - V2_2[n]) % (s - 1)) << std::endl;
    }
  }

  return 0;
}