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


#include <bits/stdc++.h>

#define M_PI 3.14159265358979323846 // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;

#define rep(a, n) for (ll a = 0; a < n; a++)

static const ll INF = 1e18;

template <typename T> static inline void chmin(T &ref, const T value) {
  if (ref > value)
    ref = value;
}

template <typename T> static inline void chmax(T &ref, const T value) {
  if (ref < value)
    ref = value;
}

ll mpow(ll x, ll n, ll m) {
  ll ans = 1;
  x %= m;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % m;
    x = x * x % m;
    n = n >> 1;
  }
  return ans;
}

vector<ll> naive(int n, string s) {
  vector<ll> nums(n);
  rep(i, n) nums[i] = s[i] - '0';
  vector<ll> vals(n);
  for (int i = 0; i < n; i++) {
    ll value = 0;
    ll v = 1;
    nums[i] = (nums[i] + 1) % 2;
    ll pcount = 0;
    for (int j = 0; j < n; j++) {
      if (nums[j])
        pcount++;
    }
    for (int j = n - 1; j >= 0; j--) {
      value += nums[j] * v;
      v *= 2;
    }
    nums[i] = (nums[i] + 1) % 2;
    vals[i] = value % pcount;
  }
  return vals;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<ll> dp(n + 1, -1);
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    ll next = i % __builtin_popcountll(i);
    dp[i] = dp[next] + 1;
  }
  // string s に対して、bit反転したものを % popcount したときの余りは?
  // popcount = s の数 ± 1
  ll pcount = 0;
  rep(i, n) {
    if (s[i] == '1') {
      pcount++;
    }
  }
  vector<ll> m;
  for (auto p : {pcount + 1, abs(pcount - 1)}) {
    ll current = 0;
    if (p > 0) {
      rep(i, n) {
        current *= 2;
        current += (s[i] == '1' ? 1 : 0);
        current %= p;
      }
    }
    m.push_back(current);
  }
  rep(i, n) {
    if (s[i] == '1') {
      // minus
      const ll mod = pcount - 1;
      if (mod == 0) {
        cout << 0 << endl;
      } else {
        ll m2 = mpow(2, n - 1 - i, mod);
        ll ans = (m[1] - m2 + mod) % (mod);
        // cout << i << "," << ans << endl;
        cout << dp[ans] + 1 << endl;
      }
    } else {
      // plus
      const ll mod = pcount + 1;
      ll m2 = mpow(2, n - 1 - i, mod);
      ll ans = (m[0] + m2) % (mod);
      // cout << i << "," << ans << endl;
      cout << dp[ans] + 1 << endl;
    }
  }
  // cout << "debug" << endl;
  // auto a = naive(n, s);
  // for (auto p : a) {
  //	cout << p <<"," << dp[p] + 1 << endl;
  // }
  return 0;
}


Error: 0

Fixed submission:


#include <bits/stdc++.h>

#define M_PI 3.14159265358979323846 // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;

#define rep(a, n) for (ll a = 0; a < n; a++)

static const ll INF = 1e18;

template <typename T> static inline void chmin(T &ref, const T value) {
  if (ref > value)
    ref = value;
}

template <typename T> static inline void chmax(T &ref, const T value) {
  if (ref < value)
    ref = value;
}

ll mpow(ll x, ll n, ll m) {
  ll ans = 1;
  x %= m;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % m;
    x = x * x % m;
    n = n >> 1;
  }
  return ans;
}

vector<ll> naive(int n, string s) {
  vector<ll> nums(n);
  rep(i, n) nums[i] = s[i] - '0';
  vector<ll> vals(n);
  for (int i = 0; i < n; i++) {
    ll value = 0;
    ll v = 1;
    nums[i] = (nums[i] + 1) % 2;
    ll pcount = 0;
    for (int j = 0; j < n; j++) {
      if (nums[j])
        pcount++;
    }
    for (int j = n - 1; j >= 0; j--) {
      value += nums[j] * v;
      v *= 2;
    }
    nums[i] = (nums[i] + 1) % 2;
    vals[i] = value % pcount;
  }
  return vals;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<ll> dp(n + 1, -1);
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    ll next = i % __builtin_popcount(i);
    dp[i] = dp[next] + 1;
  }
  // string s に対して、bit反転したものを % popcount したときの余りは?
  // popcount = s の数 ± 1
  ll pcount = 0;
  rep(i, n) {
    if (s[i] == '1') {
      pcount++;
    }
  }
  vector<ll> m;
  for (auto p : {pcount + 1, abs(pcount - 1)}) {
    ll current = 0;
    if (pcount) {
      rep(i, n) {
        current *= 2;
        current += (s[i] == '1' ? 1 : 0);
        current %= p;
      }
    }
    m.push_back(current);
  }
  rep(i, n) {
    if (s[i] == '1') {
      // minus
      const ll mod = pcount - 1;
      if (mod == 0) {
        cout << 0 << endl;
      } else {
        ll m2 = mpow(2, n - 1 - i, mod);
        ll ans = (m[1] - m2 + mod) % (mod);
        // cout << i << "," << ans << endl;
        cout << dp[ans] + 1 << endl;
      }
    } else {
      // plus
      const ll mod = pcount + 1;
      ll m2 = mpow(2, n - 1 - i, mod);
      ll ans = (m[0] + m2) % (mod);
      // cout << i << "," << ans << endl;
      cout << dp[ans] + 1 << endl;
    }
  }
  // cout << "debug" << endl;
  // auto a = naive(n, s);
  // for (auto p : a) {
  //	cout << p <<"," << dp[p] + 1 << endl;
  // }
  return 0;
}