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

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define fst first
#define snd second
#define pb push_back
#define ALL(x) x.begin(), x.end()
#define EPS (1e-9)
#define equals(a, b) (fabs((a) - (b)) < EPS)

using namespace std;

bool LT(double a, double b) { return !equals(a, b) && a < b; }
bool LTE(double a, double b) { return equals(a, b) || a < b; }

const string YES = "";
const string NO = "";

typedef long long ll;
typedef pair<int, int> ii;

long long extgcd(long long a, long long b, long long &x, long long &y) {
  long long d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else
    x = 1, y = 0;
  return d;
}

long long mod_inv(long long a, long long m) {
  long long x, y;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}

ll modmultiply(ll a, ll b, ll c) {
  ll x = 0, y = a % c;
  while (b > 0) {
    if (b % 2 == 1)
      x = (x + y) % c;
    y = (y * 2) % c;
    b /= 2;
  }
  return x % c;
}

ll modpow(ll x, ll y, ll mod) {
  ll ret = 1; // ret = x^y%mod;
  while (y) {
    if (y & 1)
      // ret = (ret*x)%mod;
      ret = modmultiply(ret, x, mod);
    // x = (x*x)%mod;
    x = modmultiply(x, x, mod);
    y >>= 1;
  }
  return ret;
}

map<ll, ll> memo;
ll f(ll x) {
  if (x == 0)
    return 0;
  if (memo.count(x) != 0) {
    return memo[x];
  }
  // cout << "here x = " << x << endl;
  ll count = __builtin_popcount(x);
  ll key = x % count;
  return memo[x] = f(key) + 1LL;
}

int n;
string s;
vector<ll> pw2vec[2];

ll calc(string X, ll mod, int type) {
  ll v = 0;
  int ps = ((type == -1) ? 0 : 1);
  rep(i, (int)X.size()) {
    if (X[i] == '0')
      continue;
    int keta = n - 1 - i;
    ll pw2 = pw2vec[ps][keta];
    v = v + pw2;
    v %= mod;
    pw2 *= 2LL;
    pw2 %= mod;
  }
  return v;
}

const bool debug = 0;

ll bf_f(string t) {
  int one = 0;
  rep(i, (int)t.size()) if (t[i] == '1')++ one;
  ll v = 0;
  rep(i, (int)t.size()) {
    if (t[i] == '1') {
      v = v + modpow(2LL, (int)t.size() - 1 - i, one);
      v %= one;
    }
  }
  return v;
}

void solve() {
  /*
  if( debug ) {
    ll v = 0, pw2 = 1;
    rep(i,n) {
      v = v + (s[i]-'0') * pw2;
      pw2 *= 2LL;
    }
    cout << "v = " + v << " f = " << f(v) << endl;
  }
  */
  int one = 0;
  rep(i, n) if (s[i] == '1')++ one;
  // precalc
  map<ll, ll> mp;
  if (one - 1 >= 1) {
    ll v = 1;
    pw2vec[0].pb(v);
    rep(i, n + 1) {
      v = v * 2LL;
      v %= (one - 1LL);
      pw2vec[0].pb(v);
    }
    mp[-1] = calc(s, one - 1, -1);
    if (debug)
      cout << "* -1 calc = " << mp[-1] << endl;
  }
  if (one + 1 >= 1) {
    ll v = 1;
    pw2vec[1].pb(v);
    rep(i, n + 1) {
      v = v * 2LL;
      v %= (one + 1LL);
      pw2vec[1].pb(v);
    }
    mp[+1] = calc(s, one + 1, +1);
    if (debug)
      cout << "* +1 calc = " << mp[+1] << endl;
  }

  rep(i, n) {
    int keta = n - 1 - i;
    int cone = one;
    ll key;
    if (s[i] == '0') {
      int ps = 1;
      ++cone;
      key = mp[+1];
      // key += modpow(2LL,keta,cone);
      key += pw2vec[ps][keta];
      key %= cone;
    } else {
      int ps = 0;
      --cone;
      if (cone == 0) {
        cout << 0 << endl;
        continue;
      }

      key = mp[-1];
      // key -= modpow(2LL,keta,cone);
      key -= pw2vec[ps][keta];
      while (key < 0LL)
        key += cone;
      key %= cone;
    }

    if (debug) {
      cout << "key = " << key << endl;
      string t = s;
      t[i] = ((t[i] == '0') ? '1' : '0');
      cout << "t = " << t << endl;
      cout << "bf_f = " << bf_f(t) << endl;
      assert(key == bf_f(t));
    }
    cout << f(key) + 1 << endl;
  }
}

