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


// Problem : D - Anything Goes to Zero
// Contest : AtCoder - AIsing Programming Contest 2020
// URL : https://atcoder.jp/contests/aising2020/tasks/aising2020_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, b, a) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)

#define bug(x) cout << #x << "=" << x << endl;
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert

template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ld pii = 3.14159265359;
const int MOD = 1000000007;
const char nl = '\\n';
const int MX = 200001; // check the limits, dummy

int g0(int N) {
  // bug(N);
  int ans = 0;
  while (__builtin_popcount(N) != 0) {
    ans++;
    N %= __builtin_popcount(N);
  }

  return ans;
}

int main() {
  // && has higher precedence than ||
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int N;
  cin >> N;
  string str;
  cin >> str;
  int c = 0;
  F0R(x, N) {
    if (str[x] == '1')
      c++;
  }
  // bug(c);
  vi b, d;
  b.pb(1 % (c - 1 == 0 ? 1 : c - 1));
  d.pb(1 % (c + 1));
  F0R(x, N - 1) {
    b.pb((b.back() * 2) % (c - 1 == 0 ? 1 : c - 1));
    d.pb((d.back() * 2) % (c + 1));
    // bug(b.back());
    // bug(d.back());
    // cout<<nl;
  }
  int vb = 0, vd = 0;
  reverse(all(str));
  F0R(x, N) {
    if (str[x] == '1') {
      vb = (vb + b[x]) % (c - 1 == 0 ? 1 : c - 1);
      vd = (vd + d[x]) % (c + 1);
    }
  }
  // bug(vb);
  // bug(vd);
  vi res;
  F0R(x, N) {
    int val;
    if (str[x] == '1') {
      if (c == 1) {
        res.pb(0);
        continue;
      }
      val = (vb - b[x] + c - 1) % (c - 1 == 0 ? 1 : c - 1);
    } else {
      // bug(d[x]);
      val = (vd + d[x]) % (c + 1);
    }
    // cout<<g0(val)<<nl;
    res.pb(1 + g0(val));
  }
  F0Rd(x, N) cout << res[x] << nl;
  return 0;
}

// read the question correctly (ll vs int)
// template by bqi343

Error: 0

Fixed submission:


// Problem : D - Anything Goes to Zero
// Contest : AtCoder - AIsing Programming Contest 2020
// URL : https://atcoder.jp/contests/aising2020/tasks/aising2020_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, b, a) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)

#define bug(x) cout << #x << "=" << x << endl;
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert

template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ld pii = 3.14159265359;
const int MOD = 1000000007;
const char nl = '\\n';
const int MX = 200001; // check the limits, dummy

int g0(int N) {
  // bug(N);
  int ans = 0;
  while (__builtin_popcount(N) != 0) {
    ans++;
    N %= __builtin_popcount(N);
  }

  return ans;
}

int main() {
  // && has higher precedence than ||
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int N;
  cin >> N;
  string str;
  cin >> str;
  int c = 0;
  F0R(x, N) {
    if (str[x] == '1')
      c++;
  }
  // bug(c);
  vi b, d;
  b.pb(1 % (c - 1));
  d.pb(1 % (c + 1));
  F0R(x, N - 1) {
    b.pb((b.back() * 2) % (c - 1));
    d.pb((d.back() * 2) % (c + 1));
    // bug(b.back());
    // bug(d.back());
    // cout<<nl;
  }
  int vb = 0, vd = 0;
  reverse(all(str));
  F0R(x, N) {
    if (str[x] == '1') {
      vb = (vb + b[x]) % (c - 1);
      vd = (vd + d[x]) % (c + 1);
    }
  }
  // bug(vb);
  // bug(vd);
  vi res;
  F0R(x, N) {
    int val;
    if (str[x] == '1') {
      val = (vb - b[x] + c - 1) % (c - 1);
    } else {
      // bug(d[x]);
      val = (vd + d[x]) % (c + 1);
    }
    // cout<<g0(val)<<nl;
    res.pb(1 + g0(val));
  }
  F0Rd(x, N) cout << res[x] << nl;
  return 0;
}

// read the question correctly (ll vs int)
// template by bqi343