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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

long long N, num = 0, xx, uu, dd;
char str[400000];

inline long long calc(long long x) {
  long long b = x, rr = 0;
  while (b > 0) {
    if (b & 1)
      ++rr;
    b >>= 1;
  }
  return x % rr;
}

inline void init() {
  long long base1 = 1, base2 = 1, now1 = 0, now2 = 0;
  for (int i = N; i >= 1; --i, base1 = base1 * 2ll, base2 = base2 * 2ll) {
    if (str[i] == '0')
      continue;
    if (num - 1 != 0) {
      now1 += base1 % (num - 1);
      now1 %= (num - 1);
    }
    now2 += base2 % (num + 1);
    now2 %= (num + 1);

    if (num - 1 != 0)
      base1 = base1 % (num - 1);
    base2 = base2 % (num + 1);
  }

  uu = now2;
  dd = now1;
}

inline long long fastpow(long long bass, long long b, long long mod) {
  long long ret = 1, base = bass;
  while (b > 0) {
    if (b & 1)
      ret = ret * base % mod;
    base = base * base % mod;
    b >>= 1;
  }
  return ret % mod;
}

int main() {
  // freopen("test.in","r",stdin);
  // freopen("test.out","w",stdout);
  scanf("%lld", &N);
  scanf("%s", str + 1);
  for (int i = 1; i <= N; ++i)
    if (str[i] == '1')
      ++num;
  xx = num;
  init();
  for (int i = 1; i <= N; ++i) {
    long long now = 0, ans = 0;
    if (str[i] == '0') {
      now = uu;
      now = (now + fastpow(2, N - i, num + 1)) % (num + 1);
      ++ans;
      while (now) {
        ++ans;
        now = calc(now);
      }
      printf("%lld\\n", ans);
    } else {
      if (num == 1)
        printf("0\\n");
      else {
        now = dd;
        now = (now - fastpow(2, N - i, num - 1) + num - 1) % (num - 1);
        ++ans;
        while (now) {
          ++ans;
          now = calc(now);
        }
        printf("%lld\\n", ans);
      }
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

long long N, num = 0, xx, uu, dd;
char str[400000];

inline long long calc(long long x) {
  long long b = x, rr = 0;
  while (b > 0) {
    if (b & 1)
      ++rr;
    b >>= 1;
  }
  return x % rr;
}

inline void init() {
  long long base1 = 1, base2 = 1, now1 = 0, now2 = 0;
  for (int i = N; i >= 1; --i, base1 = base1 * 2ll, base2 = base2 * 2ll) {
    if (str[i] == '0')
      continue;
    if (num - 1 != 0) {
      now1 += base1 % (num - 1);
      now1 %= (num - 1);
    }
    now2 += base2 % (num + 1);
    now2 %= (num + 1);

    base1 = base1 % (num - 1);
    base2 = base2 % (num + 1);
  }

  uu = now2;
  dd = now1;
}

inline long long fastpow(long long bass, long long b, long long mod) {
  long long ret = 1, base = bass;
  while (b > 0) {
    if (b & 1)
      ret = ret * base % mod;
    base = base * base % mod;
    b >>= 1;
  }
  return ret % mod;
}

int main() {
  // freopen("test.in","r",stdin);
  // freopen("test.out","w",stdout);
  scanf("%lld", &N);
  scanf("%s", str + 1);
  for (int i = 1; i <= N; ++i)
    if (str[i] == '1')
      ++num;
  xx = num;
  init();
  for (int i = 1; i <= N; ++i) {
    long long now = 0, ans = 0;
    if (str[i] == '0') {
      now = uu;
      now = (now + fastpow(2, N - i, num + 1)) % (num + 1);
      ++ans;
      while (now) {
        ++ans;
        now = calc(now);
      }
      printf("%lld\\n", ans);
    } else {
      if (num == 1)
        printf("0\\n");
      else {
        now = dd;
        now = (now - fastpow(2, N - i, num - 1) + num - 1) % (num - 1);
        ++ans;
        while (now) {
          ++ans;
          now = calc(now);
        }
        printf("%lld\\n", ans);
      }
    }
  }

  return 0;
}