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

#include <bits/stdc++.h>
#define inf 0x7fffffff
#define eps 1e-12
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const double pi = acos(-1);
inline ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int n, m, l, r, d;
int a[300005], sum, b[300005];
char ch[300005];
int ans[300005];
int getans(int x) {
  if (x == 0 || ans[x])
    return ans[x];
  int cnt = 0;
  for (int i = 0; (1 << i) <= x; i++) {
    if (x & (1 << i))
      cnt++;
  }
  return getans(x % cnt) + 1;
}
int Mod[30];
int bit[505];
int len;
int check(int x) {
  int i;
  for (i = x; i <= x + len - 1; i++) {
    if (b[i] > bit[i - x + 1])
      return 1;
    if (b[i] < bit[i - x + 1])
      return -1;
  }
  return 0;
}
int getMod(int x) {
  len = 0;
  while (x) {
    bit[++len] = x % 2;
    x >>= 1;
  }
  int i, j;
  for (i = 1; i <= len / 2; i++)
    swap(bit[i], bit[len - i + 1]);
  for (i = 1; i <= n - len + 1; i++) {
    while (check(i) >= 0) {
      for (j = i + len - 1; j >= i; j--) {
        b[j] -= bit[j - i + 1];
        if (b[j] < 0)
          b[j] += 2, b[j - 1]--;
      }
    }
    if (i == n - len + 1)
      break;
    if (b[i])
      b[i] = 0, b[i + 1] += 2;
  }
  int ans = 0;
  for (i = n - len + 1; i <= n; i++)
    ans = ans * 2 + b[i];
  return ans;
}
ll qpow(ll a, ll b, ll mo) {
  ll ans = 1, base = a;
  while (b) {
    if (b & 1)
      ans = ans * base % mo;
    base = base * base % mo;
    b >>= 1;
  }
  return ans;
}
int main() {
  int i, j, k;
  n = read();
  for (i = 1; i <= n; i++)
    ans[i] = getans(i);
  scanf("%s", ch + 1);
  for (i = 1; i <= n; i++)
    b[i] = a[i] = ch[i] - '0', sum += a[i];
  if (sum == 0) {
    for (i = 1; i <= n; i++)
      puts("1");
    return 0;
  }
  sum--;
  if (sum)
    Mod[0] = getMod(sum);
  sum += 2;
  for (i = 1; i <= n; i++)
    b[i] = a[i];
  Mod[2] = getMod(sum);
  sum--;
  int now;
  for (i = 1; i <= n; i++) {
    if (a[i] == 1 && sum == 1) {
      puts("0");
      continue;
    }
    if (a[i] == 1)
      now = ((Mod[0] - qpow(2, n - i, sum - 1)) % (sum - 1) + sum - 1) %
            (sum - 1);
    else
      now = (Mod[2] + qpow(2, n - i, sum + 1)) % (sum + 1);
    printf("%d\\n", ans[now] + 1);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define inf 0x7fffffff
#define eps 1e-12
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const double pi = acos(-1);
inline ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int n, m, l, r, d;
int a[300005], sum, b[300005];
char ch[300005];
int ans[300005];
int getans(int x) {
  if (x == 0 || ans[x])
    return ans[x];
  int cnt = 0;
  for (int i = 0; (1 << i) <= x; i++) {
    if (x & (1 << i))
      cnt++;
  }
  return getans(x % cnt) + 1;
}
int Mod[30];
int bit[505];
int len;
int check(int x) {
  int i;
  for (i = x; i <= x + len - 1; i++) {
    if (b[i] > bit[i - x + 1])
      return 1;
    if (b[i] < bit[i - x + 1])
      return -1;
  }
  return 0;
}
int getMod(int x) {
  len = 0;
  while (x) {
    bit[++len] = x % 2;
    x >>= 1;
  }
  int i, j;
  for (i = 1; i <= len / 2; i++)
    swap(bit[i], bit[len - i + 1]);
  for (i = 1; i <= n - len + 1; i++) {
    while (check(i) >= 0) {
      for (j = i + len - 1; j >= i; j--) {
        b[j] -= bit[j - i + 1];
        if (b[j] < 0)
          b[j] += 2, b[j - 1]--;
      }
    }
    if (i == n - len + 1)
      break;
    if (b[i])
      b[i] = 0, b[i + 1] += 2;
  }
  int ans = 0;
  for (i = n - len + 1; i <= n; i++)
    ans = ans * 2 + b[i];
  return ans;
}
ll qpow(ll a, ll b, ll mo) {
  ll ans = 1, base = a;
  while (b) {
    if (b & 1)
      ans = ans * base % mo;
    base = base * base % mo;
    b >>= 1;
  }
  return ans;
}
int main() {
  int i, j, k;
  n = read();
  for (i = 1; i <= n; i++)
    ans[i] = getans(i);
  scanf("%s", ch + 1);
  for (i = 1; i <= n; i++)
    b[i] = a[i] = ch[i] - '0', sum += a[i];
  sum--;
  Mod[0] = getMod(sum);
  sum += 2;
  for (i = 1; i <= n; i++)
    b[i] = a[i];
  Mod[2] = getMod(sum);
  sum--;
  int now;
  /*for(i=1;i<=n;i++){
          if(a[i]==1&&sum==1){puts("0");continue;}
          if(a[i]==1) now=((Mod[0]-qpow(2,n-i,sum-1))%(sum-1)+sum-1)%(sum-1);
          else now=(Mod[2]+qpow(2,n-i,sum+1))%(sum+1);
          printf("%d\\n",ans[now]+1);
  }*/
  return 0;
}