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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
#define int long long
typedef unsigned long long lu;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;
typedef vector<string> vs;
typedef vector<pair<ll, ll>> vpr;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef long double ld;
#define f(i, n) for (ll i = 0; i < n; i++)
#define ff(i, n) for (ll i = 1; i <= n; i++)
#define pb push_back
#define mp make_pair
#define endl "\\n"
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define amax(x, y)                                                             \\
  if (y > x)                                                                   \\
    x = y;
#define amin(x, y)                                                             \\
  if (y < x)                                                                   \\
    x = y;
#define bg(x) x.begin()
#define sz(x) (ll) x.size()
#define in(x, n)                                                               \\
  for (ll i = 0; i < n; i++)                                                   \\
  cin >> x[i]
#define out(x, n)                                                              \\
  for (ll i = 0; i < n; i++)                                                   \\
  cout << x[i] << " "
#define mxt(a) *(max_element(a.begin(), a.end()))
#define mnt(a) *(min_element(a.begin(),a.end())
#define tc                                                                     \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
typedef pair<ll, ll> pi;
#define yes cout << "YES\\n";
#define no cout << "NO\\n";
#define yesno(f)                                                               \\
  if (f)                                                                       \\
  yes else no
const v dx = {1, -1, 0, 0};
const v dy = {0, 0, 1, -1};
const ld PI = 2 * acos(0.0);
ll cel(ll x1, ll y1) {
  if ((x1 % y1) == 0)
    return x1 / y1;
  else
    return x1 / y1 + 1;
}
ll power(ll a, ll b, ll m) {
  if (b == 0)
    return 1;
  ll d = power(a, b / 2, m);
  d = (d * d) % m;
  if (b & 1)
    d = (d * a) % m;
  return d;
}
const ll mod = 1e9 + 7;
int MOD(int a) {
  if (a < 0)
    a += mod;
  if (a >= mod)
    a %= mod;
  return a;
}
// set_name.find_by_order(k) It returns to an iterator to the kth element
// (counting from zero) in the set in O(logn) time set_name.order_of_key(k) It
// returns to the number of items that are strictly smaller than our item k in
// O(logn) time.
/*string operations :
str.substr (x,y) : returns a substring str[x],str[x+1],...str[x+y-1]
str.substr (x) : returns a substring str[x],... end of string
str.find(qtr) : returns the first occurenece of qtr in str */
int fun(int i) {
  int c = 0;
  while (i) {
    c += (i % 2);
    i /= 2;
  }
  return c;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int n;
  cin >> n;
  string s;
  cin >> s;
  int c = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      c++;
  }
  v ans(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    ans[i] = ans[i % fun(i)] + 1;
  }
  // out(ans,n);
  vv powers(n, v(2, 1)), pre(n, v(2, 0)), suf(n, v(2, 0));
  int c1 = c + 1, c2 = max(c - 1, 1ll);
  bool flg = false;
  if (c == 1)
    flg = true;
  for (int i = 1; i < n; i++) {
    powers[i][0] = (2 * powers[i - 1][0]) % c1;
    powers[i][1] = (2 * powers[i - 1][1]) % c2;
  }
  // v pre(n),suf(n);
  if (s[0] == '1') {
    pre[0][0] = powers[n - 1][0];
    pre[0][1] = powers[n - 1][1];
  }
  if (s[n - 1] == '1') {
    suf[n - 1][0] = powers[0][0];
    suf[n - 1][1] = powers[0][1];
  }
  for (int i = 1; i < n; i++) {
    if (s[i] == '0') {
      pre[i][0] = pre[i - 1][0];
      pre[i][1] = pre[i - 1][1];
      continue;
    } else {
      pre[i][0] = (pre[i - 1][0] + powers[n - 1 - i][0]) % c1;
      pre[i][1] = (pre[i - 1][1] + powers[n - 1 - i][1]) % c2;
    }
  }
  for (int i = n - 2; i >= 0; i--) {
    if (s[i] == '0') {
      suf[i][0] = suf[i + 1][0];
      suf[i][1] = suf[i + 1][1];
      continue;
    } else {
      suf[i][0] = (suf[i + 1][0] + powers[n - i - 1][0]) % c1;
      suf[i][1] = (suf[i + 1][1] + powers[n - i - 1][1]) % c2;
    }
  }
  for (int i = 0; i < n; i++) {

    int ch = 0;
    if (s[i] == '1') {
      if (flg) {
        cout << 0 << endl;
        continue;
      }
      // int ch=0;
      if (i != 0)
        ch += pre[i - 1][1];
      if (i != (n - 1))
        ch += suf[i + 1][1];
      ch %= c2;
    } else {
      if (i != 0)
        ch += pre[i - 1][0];
      if (i != (n - 1))
        ch += suf[i + 1][0];
      ch %= c1;
      ch += powers[n - i - 1][0];
      ch %= c1;
    }

    // cout<<ch<<endl;
    cout << ans[ch] + 1 << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
#define int long long
typedef unsigned long long lu;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;
typedef vector<string> vs;
typedef vector<pair<ll, ll>> vpr;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef long double ld;
#define f(i, n) for (ll i = 0; i < n; i++)
#define ff(i, n) for (ll i = 1; i <= n; i++)
#define pb push_back
#define mp make_pair
#define endl "\\n"
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define amax(x, y)                                                             \\
  if (y > x)                                                                   \\
    x = y;
#define amin(x, y)                                                             \\
  if (y < x)                                                                   \\
    x = y;
#define bg(x) x.begin()
#define sz(x) (ll) x.size()
#define in(x, n)                                                               \\
  for (ll i = 0; i < n; i++)                                                   \\
  cin >> x[i]
#define out(x, n)                                                              \\
  for (ll i = 0; i < n; i++)                                                   \\
  cout << x[i] << " "
#define mxt(a) *(max_element(a.begin(), a.end()))
#define mnt(a) *(min_element(a.begin(),a.end())
#define tc                                                                     \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
typedef pair<ll, ll> pi;
#define yes cout << "YES\\n";
#define no cout << "NO\\n";
#define yesno(f)                                                               \\
  if (f)                                                                       \\
  yes else no
const v dx = {1, -1, 0, 0};
const v dy = {0, 0, 1, -1};
const ld PI = 2 * acos(0.0);
ll cel(ll x1, ll y1) {
  if ((x1 % y1) == 0)
    return x1 / y1;
  else
    return x1 / y1 + 1;
}
ll power(ll a, ll b, ll m) {
  if (b == 0)
    return 1;
  ll d = power(a, b / 2, m);
  d = (d * d) % m;
  if (b & 1)
    d = (d * a) % m;
  return d;
}
const ll mod = 1e9 + 7;
int MOD(int a) {
  if (a < 0)
    a += mod;
  if (a >= mod)
    a %= mod;
  return a;
}
// set_name.find_by_order(k) It returns to an iterator to the kth element
// (counting from zero) in the set in O(logn) time set_name.order_of_key(k) It
// returns to the number of items that are strictly smaller than our item k in
// O(logn) time.
/*string operations :
str.substr (x,y) : returns a substring str[x],str[x+1],...str[x+y-1]
str.substr (x) : returns a substring str[x],... end of string
str.find(qtr) : returns the first occurenece of qtr in str */
int fun(int i) {
  int c = 0;
  while (i) {
    c += (i % 2);
    i /= 2;
  }
  return c;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int n;
  cin >> n;
  string s;
  cin >> s;
  int c = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      c++;
  }
  v ans(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    ans[i] = ans[i % fun(i)] + 1;
  }
  // out(ans,n);
  vv powers(n, v(2, 1)), pre(n, v(2, 0)), suf(n, v(2, 0));
  int c1 = c + 1, c2 = c - 1;
  for (int i = 1; i < n; i++) {
    powers[i][0] = (2 * powers[i - 1][0]) % c1;
    powers[i][1] = (2 * powers[i - 1][1]) % c2;
  }
  // v pre(n),suf(n);
  if (s[0] == '1') {
    pre[0][0] = powers[n - 1][0];
    pre[0][1] = powers[n - 1][1];
  }
  if (s[n - 1] == '1') {
    suf[n - 1][0] = powers[0][0];
    suf[n - 1][1] = powers[0][1];
  }
  for (int i = 1; i < n; i++) {
    if (s[i] == '0') {
      pre[i][0] = pre[i - 1][0];
      pre[i][1] = pre[i - 1][1];
      continue;
    } else {
      pre[i][0] = (pre[i - 1][0] + powers[n - 1 - i][0]) % c1;
      pre[i][1] = (pre[i - 1][1] + powers[n - 1 - i][1]) % c2;
    }
  }
  for (int i = n - 2; i >= 0; i--) {
    if (s[i] == '0') {
      suf[i][0] = suf[i + 1][0];
      suf[i][1] = suf[i + 1][1];
      continue;
    } else {
      suf[i][0] = (suf[i + 1][0] + powers[n - i - 1][0]) % c1;
      suf[i][1] = (suf[i + 1][1] + powers[n - i - 1][1]) % c2;
    }
  }
  for (int i = 0; i < n; i++) {
    int ch = 0;
    if (s[i] == '1') {
      // int ch=0;
      if (i != 0)
        ch += pre[i - 1][1];
      if (i != (n - 1))
        ch += suf[i + 1][1];
      ch %= c2;
    } else {
      if (i != 0)
        ch += pre[i - 1][0];
      if (i != (n - 1))
        ch += suf[i + 1][0];
      ch %= c1;
      ch += powers[n - i - 1][0];
      ch %= c1;
    }
    // cout<<ch<<endl;
    cout << ans[ch] + 1 << endl;
  }
  return 0;
}