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

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pll pair<ll, ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lb(c, x) distance(c.begin(), lower_bound(all(c), x))
#define ub(c, x) distance(c.begin(), upper_bound(all(c), x))

using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ll INF = 1e9 + 7;

ll dp1[200100];

ll dp2[200100];

bool fin1[200100];
bool fin2[200100];

ll dfs1(ll v) {
  if (v == 0)
    return 0;
  if (fin1[v])
    return dp1[v];
  ll r = 0;
  ll be = 1;
  rep(i, 20) {
    if (be > v)
      break;
    be *= 2;
    if ((v >> i) % 2 == 1)
      r++;
  }
  ll res = dfs1(v % r);
  fin1[v] = true;
  return dp1[v] = res + 1;
}

ll dfs2(ll v) {
  if (v == 0)
    return 0;
  if (fin2[v])
    return dp2[v];
  ll r = 0;
  // string m=to_string(v);
  // ll t=m.length();
  ll be = 1;
  rep(i, 20) {
    if (be > v)
      break;
    be *= 2;
    if ((v >> i) % 2 == 1)
      r++;
  }
  ll res = dfs2(v % r);
  fin2[v] = true;
  return dp2[v] = res + 1;
}

int main() {
  ll n;
  cin >> n;
  string x;
  cin >> x;
  // vector<ll> cnt; //最初の割る方を求める
  ll sum = 0;
  rep(i, n) {
    if (x[i] == '1')
      sum++;
  }
  if (sum == 1) {
    vector<vector<ll>> p(200100, vector<ll>(2));

    ll b = 1;
    rep(i, 200100) {
      p[i][1] = b;
      b *= 2;
      b %= (sum + 1);
    }
    ll tot1 = 0, tot2 = 0;
    rep(i, n) {
      if (x[i] == '1') {
        tot2 += p[n - 1 - i][1];
        tot2 %= (sum + 1);
      }
    }
    rep(i, n) {
      ll tot = 0;
      if (x[i] == '1') {
        cout << 0 << endl;
      } else {
        tot += tot2 + p[n - 1 - i][1];
        tot %= sum + 1;
        cout << dfs2(tot) + 1 << endl;
      }
    }
    return 0;
  }

  if (sum == n) {
    // cnt.push_back(sum-1);
    vector<ll> p(200100);
    ll a = 1;
    rep(i, 200100) {
      p[i] = a;
      a *= 2;
      a %= (sum - 1);
    }
    ll tot1 = 0;
    rep(i, n) {
      if (x[i] == '1') {
        tot1 += p[n - 1 - i];
        tot1 %= (sum - 1);
      }
    }
    rep(i, n) {
      ll tot = 0;
      tot += tot1 + (sum - 1 - p[n - 1 - i]);
      tot += sum - 1;
      tot %= (sum - 1);
      cout << dfs1(tot) + 1 << endl;
    }
  } else if (sum == 0) {
    // cnt.push_back(sum+1);
    vector<ll> p(200100);
    ll a = 1;
    rep(i, 200100) {
      p[i] = a;
      a *= 2;
      a %= (sum + 1);
    }
    ll tot2 = 0;
    rep(i, n) {
      if (x[i] == '1') {
        tot2 += p[n - 1 - i];
        tot2 %= (sum + 1);
      }
    }
    rep(i, n) {
      ll tot = 0;
      tot += tot2 + p[n - 1 - i];
      tot %= (sum + 1);
      cout << dfs2(tot) + 1 << endl;
    }
  } else {
    // cnt.push_back(sum-1);
    // cnt.push_back(sum+1);
    vector<vector<ll>> p(200100, vector<ll>(2));
    ll a = 1;
    rep(i, 200100) {
      p[i][0] = a;
      a *= 2;
      a %= (sum - 1);
    }
    ll b = 1;
    rep(i, 200100) {
      p[i][1] = b;
      b *= 2;
      b %= (sum + 1);
    }
    ll tot1 = 0, tot2 = 0;
    rep(i, n) {
      if (x[i] == '1') {
        tot1 += p[n - 1 - i][0];
        tot1 %= (sum - 1);
        tot2 += p[n - 1 - i][1];
        tot2 %= (sum + 1);
      }
    }
    rep(i, n) {
      ll tot = 0;
      if (x[i] == '1') {
        tot += tot1 + (sum - 1 - p[n - 1 - i][0]);
        tot %= sum - 1;
        cout << dfs1(tot) + 1 << endl;
      } else {
        tot += tot2 + p[n - 1 - i][1];
        tot %= sum + 1;
        cout << dfs2(tot) + 1 << endl;
      }
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pll pair<ll, ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lb(c, x) distance(c.begin(), lower_bound(all(c), x))
#define ub(c, x) distance(c.begin(), upper_bound(all(c), x))

using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ll INF = 1e9 + 7;

ll dp1[200100];

ll dp2[200100];

bool fin1[200100];
bool fin2[200100];

ll dfs1(ll v) {
  if (v == 0)
    return 0;
  if (fin1[v])
    return dp1[v];
  ll r = 0;
  ll be = 1;
  rep(i, 10) {
    if (be > v)
      break;
    be *= 2;
    if ((v >> i) % 2 == 1)
      r++;
  }
  ll res = dfs1(v % r);
  fin1[v] = true;
  return dp1[v] = res + 1;
}

ll dfs2(ll v) {
  if (v == 0)
    return 0;
  if (fin2[v])
    return dp2[v];
  ll r = 0;
  // string m=to_string(v);
  // ll t=m.length();
  ll be = 1;
  rep(i, 10) {
    if (be > v)
      break;
    be *= 2;
    if ((v >> i) % 2 == 1)
      r++;
  }
  ll res = dfs2(v % r);
  fin2[v] = true;
  return dp2[v] = res + 1;
}

int main() {
  ll n;
  cin >> n;
  string x;
  cin >> x;
  // vector<ll> cnt; //最初の割る方を求める
  ll sum = 0;
  rep(i, n) {
    if (x[i] == '1')
      sum++;
  }
  if (sum == 1) {
    vector<vector<ll>> p(200100, vector<ll>(2));

    ll b = 1;
    rep(i, 200100) {
      p[i][1] = b;
      b *= 2;
      b %= (sum + 1);
    }
    ll tot1 = 0, tot2 = 0;
    rep(i, n) {
      if (x[i] == '1') {
        tot2 += p[n - 1 - i][1];
        tot2 %= (sum + 1);
      }
    }
    rep(i, n) {
      ll tot = 0;
      if (x[i] == '1') {
        cout << 0 << endl;
      } else {
        tot += tot2 + p[n - 1 - i][1];
        tot %= sum + 1;
        cout << dfs2(tot) + 1 << endl;
      }
    }
    return 0;
  }

  if (sum == n) {
    // cnt.push_back(sum-1);
    vector<ll> p(200100);
    ll a = 1;
    rep(i, 200100) {
      p[i] = a;
      a *= 2;
      a %= (sum - 1);
    }
    ll tot1 = 0;
    rep(i, n) {
      if (x[i] == '1') {
        tot1 += p[n - 1 - i];
        tot1 %= (sum - 1);
      }
    }
    rep(i, n) {
      ll tot = 0;
      tot += tot1 + (sum - 1 - p[n - 1 - i]);
      tot += sum - 1;
      tot %= (sum - 1);
      cout << dfs1(tot) + 1 << endl;
    }
  } else if (sum == 0) {
    // cnt.push_back(sum+1);
    vector<ll> p(200100);
    ll a = 1;
    rep(i, 200100) {
      p[i] = a;
      a *= 2;
      a %= (sum + 1);
    }
    ll tot2 = 0;
    rep(i, n) {
      if (x[i] == '1') {
        tot2 += p[n - 1 - i];
        tot2 %= (sum + 1);
      }
    }
    rep(i, n) {
      ll tot = 0;
      tot += tot2 + p[n - 1 - i];
      tot %= (sum + 1);
      cout << dfs2(tot) + 1 << endl;
    }
  } else {
    // cnt.push_back(sum-1);
    // cnt.push_back(sum+1);
    vector<vector<ll>> p(200100, vector<ll>(2));
    ll a = 1;
    rep(i, 200100) {
      p[i][0] = a;
      a *= 2;
      a %= (sum - 1);
    }
    ll b = 1;
    rep(i, 200100) {
      p[i][1] = b;
      b *= 2;
      b %= (sum + 1);
    }
    ll tot1 = 0, tot2 = 0;
    rep(i, n) {
      if (x[i] == '1') {
        tot1 += p[n - 1 - i][0];
        tot1 %= (sum - 1);
        tot2 += p[n - 1 - i][1];
        tot2 %= (sum + 1);
      }
    }
    rep(i, n) {
      ll tot = 0;
      if (x[i] == '1') {
        tot += tot1 + (sum - 1 - p[n - 1 - i][0]);
        tot %= sum - 1;
        cout << dfs1(tot) + 1 << endl;
      } else {
        tot += tot2 + p[n - 1 - i][1];
        tot %= sum + 1;
        cout << dfs2(tot) + 1 << endl;
      }
    }
  }

  return 0;
}