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

#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define clr clear()
#define ins insert
#define tol(s) transform(s.begin(), s.end(), s.begin(), ::tolower);
#define tou(s) transform(s.begin(), s.end(), s.begin(), ::toupper);
#define rep(i, n) for (__typeof(n) i = 0; i < (n); i++)
#define mem(x, val) memset((x), (val), sizeof(x));
#define cusmem(x, val)                                                         \\
  for (auto &i : x)                                                            \\
    i = val;
#define all(x) x.begin(), x.end()
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef vector<pll> vpll;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<pii> vpii;
string imp = "Impossible\\n";

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
clock_t startTime;
double getCurrentTime() {
  return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
// const int N=11;
const int N = 5e6 + 5;
ll MOD = 1e9 + 7;
ll n, m, k, i, j;
bool f0, f1, f2;
const int MAXN = 2e3;
int powmodp(ll a, ll b, ll mod) { // log a
  a = a % mod;
  if (b == 1)
    return a;
  ll res = 1;
  while (b) {
    if (b & 1) {
      res = res * a;
      res %= mod;
    }
    b = b >> 1;
    a = a * a;
    a %= mod;
  }
  return res;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastio cout << fixed << setprecision(11);
  int tests = 1;
  // cin>>tests;
  while (tests--) {
    int N = 2e5 + 5;
    int dp[N];
    mem(dp, 0);
    dp[1] = 1;
    for (int i = 2; i < N; i++) {
      dp[i] = 1 + dp[(i % __builtin_popcount(i))];
    }
    int n;
    cin >> n;
    string x;
    cin >> x;
    reverse(all(x));
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      if (x[i] == '1')
        cnt++;
    }
    if (cnt == 0) {
      for (int i = 0; i < n; i++)
        cout << "1\\n";
      continue;
    }
    int res1 = 0, res2 = 0;
    for (int i = 0; i < n; i++) {
      if (x[i] == '1' && cnt != 1) {
        res1 += powmodp(2, i, cnt - 1);
        res1 %= (cnt - 1);
      }
    }
    for (int i = 0; i < n; i++) {
      if (x[i] == '1') {
        res2 += powmodp(2, i, cnt + 1);
        res2 %= (cnt + 1);
      }
    }
    vector<int> ans(n, 1);
    for (int i = 0; i < n; i++) {
      if (x[i] == '1') {
        if (cnt == 1)
          ans[i] = 0;
        else
          ans[i] += dp[(res1 - powmodp(2, i, cnt - 1) + cnt - 1) % (cnt - 1)];
      } else {
        ans[i] += dp[(res2 + powmodp(2, i, cnt + 1)) % (cnt + 1)];
      }
    }
    for (int i = n - 1; i >= 0; i--)
      cout << ans[i] << "\\n";
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define clr clear()
#define ins insert
#define tol(s) transform(s.begin(), s.end(), s.begin(), ::tolower);
#define tou(s) transform(s.begin(), s.end(), s.begin(), ::toupper);
#define rep(i, n) for (__typeof(n) i = 0; i < (n); i++)
#define mem(x, val) memset((x), (val), sizeof(x));
#define cusmem(x, val)                                                         \\
  for (auto &i : x)                                                            \\
    i = val;
#define all(x) x.begin(), x.end()
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef vector<pll> vpll;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<pii> vpii;
string imp = "Impossible\\n";

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
clock_t startTime;
double getCurrentTime() {
  return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
// const int N=11;
const int N = 5e6 + 5;
ll MOD = 1e9 + 7;
ll n, m, k, i, j;
bool f0, f1, f2;
const int MAXN = 2e3;
int powmodp(ll a, ll b, ll mod) { // log a
  a = a % mod;
  if (b == 1)
    return a;
  ll res = 1;
  while (b) {
    if (b & 1) {
      res = res * a;
      res %= mod;
    }
    b = b >> 1;
    a = a * a;
    a %= mod;
  }
  return res;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastio cout << fixed << setprecision(11);
  int tests = 1;
  // cin>>tests;
  while (tests--) {
    int N = 2e5 + 5;
    int dp[N];
    mem(dp, 0);
    dp[1] = 1;
    for (int i = 2; i < N; i++) {
      assert(i % __builtin_popcount(i) < i);
      dp[i] = 1 + dp[(i % __builtin_popcount(i))];
    }
    int n;
    cin >> n;
    string x;
    cin >> x;
    reverse(all(x));
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      if (x[i] == '1')
        cnt++;
    }
    int res1 = 0, res2 = 0;
    for (int i = 0; i < n; i++) {
      if (x[i] == '1') {
        res1 += powmodp(2, i, cnt - 1);
        res1 %= (cnt - 1);
      }
    }
    for (int i = 0; i < n; i++) {
      if (x[i] == '1') {
        res2 += powmodp(2, i, cnt + 1);
        res2 %= (cnt + 1);
      }
    }
    vector<int> ans(n, 1);
    for (int i = 0; i < n; i++) {
      if (x[i] == '1') {
        ans[i] += dp[(res1 - powmodp(2, i, cnt - 1) + cnt - 1) % (cnt - 1)];
      } else {
        ans[i] += dp[(res2 + powmodp(2, i, cnt + 1)) % (cnt + 1)];
      }
    }
    for (int i = n - 1; i >= 0; i--)
      cout << ans[i] << "\\n";
  }
  return 0;
}