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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
#define st first
#define sd second
#define mkp make_pair
#define pb push_back
void wenjian() {
  freopen("concatenation.in", "r", stdin);
  freopen("concatenation.out", "w", stdout);
}
void tempwj() {
  freopen("hash.in", "r", stdin);
  freopen("hash.out", "w", stdout);
}
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll qpow(ll a, ll b, ll mod) {
  a %= mod;
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
struct cmp {
  bool operator()(const pii &a, const pii &b) { return a.second > b.second; }
};
int lb(int x) { return x & -x; }

const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll mod = 2147483648;
const int maxn = 4e5 + 5;
vector<int> vv;
int s[maxn];
ll t[maxn];
char a[maxn];
int main() {
  for (int i = 1; i <= 2e5; i++) {
    int ans = 0;
    int x = i;
    while (x) {
      int temp = 0;
      int t = x;
      while (t) {
        if (t & 1)
          temp++;
        t >>= 1;
      }
      x %= temp;
      ans++;
    }
    s[i] = ans;
  }
  int n;
  scanf("%d%s", &n, a + 1);
  int s1 = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] == '1')
      s1++;
  }
  t[s1] = 0; // intf("11111111\\n");
  for (int i = 1; i <= n; i++) {
    //        printf("%d\\n",i);
    for (int k = max(0, s1 - 1); k <= s1 + 1; k++) {
      // printf("%d\\n",k);
      if (k > 0) {
        t[k] = (t[k] * 2 % k + (a[i] - '0')) % k;
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    int k = n - i;
    int y;
    if (a[i] == '1') {
      y = s1 - 1;
      if (y == 0) {
        printf("0\\n");
        continue;
      }
      ll ss = qpow(2, k, y);
      printf("%d\\n", s[((t[y] - ss) % y + y) % y] + 1);
    } else {
      y = s1 + 1;
      ll ss = qpow(2, k, y);
      printf("%d\\n", s[(t[y] + ss) % y] + 1);
    }
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
#define st first
#define sd second
#define mkp make_pair
#define pb push_back
void wenjian() {
  freopen("concatenation.in", "r", stdin);
  freopen("concatenation.out", "w", stdout);
}
void tempwj() {
  freopen("hash.in", "r", stdin);
  freopen("hash.out", "w", stdout);
}
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll qpow(ll a, ll b, ll mod) {
  a %= mod;
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
struct cmp {
  bool operator()(const pii &a, const pii &b) { return a.second > b.second; }
};
int lb(int x) { return x & -x; }

const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll mod = 2147483648;
const int maxn = 4e5 + 5;
vector<int> vv;
int s[maxn];
ll t[maxn];
char a[maxn];
int main() {
  for (int i = 1; i <= 2e5; i++) {
    int ans = 0;
    int x = i;
    while (x) {
      int temp = 0;
      int t = x;
      while (t) {
        if (t & 1)
          temp++;
        t >>= 1;
      }
      x %= temp;
      ans++;
    }
    s[i] = ans;
  }
  int n;
  scanf("%d%s", &n, a + 1);
  int s1 = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] == '1')
      s1++;
  }
  t[s1] = 0; // intf("11111111\\n");
  for (int i = 1; i <= n; i++) {
    //        printf("%d\\n",i);
    for (int k = max(0, s1 - 1); k <= s1 + 1; k++) {
      // printf("%d\\n",k);
      if (k > 0) {
        t[k] = (t[k] * 2 % k + (a[i] - '0')) % k;
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    int k = n - i;
    int y;
    if (a[i] == '1') {
      y = s1 - 1;
      ll ss = qpow(2, k, y);
      printf("%d\\n", s[((t[y] - ss) % y + y) % y] + 1);
    } else {
      y = s1 + 1;
      ll ss = qpow(2, k, y);
      printf("%d\\n", s[(t[y] + ss) % y] + 1);
    }
  }
}