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

#if 0
    clear
    g++ -std=c++17 -O2 -Wall -lm "$0" && ./a.out
    exit
#endif
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#define endl '\\n'
#define pb push_back
#define fir first
#define sec second
#define ins insert
#define umap map
#define unordered_map map
#define mset multiset
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
const int INF = 0x3f3f3f3f;
const int MAXN = 2e5 + 10;
const int MOD = 1e9 + 7;
const long long INFLL = 0x3f3f3f3f3f3f3f3f;
long long TESTCASE_NUM = 1;
clock_t CLOCK_START_TIME = clock();
template <typename T> inline void _read(T &x) { cin >> x; }
inline void _read(int &x) { scanf("%d", &x); }
inline void _read(long long &x) { scanf("%lld", &x); }
inline void _read(float &x) { scanf("%f", &x); }
inline void _read(double &x) { scanf("%lf", &x); }
inline void _read(char &x) { x = getchar(); }
inline void _read(char *x) { scanf("%s", x); }
inline void read() {}
template <typename T, typename... U> inline void read(T &head, U &...tail) {
  _read(head);
  read(tail...);
}
template <typename T> inline void _write(const T &x) { cout << x; }
inline void _write(const int &x) { printf("%d", x); }
inline void _write(const long long &x) { printf("%lld", x); }
inline void _write(const float &x) { printf("%.6f", x); }
inline void _write(const double &x) { printf("%.8lf", x); }
inline void _write(const char &x) { putchar(x); }
inline void _write(const char *x) { printf("%s", x); }
inline void write() {}
template <typename T, typename... U>
inline void write(const T &head, const U &...tail) {
  _write(head);
  write(tail...);
}
inline void debug() {}
#ifdef _USE_DEBUG
template <typename T, typename... U>
inline void debug(const T &head, const U &...tail) {
  _write("\\033[1m\\033[33m\\33[4m");
  write(head, tail...);
  _write("\\033[0m");
  // system("sleep 1");
}
#else
template <typename T, typename... U>
inline void debug(const T &head, const U &...tail) {}
#endif
void solve();
template <typename T1, typename T2> inline T1 max(T1 a, T2 b) {
  return a < b ? b : a;
}
template <typename T1, typename T2> inline T1 min(T1 a, T2 b) {
  return a < b ? a : b;
}
template <typename T> inline bool isprime(T p) {
  if (p == 2 || p == 3)
    return 1;
  if (p < 2 || (p % 6 != 1 && p % 6 != 5))
    return 0;
  for (T i = 5; i * i <= p; i = i + 6)
    if (p % i == 0 || p % (i + 2) == 0)
      return 0;
  return 1;
}

// Unordered_map can have O(n) complexity in some cases, use map instead.
// To NOT replace unordered_map with map, use the following lines.
#if 0
#define _USE_UNORDERED_MAP
#undef unordered_map
#undef umap
#define umap unordered_map
#endif

// Using long long globally can have undesirable side effects.
// To replace int with long long, use the following lines.
#if 0
#define _USE_LONG_LONG
#define int long long
#define pii pair<long long, long long>
#define vi vector<long long>
#define INF 0x3f3f3f3f3f3f3f3f
#endif

void DISPLAY_DEBUG_INFO() {
#ifdef _USE_DEBUG
  printf("\\n\\033[1m\\033[32m=> \\033[1m\\033[33m%ld\\033[1m\\033[32m ms used. ",
         clock() - CLOCK_START_TIME);
#endif
#if (defined _USE_LONG_LONG) && (defined _USE_DEBUG)
  printf("\\033[1m\\033[32mAll \\033[1m\\033[33mint\\033[1m\\033[32m has been "
         "replaced by \\033[1m\\033[33mlong long\\033[1m\\033[32m.\\033[0m ");
#endif
#if (!defined _USE_UNORDERED_MAP) && (defined _USE_DEBUG)
  printf("\\033[1m\\033[32mAll \\033[1m\\033[33munordered_map\\033[1m\\033[32m has "
         "been replaced by \\033[1m\\033[33mmap\\033[1m\\033[32m.\\033[0m ");
#endif
#ifdef _USE_DEBUG
  printf("\\033[0m\\n\\n");
#endif
}

signed main() {
#ifdef _USE_FILEIO
  freopen("in1.txt", "r", stdin);
  // freopen("l.txt", "p[3]", stdout);
#endif
  // scanf("%lld\\n", &TESTCASE_NUM);
  while (TESTCASE_NUM--)
    solve();
  DISPLAY_DEBUG_INFO();
  return 0;
}

ll preadd[MAXN + 10];
ll premin[MAXN + 10];

