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

// Best practice

#include <bits/stdc++.h>

#define int long long
#define pb push_back
#define pf emplace_front
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pii pair<int, int>
#define psi pair<string, int>
#define vi vector<int>
#define vpii vector<pii>
#define vvi vector<vi>
#define sz(x) (int)(x).size()
#define x first
#define y second
#define endl '\\n'
#define tezz                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define MOD 1000000007
#define hell 998244353
#define prec(n) fixed << setprecision(n)
#define ini(a, i) memset(a, i, sizeof(a))
#define output(x) cout << (x ? "YES" : "NO") << endl;
using namespace std;

#define debug(vec)                                                             \\
  cout << (#vec) << " : [ ";                                                   \\
  for (auto &i : vec)                                                          \\
    cout << i << " ";                                                          \\
  cout << "]" << endl
#define debugp(vec)                                                            \\
  cout << (#vec) << " : [ ";                                                   \\
  for (auto &i : vec)                                                          \\
    cout << "(" << i.x << "," << i.y << ")"                                    \\
         << " ";                                                               \\
  cout << "]" << endl
#define trace(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
    cout << endl;                                                              \\
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cout << "[" << *it << ": " << a << "]\\t";
  err(++it, args...);
}

// const int dx[4] = { -1, 1, 0, 0};
// const int dy[4] = {0, 0, -1, 1};
// int dX[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
// int dY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

// Code from Here
// -------------------------------------------------------------------------------

int dp[200005];

int count(string s, int c) {
  int n = s.length(), pw = 1, ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      ans += pw;
      ans %= c;
    }
    // trace(c, ans);
    pw *= 2;
    pw %= c;
  }
  return ans;
}

signed main() {
  tezz

      int n;
  cin >> n;
  string s;
  cin >> s;
  int m = 0, c = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      c++;
  }
  int ans[n], pw1 = 1, pw2 = 1, pw3 = 1;
  dp[0] = 0;
  dp[1] = dp[2] = 1;
  for (int i = 3; i <= 200002; i++) {
    dp[i] = dp[i % __builtin_popcount(i)] + 1;
  }
  int x1;
  if (c > 1)
    x1 = count(s, c - 1);
  int x2 = count(s, c);
  int x3 = count(s, c + 1);
  // if(c==0){
  // 	for(int i=0; i<n; i++)cout << 1 << endl;
  // 		return 0;
  // }
  for (int i = n - 1; i >= 0; i--) {
    int C = c;
    if (s[i] == '0') {
      ans[i] = dp[(x3 + pw3) % (c + 1)] + 1;
    } else {
      if (c <= 1) {
        ans[i] = 0;
      } else
        ans[i] = dp[((x1 - pw1 + c - 1) % (c - 1) + (c - 1) % (c - 1))] + 1;
    }
    // trace(pw1, pw2, pw3);
    pw1 *= 2;
    pw2 *= 2;
    pw3 *= 2;
    if (c > 1)
      pw1 %= (c - 1);
    pw2 %= (c);
    pw3 %= (c + 1);
  }
  // trace(x1, x2, x3);
  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;

  return 0;
}

Error: 0

Fixed submission:

// Best practice

#include <bits/stdc++.h>

#define int long long
#define pb push_back
#define pf emplace_front
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pii pair<int, int>
#define psi pair<string, int>
#define vi vector<int>
#define vpii vector<pii>
#define vvi vector<vi>
#define sz(x) (int)(x).size()
#define x first
#define y second
#define endl '\\n'
#define tezz                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define MOD 1000000007
#define hell 998244353
#define prec(n) fixed << setprecision(n)
#define ini(a, i) memset(a, i, sizeof(a))
#define output(x) cout << (x ? "YES" : "NO") << endl;
using namespace std;

#define debug(vec)                                                             \\
  cout << (#vec) << " : [ ";                                                   \\
  for (auto &i : vec)                                                          \\
    cout << i << " ";                                                          \\
  cout << "]" << endl
#define debugp(vec)                                                            \\
  cout << (#vec) << " : [ ";                                                   \\
  for (auto &i : vec)                                                          \\
    cout << "(" << i.x << "," << i.y << ")"                                    \\
         << " ";                                                               \\
  cout << "]" << endl
#define trace(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
    cout << endl;                                                              \\
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cout << "[" << *it << ": " << a << "]\\t";
  err(++it, args...);
}

// const int dx[4] = { -1, 1, 0, 0};
// const int dy[4] = {0, 0, -1, 1};
// int dX[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
// int dY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

// Code from Here
// -------------------------------------------------------------------------------

int dp[200005];

int count(string s, int c) {
  int n = s.length(), pw = 1, ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      ans += pw;
      ans %= c;
    }
    // trace(c, ans);
    pw *= 2;
    pw %= c;
  }
  return ans;
}

signed main() {
  tezz

      int n;
  cin >> n;
  string s;
  cin >> s;
  int m = 0, c = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      c++;
  }
  int ans[n], pw1 = 1, pw2 = 1, pw3 = 1;
  dp[0] = 0;
  dp[1] = dp[2] = 1;
  for (int i = 3; i <= 200002; i++) {
    dp[i] = dp[i % __builtin_popcount(i)] + 1;
  }
  int x1 = count(s, c - 1);
  int x2 = count(s, c);
  int x3 = count(s, c + 1);
  for (int i = n - 1; i >= 0; i--) {
    int C = c;
    if (s[i] == '0') {
      ans[i] = dp[(x3 + pw3) % (c + 1)] + 1;
    } else {
      ans[i] = dp[(x1 - pw1 + c - 1) % (c - 1)] + 1;
    }
    // trace(pw1, pw2, pw3);
    pw1 *= 2;
    pw2 *= 2;
    pw3 *= 2;
    pw1 %= (c - 1);
    pw2 %= (c);
    pw3 %= (c + 1);
  }
  // trace(x1, x2, x3);
  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;

  return 0;
}