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

// prepared by dumb
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define dl double
#define llu unsigned long long int
#define scll(x) scanf("%lld", &x)
#define scld(x) scanf("%lf", &x)
#define scull(x) scanf("%llu", &x)
#define scch(x) scanf(" %c", &x)
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define ppb() pop_back()
#define ppf() pop_front()
#define Pi acos(-1.0)
#define inf 1000000000000000000
#define mod 1000000007
#define ff first
#define ss second
#define mpr make_pair
#define debug(x) cout << x << '\\n';
// Creates a min heap
// priority_queue <int, vector<int>, greater<int> > pq;
// sin((x*pi)/180.0)[x in degree]
// sin(x)[x in radian]
// asin(x)[radian]
//(asin(x)*180)/pi[degree]
// or(|)//and(&)//xor(^)//not(~)
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

ll bigmod(ll a, ll b, ll cnt) {
  ll x;
  if (b == 0)
    return 1LL;
  else {
    x = bigmod(a, b / 2LL, cnt);
    x = (x % cnt * x % cnt) % cnt;
    if (b % 2LL == 1LL)
      return (x % cnt * a % cnt) % cnt;
    else
      return x % cnt;
  }
}
ll dp[2000010];
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll a, b, c = 0, d, e, f = 0, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v,
           w, x, y, z;
  ll cnt = 0, ans = 0, l1, l2, l3, c1 = 0, c2 = 0, c3 = 0, mx = 0, mn = 0;
  ll prevsum = 0, ans1 = 0, ans2 = 0, odd = 0, even = 0, hh, mm;
  string str, str1, str2;
  scll(n);
  cin >> str;
  for (i = 1; i <= 200005; i++) {
    l1 = __builtin_popcountll(i);
    dp[i] = 1 + dp[i % l1];
  }
  for (i = n - 1; i >= 0; i--) {
    if (str[i] == '1')
      cnt++;
  }
  if (cnt == 0) {
    for (i = 0; i < n; i++)
      cout << 1 << '\\n';
  } else {
    if (cnt != 1) {
      for (i = n - 1; i >= 0; i--) {
        ans1 =
            (ans1 % (cnt - 1) +
             ((str[i] - '0') * bigmod(2LL, n - i - 1, cnt - 1)) % (cnt - 1)) %
            (cnt - 1);
      }
    }
    for (i = n - 1; i >= 0; i--) {
      ans2 = (ans2 % (cnt + 1) +
              ((str[i] - '0') * bigmod(2LL, n - i - 1, cnt + 1)) % (cnt + 1)) %
             (cnt + 1);
    }

    for (i = 0; i < n; i++) {
      if (str[i] == '1') {
        if (cnt == 1) {
          cout << 0 << '\\n';
        } else {
          cout << 1 + dp[((ans1 % (cnt - 1) -
                           bigmod(2LL, n - i - 1, cnt - 1) % (cnt - 1)) +
                          (cnt - 1)) %
                         (cnt - 1)]
               << '\\n';
        }
      } else {

        cout << 1 + dp[(ans2 % (cnt + 1) +
                        bigmod(2LL, n - i - 1, cnt + 1) % (cnt + 1)) %
                       (cnt + 1)]
             << '\\n';
      }
    }
  }
  // cout<<__builtin_popcountll(200005);

  return 0;
}

Error: WA

Fixed submission:

// prepared by dumb
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define dl double
#define llu unsigned long long int
#define scll(x) scanf("%lld", &x)
#define scld(x) scanf("%lf", &x)
#define scull(x) scanf("%llu", &x)
#define scch(x) scanf(" %c", &x)
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define ppb() pop_back()
#define ppf() pop_front()
#define Pi acos(-1.0)
#define inf 1000000000000000000
#define mod 1000000007
#define ff first
#define ss second
#define mpr make_pair
#define debug(x) cout << x << '\\n';
// Creates a min heap
// priority_queue <int, vector<int>, greater<int> > pq;
// sin((x*pi)/180.0)[x in degree]
// sin(x)[x in radian]
// asin(x)[radian]
//(asin(x)*180)/pi[degree]
// or(|)//and(&)//xor(^)//not(~)
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

ll bigmod(ll a, ll b, ll cnt) {
  ll x;
  if (b == 0)
    return 1LL;
  else {
    x = bigmod(a, b / 2LL, cnt);
    x = (x % cnt * x % cnt) % cnt;
    if (b % 2LL == 1LL)
      return (x % cnt * a % cnt) % cnt;
    else
      return x % cnt;
  }
}
ll dp[2000010];
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll a, b, c = 0, d, e, f = 0, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v,
           w, x, y, z;
  ll cnt = 0, ans = 0, l1, l2, l3, c1 = 0, c2 = 0, c3 = 0, mx = 0, mn = 0;
  ll prevsum = 0, ans1 = 0, ans2 = 0, odd = 0, even = 0, hh, mm;
  string str, str1, str2;
  scll(n);
  cin >> str;
  for (i = 1; i <= 200005; i++) {
    l1 = __builtin_popcountll(i);
    dp[i] = 1 + dp[i % l1];
  }
  for (i = n - 1; i >= 0; i--) {
    if (str[i] == '1')
      cnt++;
  }
  if (cnt == 0) {
    for (i = 0; i < n; i++)
      cout << 1 << '\\n';
  } else {
    for (i = n - 1; i >= 0; i--) {
      ans1 = (ans1 % (cnt - 1) +
              ((str[i] - '0') * bigmod(2LL, n - i - 1, cnt - 1)) % (cnt - 1)) %
             (cnt - 1);
    }
    for (i = n - 1; i >= 0; i--) {
      ans2 = (ans2 % (cnt + 1) +
              ((str[i] - '0') * bigmod(2LL, n - i - 1, cnt + 1)) % (cnt + 1)) %
             (cnt + 1);
    }

    for (i = 0; i < n; i++) {
      if (str[i] == '1') {
        if (cnt == 1) {
          cout << 0 << '\\n';
        } else {
          cout << 1 + dp[((ans1 % (cnt - 1) -
                           bigmod(2LL, n - i - 1, cnt - 1) % (cnt - 1)) +
                          (cnt - 1)) %
                         (cnt - 1)]
               << '\\n';
        }
      } else {
        cout << 1 + dp[(ans2 % (cnt + 1) +
                        bigmod(2LL, n - i - 1, cnt + 1) % (cnt + 1)) %
                       (cnt + 1)]
             << '\\n';
      }
    }
  }
  // cout<<__builtin_popcountll(200005);

  return 0;
}