void solve() {
  ll n;
  read(n);
  string a;
  read(a);
  ll cnt = 0;
  ll baseadd = 1;
  ll basemin = 1;
  ll totadd = 0;
  ll totmin = 0;
  for (ll i = 0; i < a.size(); i++) {
    if (a[i] == '1') {
      cnt++;
    }
  }
  if (cnt == 0) {
    for (ll i = 0; i < a.size(); i++) {
      write(1, endl);
    }
    return;
  }
  if (cnt == 1) {
    for (ll i = 0; i < a.size(); i++) {
      preadd[a.size() - i - 1] = baseadd;
      baseadd = baseadd * 2LL % (cnt + 1);
      if (a[a.size() - i - 1] == '1') {
        totadd += preadd[a.size() - i - 1];
        totadd %= cnt + 1;
      }
    }
    for (ll i = 0; i < a.size(); i++) {
      if (a[i] == '1') {
        write(0, endl);
      } else {
        ll temp = totadd;
        temp = (temp + preadd[i]) % (cnt + 1);
        ll ansans = 1;
        while (1) {
          if (temp == 0) {
            write(ansans, endl);
            break;
          }
          ansans++;
          temp = temp % __builtin_popcountll(temp);
        }
      }
    }
  } else {
    for (ll i = 0; i < a.size(); i++) {
      preadd[a.size() - i - 1] = baseadd;
      premin[a.size() - i - 1] = basemin;
      baseadd = baseadd * 2LL % (cnt + 1);
      basemin = basemin * 2LL % (cnt - 1);

      if (a[a.size() - i - 1] == '1') {
        totadd += preadd[a.size() - i - 1];
        totadd %= cnt + 1;
        totmin += premin[a.size() - i - 1];
        totmin %= cnt - 1;
      }
    }
    for (ll i = 0; i < a.size(); i++) {
      if (a[i] == '1') {
        ll temp = totmin;
        temp = ((temp - premin[i]) % (cnt - 1) + cnt - 1) % (cnt - 1);
        ll ansans = 1;
        while (1) {
          if (temp == 0) {
            write(ansans, endl);
            break;
          }
          ansans++;
          temp = temp % __builtin_popcountll(temp);
        }
      } else {
        ll temp = totadd;
        temp = (temp + preadd[i]) % (cnt + 1);
        ll ansans = 1;
        while (1) {
          if (temp == 0) {
            write(ansans, endl);
            break;
          }
          ansans++;
          temp = temp % __builtin_popcountll(temp);
        }
      }
    }
  }
}


Error: 0

Fixed submission:

#if 0
    clear
    g++ -std=c++17 -O2 -Wall -lm "$0" && ./a.out
    exit
#endif
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#define endl '\\n'
#define pb push_back
#define fir first
#define sec second
#define ins insert
#define umap map
#define unordered_map map
#define mset multiset
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
const int INF = 0x3f3f3f3f;
const int MAXN = 2e5 + 10;
const int MOD = 1e9 + 7;
const long long INFLL = 0x3f3f3f3f3f3f3f3f;
long long TESTCASE_NUM = 1;
clock_t CLOCK_START_TIME = clock();
template <typename T> inline void _read(T &x) { cin >> x; }
inline void _read(int &x) { scanf("%d", &x); }
inline void _read(long long &x) { scanf("%lld", &x); }
inline void _read(float &x) { scanf("%f", &x); }
inline void _read(double &x) { scanf("%lf", &x); }
inline void _read(char &x) { x = getchar(); }
inline void _read(char *x) { scanf("%s", x); }
inline void read() {}
template <typename T, typename... U> inline void read(T &head, U &...tail) {
  _read(head);
  read(tail...);
}
template <typename T> inline void _write(const T &x) { cout << x; }
inline void _write(const int &x) { printf("%d", x); }
inline void _write(const long long &x) { printf("%lld", x); }
inline void _write(const float &x) { printf("%.6f", x); }
inline void _write(const double &x) { printf("%.8lf", x); }
inline void _write(const char &x) { putchar(x); }
inline void _write(const char *x) { printf("%s", x); }
inline void write() {}
template <typename T, typename... U>
inline void write(const T &head, const U &...tail) {
  _write(head);
  write(tail...);
}
inline void debug() {}
#ifdef _USE_DEBUG
template <typename T, typename... U>
inline void debug(const T &head, const U &...tail) {
  _write("\\033[1m\\033[33m\\33[4m");
  write(head, tail...);
  _write("\\033[0m");
  // system("sleep 1");
}
#else
template <typename T, typename... U>
inline void debug(const T &head, const U &...tail) {}
#endif
void solve();
template <typename T1, typename T2> inline T1 max(T1 a, T2 b) {
  return a < b ? b : a;
}
template <typename T1, typename T2> inline T1 min(T1 a, T2 b) {
  return a < b ? a : b;
}
template <typename T> inline bool isprime(T p) {
  if (p == 2 || p == 3)
    return 1;
  if (p < 2 || (p % 6 != 1 && p % 6 != 5))
    return 0;
  for (T i = 5; i * i <= p; i = i + 6)
    if (p % i == 0 || p % (i + 2) == 0)
      return 0;
  return 1;
}