int main() {
  /*
  cout << 200000 << endl;
  rep(i,2*100000) {
    cout << rand()%2;
  } puts(""); return 0;
  */

  // cout << f(7) << " " << f(3) << " " << f(1) << " " << f(0) << endl;
  cin >> n >> s;
  solve();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define fst first
#define snd second
#define pb push_back
#define ALL(x) x.begin(), x.end()
#define EPS (1e-9)
#define equals(a, b) (fabs((a) - (b)) < EPS)

using namespace std;

bool LT(double a, double b) { return !equals(a, b) && a < b; }
bool LTE(double a, double b) { return equals(a, b) || a < b; }

const string YES = "";
const string NO = "";

typedef long long ll;
typedef pair<int, int> ii;

long long extgcd(long long a, long long b, long long &x, long long &y) {
  long long d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else
    x = 1, y = 0;
  return d;
}

long long mod_inv(long long a, long long m) {
  long long x, y;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}

ll modmultiply(ll a, ll b, ll c) {
  ll x = 0, y = a % c;
  while (b > 0) {
    if (b % 2 == 1)
      x = (x + y) % c;
    y = (y * 2) % c;
    b /= 2;
  }
  return x % c;
}

ll modpow(ll x, ll y, ll mod) {
  ll ret = 1; // ret = x^y%mod;
  while (y) {
    if (y & 1)
      // ret = (ret*x)%mod;
      ret = modmultiply(ret, x, mod);
    // x = (x*x)%mod;
    x = modmultiply(x, x, mod);
    y >>= 1;
  }
  return ret;
}

map<ll, ll> memo;
ll f(ll x) {
  if (x == 0)
    return 0;
  if (memo.count(x) != 0) {
    return memo[x];
  }
  // cout << "here x = " << x << endl;
  ll count = __builtin_popcount(x);
  ll key = x % count;
  return memo[x] = f(key) + 1LL;
}

int n;
string s;
vector<ll> pw2vec[2];

ll calc(string X, ll mod, int type) {
  ll v = 0;
  int ps = ((type == -1) ? 0 : 1);
  rep(i, (int)X.size()) {
    if (X[i] == '0')
      continue;
    int keta = n - 1 - i;
    ll pw2 = pw2vec[ps][keta];
    v = v + pw2;
    v %= mod;
    pw2 *= 2LL;
    pw2 %= mod;
  }
  return v;
}

const bool debug = 0;

ll bf_f(string t) {
  int one = 0;
  rep(i, (int)t.size()) if (t[i] == '1')++ one;
  ll v = 0;
  rep(i, (int)t.size()) {
    if (t[i] == '1') {
      v = v + modpow(2LL, (int)t.size() - 1 - i, one);
      v %= one;
    }
  }
  return v;
}

void solve() {
  /*
  if( debug ) {
    ll v = 0, pw2 = 1;
    rep(i,n) {
      v = v + (s[i]-'0') * pw2;
      pw2 *= 2LL;
    }
    cout << "v = " + v << " f = " << f(v) << endl;
  }
  */
  int one = 0;
  rep(i, n) if (s[i] == '1')++ one;
  // precalc
  map<ll, ll> mp;
  if (one - 1 >= 0) {
    ll v = 1;
    pw2vec[0].pb(v);
    rep(i, n + 1) {
      v = v * 2LL;
      v %= (one - 1LL);
      pw2vec[0].pb(v);
    }
    mp[-1] = calc(s, one - 1, -1);
    if (debug)
      cout << "* -1 calc = " << mp[-1] << endl;
  }
  if (one + 1 >= 0) {
    ll v = 1;
    pw2vec[1].pb(v);
    rep(i, n + 1) {
      v = v * 2LL;
      v %= (one + 1LL);
      pw2vec[1].pb(v);
    }
    mp[+1] = calc(s, one + 1, +1);
    if (debug)
      cout << "* +1 calc = " << mp[+1] << endl;
  }

  rep(i, n) {
    int keta = n - 1 - i;
    int cone = one;
    ll key;
    if (s[i] == '0') {
      int ps = 1;
      ++cone;
      key = mp[+1];
      // key += modpow(2LL,keta,cone);
      key += pw2vec[ps][keta];
      key %= cone;
    } else {
      int ps = 0;
      --cone;
      key = mp[-1];
      // key -= modpow(2LL,keta,cone);
      key -= pw2vec[ps][keta];
      while (key < 0LL)
        key += cone;
      key %= cone;
    }
    if (debug) {
      cout << "key = " << key << endl;
      string t = s;
      t[i] = ((t[i] == '0') ? '1' : '0');
      cout << "t = " << t << endl;
      cout << "bf_f = " << bf_f(t) << endl;
      assert(key == bf_f(t));
    }
    cout << f(key) + 1 << endl;
  }
}

int main() {
  /*
  cout << 200000 << endl;
  rep(i,2*100000) {
    cout << 1;
  } puts(""); return 0;
  */
  // cout << f(7) << " " << f(3) << " " << f(1) << " " << f(0) << endl;
  cin >> n >> s;
  solve();
  return 0;
}