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


/* You are being watched. */

//~ while (clock() <= 69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")

#include <bits/stdc++.h>

//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC
//target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") ~ #pragma
//GCC optimize("unroll-loops")

using namespace std;

//~ #include <ext/pb_ds/assoc_container.hpp>
//~ #include <ext/pb_ds/tree_policy.hpp>
//~ using namespace __gnu_pbds;
//~ template <typename T>
//~ using ordered_set =
//~ tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define all(x) (x).begin(), (x).end()
#define show(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << "\\n"
#define FastIO                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
// using li = __int128;
// using uli = unsigned __int128;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

double getTime() { return clock() / (double)CLOCKS_PER_SEC; }

void read(){};

template <typename T, typename... Args> void read(T &a, Args &...args) {
  cin >> a;
  read(args...);
}

void print(){};

template <typename T, typename... Args> void print(T a, Args... args) {
  cout << a << " \\n"[sizeof...(args) == 0];
  print(args...);
}

const int N = 1e4 + 5;

ll get(ll x, ll y, ll MOD) {
  ll res = 1LL;
  while (y) {
    if (y & 1)
      res = (res * x) % MOD;
    y >>= 1LL;
    x = (x * x) % MOD;
  }
  return res;
}

int moves(ll x) {
  int ans = 0;
  while (x) {
    x = x % __builtin_popcountll(x);
    ans++;
  }
  return ans;
}

void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int have = count(s.begin(), s.end(), '1');
  map<ll, ll> val;
  for (ll can : {have - 1, have, have + 1}) {
    if (can <= 0)
      continue;
    ll cur = 0ll;
    for (int i = 0; i < n; i++) {
      if (s[i] == '1')
        cur = (cur + get(2LL, n - i - 1LL, can)) % can;
    }
    val[can] = cur;
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      ll cur = val[have + 1];
      cur = (cur + get(2LL, n - i - 1LL, have + 1)) % (have + 1);
      print(moves(cur) + 1);
    } else {
      if (val.count(have - 1) == 0) {
        print(0);
        continue;
      }
      ll cur = val[have - 1];
      cur = (cur - get(2LL, n - i - 1LL, have - 1)) % (have - 1);
      if (cur < 0)
        cur += (have - 1);
      print(moves(cur) + 1);
    }
  }
}

int main() {
  int t;
  t = 1;
  while (t--)
    solve();
}


Error: 0

Fixed submission:


/* You are being watched. */

//~ while (clock() <= 69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")

#include <bits/stdc++.h>

//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC
//target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") ~ #pragma
//GCC optimize("unroll-loops")

using namespace std;

//~ #include <ext/pb_ds/assoc_container.hpp>
//~ #include <ext/pb_ds/tree_policy.hpp>
//~ using namespace __gnu_pbds;
//~ template <typename T>
//~ using ordered_set =
//~ tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define all(x) (x).begin(), (x).end()
#define show(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << "\\n"
#define FastIO                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
// using li = __int128;
// using uli = unsigned __int128;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

double getTime() { return clock() / (double)CLOCKS_PER_SEC; }

void read(){};

template <typename T, typename... Args> void read(T &a, Args &...args) {
  cin >> a;
  read(args...);
}

void print(){};

template <typename T, typename... Args> void print(T a, Args... args) {
  cout << a << " \\n"[sizeof...(args) == 0];
  print(args...);
}

const int N = 1e4 + 5;

ll get(ll x, ll y, ll MOD) {
  ll res = 1LL;
  while (y) {
    if (y & 1)
      res = (res * x) % MOD;
    y >>= 1LL;
    x = (x * x) % MOD;
  }
  return res;
}

int moves(ll x) {
  int ans = 0;
  while (x) {
    x = x % __builtin_popcountll(x);
    ans++;
  }
  return ans;
}

void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int have = count(s.begin(), s.end(), '1');
  map<int, ll> val;
  for (auto can : {have - 1, have, have + 1}) {
    if (have <= 0)
      continue;
    ll cur = 0ll;
    for (int i = 0; i < n; i++) {
      if (s[i] == '1')
        cur = (cur + get(2LL, n - i - 1LL, can)) % can;
    }
    val[can] = cur;
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      ll cur = val[have + 1];
      cur = (cur + get(2LL, n - i - 1LL, have + 1)) % (have + 1);
      print(moves(cur) + 1);
    } else {
      if (val.count(have - 1) == 0) {
        print(0);
        continue;
      }
      if (have - 1 == 0) {
        print("baal");
        continue;
      }
      ll cur = val[have - 1];
      cur = (cur - get(2LL, n - i - 1LL, have - 1)) % (have - 1);
      if (cur < 0)
        cur += (have - 1);
      print(moves(cur) + 1);
    }
  }
}

int main() {
  int t;
  t = 1;
  while (t--)
    solve();
}