// Unordered_map can have O(n) complexity in some cases, use map instead.
// To NOT replace unordered_map with map, use the following lines.
#if 0
#define _USE_UNORDERED_MAP
#undef unordered_map
#undef umap
#define umap unordered_map
#endif

// Using long long globally can have undesirable side effects.
// To replace int with long long, use the following lines.
#if 0
#define _USE_LONG_LONG
#define int long long
#define pii pair<long long, long long>
#define vi vector<long long>
#define INF 0x3f3f3f3f3f3f3f3f
#endif

void DISPLAY_DEBUG_INFO() {
#ifdef _USE_DEBUG
  printf("\\n\\033[1m\\033[32m=> \\033[1m\\033[33m%ld\\033[1m\\033[32m ms used. ",
         clock() - CLOCK_START_TIME);
#endif
#if (defined _USE_LONG_LONG) && (defined _USE_DEBUG)
  printf("\\033[1m\\033[32mAll \\033[1m\\033[33mint\\033[1m\\033[32m has been "
         "replaced by \\033[1m\\033[33mlong long\\033[1m\\033[32m.\\033[0m ");
#endif
#if (!defined _USE_UNORDERED_MAP) && (defined _USE_DEBUG)
  printf("\\033[1m\\033[32mAll \\033[1m\\033[33munordered_map\\033[1m\\033[32m has "
         "been replaced by \\033[1m\\033[33mmap\\033[1m\\033[32m.\\033[0m ");
#endif
#ifdef _USE_DEBUG
  printf("\\033[0m\\n\\n");
#endif
}

signed main() {
#ifdef _USE_FILEIO
  freopen("in1.txt", "r", stdin);
  // freopen("l.txt", "p[3]", stdout);
#endif
  // scanf("%lld\\n", &TESTCASE_NUM);
  while (TESTCASE_NUM--)
    solve();
  DISPLAY_DEBUG_INFO();
  return 0;
}

ll preadd[MAXN];
ll premin[MAXN];

void solve() {
  ll n;
  read(n);
  string a;
  read(a);
  ll cnt = 0;
  ll baseadd = 1;
  ll basemin = 1;
  ll totadd = 0;
  ll totmin = 0;
  for (int i = 0; i < a.size(); i++) {
    if (a[i] == '1') {
      cnt++;
    }
  }
  // debug(cnt, endl);
  if (cnt == 0) {
    for (int i = 0; i < a.size(); i++) {
      write(0, endl);
    }
  }
  if (cnt == 1) {
    for (int i = 0; i < a.size(); i++) {
      preadd[a.size() - i - 1] = baseadd;
      // premin[a.size() - i - 1] = basemin;
      baseadd = baseadd * 2 % (cnt + 1);
      // basemin = basemin * 2 % (cnt - 1);
      if (a[a.size() - i - 1] == '1') {
        totadd += preadd[a.size() - i - 1];
        // totmin += premin[i];
      }
    }
    for (int i = 0; i < a.size(); i++) {
      if (a[i] == '1') {
        write(1, endl);

      } else {
        ll temp = totadd;
        temp = (temp + (cnt + 1) + preadd[i]) % (cnt + 1);
        ll ansans = 1;
        while (1) {
          ansans++;
          temp = temp % __builtin_popcountll(temp);
          if (temp == 0) {
            write(ansans, endl);
            break;
          }
        }
      }
    }
  } else {
    for (int i = 0; i < a.size(); i++) {
      // debug(basemin, ' ');
      // debug(cnt, endl);
      preadd[a.size() - i - 1] = baseadd;
      premin[a.size() - i - 1] = basemin;
      baseadd = baseadd * 2 % (cnt + 1);
      basemin = basemin * 2 % (cnt - 1);

      if (a[a.size() - i - 1] == '1') {
        totadd += preadd[a.size() - i - 1];
        totmin += premin[a.size() - i - 1];
        // debug(totmin, ' ');
      }
    }
    for (int i = 0; i < a.size(); i++) {
      if (a[i] == '1') {
        // if (cnt == 2)
        // {
        //     write(1, endl);
        //     continue;
        // }
        // debug(totmin, endl);
        ll temp = totmin;
        temp = (temp + (cnt - 1) - premin[i]) % (cnt - 1);
        ll ansans = 1;
        while (1) {
          if (temp == 0) {
            write(ansans, endl);
            break;
          }
          ansans++;
          temp = temp % __builtin_popcountll(temp);
        }
      } else {
        ll temp = totadd;
        temp = (temp + (cnt + 1) + preadd[i]) % (cnt + 1);
        ll ansans = 1;
        while (1) {
          if (temp == 0) {
            write(ansans, endl);
            break;
          }
          ansans++;
          temp = temp % __builtin_popcountll(temp);
        }
      }
    }
  }
}