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


#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <float.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
// #include<assert.h>
#include <random>
using namespace std;
#define int long long

#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807LL
#define I64_MAX2 1223372036854775807LL
#define INF I64_MAX2
// #define MOD1 1000000007
// #define MOD1 998244353
#define MEM_SIZE 10000
#define DEBUG_OUT true
#define ALL(x) (x).begin(), (x).end()

template <typename T> void DEBUG(T e) {
  if (DEBUG_OUT == false)
    return;
  std::cout << e << " ";
}
template <typename T> void DEBUG(const std::vector<T> &v) {
  if (DEBUG_OUT == false)
    return;
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void DEBUG(const std::vector<std::vector<T>> &vv) {
  if (DEBUG_OUT == false)
    return;
  for (const auto &v : vv) {
    DEBUG(v);
  }
}
template <class T, class... Ts> void DEBUG(T d, Ts... e) {
  if (DEBUG_OUT == false)
    return;
  DEBUG(d);
  DEBUG(e...);
}
template <class T> void corner(bool flg, T hoge) {
  if (flg) {
    cout << hoge << endl;
    abort();
  }
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

// BELOW
int MOD1;
struct mint1 {
  int x; // typedef long long int;
  mint1(int x = 0) : x((x % MOD1 + MOD1) % MOD1) {}
  mint1 &operator+=(const mint1 a) {
    if ((x += a.x) >= MOD1)
      x -= MOD1;
    return *this;
  }
  mint1 &operator-=(const mint1 a) {
    if ((x += MOD1 - a.x) >= MOD1)
      x -= MOD1;
    return *this;
  }
  mint1 &operator*=(const mint1 a) {
    (x *= a.x) %= MOD1;
    return *this;
  }
  mint1 operator+(const mint1 a) const {
    mint1 res(*this);
    return res += a;
  }
  mint1 operator-(const mint1 a) const {
    mint1 res(*this);
    return res -= a;
  }
  mint1 operator*(const mint1 a) const {
    mint1 res(*this);
    return res *= a;
  }
  mint1 pow(int t) const {
    if (!t)
      return 1;
    mint1 a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime MOD1
  mint1 inv() const { return pow(MOD1 - 2); }
  mint1 &operator/=(const mint1 a) { return (*this) *= a.inv(); }
  mint1 operator/(const mint1 a) const {
    mint1 res(*this);
    return res /= a;
  }
};

int MOD2;
struct mint2 {
  int x; // typedef long long int;
  mint2(int x = 0) : x((x % MOD2 + MOD2) % MOD2) {}
  mint2 &operator+=(const mint2 a) {
    if ((x += a.x) >= MOD2)
      x -= MOD2;
    return *this;
  }
  mint2 &operator-=(const mint2 a) {
    if ((x += MOD2 - a.x) >= MOD2)
      x -= MOD2;
    return *this;
  }
  mint2 &operator*=(const mint2 a) {
    (x *= a.x) %= MOD2;
    return *this;
  }
  mint2 operator+(const mint2 a) const {
    mint2 res(*this);
    return res += a;
  }
  mint2 operator-(const mint2 a) const {
    mint2 res(*this);
    return res -= a;
  }
  mint2 operator*(const mint2 a) const {
    mint2 res(*this);
    return res *= a;
  }
  mint2 pow(int t) const {
    if (!t)
      return 1;
    mint2 a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime MOD2
  mint2 inv() const { return pow(MOD2 - 2); }
  mint2 &operator/=(const mint2 a) { return (*this) *= a.inv(); }
  mint2 operator/(const mint2 a) const {
    mint2 res(*this);
    return res /= a;
  }
};

void solve(void) {
  int N;
  cin >> N;

  string str;
  cin >> str;
  reverse(ALL(str));
  int cnt = 0;
  int M = 200010;
  vector<int> table(M, -1);
  table[0] = 0;
  for (int i = 1; i < M; i++) {
    int p = __builtin_popcount(i);
    table[i] = table[i % p] + 1;
  }

  for (int i = 0; i < N; i++) {
    if (str[i] == '1')
      cnt++;
  }

  MOD1 = cnt - 1;
  MOD2 = cnt + 1;
  if (MOD1 == 0)
    MOD1 = 1e9 + 7;
  mint1 Q1 = 0;
  mint2 Q2 = 0;
  for (int i = 0; i < N; i++) {
    mint1 X1 = 2;
    mint2 X2 = 2;

    if (str[i] == '1') {
      X1 = X1.pow(i);
      X2 = X2.pow(i);
      Q1 += X1;
      Q2 += X2;
    }
  }

  for (int i = N - 1; i >= 0; i--) {
    if (str[i] == '0') {
      mint2 X = 2;
      X = X.pow(i);
      mint2 Z = Q2 + X;
      cout << table[Z.x] + 1 << endl;
    } else {
      if (MOD1 == 1e9 + 7) {
        cout << 0 << endl;
        continue;
      }
      mint1 X = 2;
      X = X.pow(i);
      mint1 Z = Q1 - X;
      cout << table[Z.x] + 1 << endl;
    }
  }

  return;
}

int32_t main(int32_t argc, const char *argv[]) {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  std::cout << std::fixed;
  std::cout << std::setprecision(11);
  solve();

  return 0;
}


Error: 0

Fixed submission:


#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <float.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
// #include<assert.h>
#include <random>
using namespace std;
#define int long long

#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807LL
#define I64_MAX2 1223372036854775807LL
#define INF I64_MAX2
// #define MOD1 1000000007
// #define MOD1 998244353
#define MEM_SIZE 10000
#define DEBUG_OUT true
#define ALL(x) (x).begin(), (x).end()

template <typename T> void DEBUG(T e) {
  if (DEBUG_OUT == false)
    return;
  std::cout << e << " ";
}
template <typename T> void DEBUG(const std::vector<T> &v) {
  if (DEBUG_OUT == false)
    return;
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void DEBUG(const std::vector<std::vector<T>> &vv) {
  if (DEBUG_OUT == false)
    return;
  for (const auto &v : vv) {
    DEBUG(v);
  }
}
template <class T, class... Ts> void DEBUG(T d, Ts... e) {
  if (DEBUG_OUT == false)
    return;
  DEBUG(d);
  DEBUG(e...);
}
template <class T> void corner(bool flg, T hoge) {
  if (flg) {
    cout << hoge << endl;
    abort();
  }
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

// BELOW
int MOD1;
struct mint1 {
  int x; // typedef long long int;
  mint1(int x = 0) : x((x % MOD1 + MOD1) % MOD1) {}
  mint1 &operator+=(const mint1 a) {
    if ((x += a.x) >= MOD1)
      x -= MOD1;
    return *this;
  }
  mint1 &operator-=(const mint1 a) {
    if ((x += MOD1 - a.x) >= MOD1)
      x -= MOD1;
    return *this;
  }
  mint1 &operator*=(const mint1 a) {
    (x *= a.x) %= MOD1;
    return *this;
  }
  mint1 operator+(const mint1 a) const {
    mint1 res(*this);
    return res += a;
  }
  mint1 operator-(const mint1 a) const {
    mint1 res(*this);
    return res -= a;
  }
  mint1 operator*(const mint1 a) const {
    mint1 res(*this);
    return res *= a;
  }
  mint1 pow(int t) const {
    if (!t)
      return 1;
    mint1 a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime MOD1
  mint1 inv() const { return pow(MOD1 - 2); }
  mint1 &operator/=(const mint1 a) { return (*this) *= a.inv(); }
  mint1 operator/(const mint1 a) const {
    mint1 res(*this);
    return res /= a;
  }
};

int MOD2;
struct mint2 {
  int x; // typedef long long int;
  mint2(int x = 0) : x((x % MOD2 + MOD2) % MOD2) {}
  mint2 &operator+=(const mint2 a) {
    if ((x += a.x) >= MOD2)
      x -= MOD2;
    return *this;
  }
  mint2 &operator-=(const mint2 a) {
    if ((x += MOD2 - a.x) >= MOD2)
      x -= MOD2;
    return *this;
  }
  mint2 &operator*=(const mint2 a) {
    (x *= a.x) %= MOD2;
    return *this;
  }
  mint2 operator+(const mint2 a) const {
    mint2 res(*this);
    return res += a;
  }
  mint2 operator-(const mint2 a) const {
    mint2 res(*this);
    return res -= a;
  }
  mint2 operator*(const mint2 a) const {
    mint2 res(*this);
    return res *= a;
  }
  mint2 pow(int t) const {
    if (!t)
      return 1;
    mint2 a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime MOD2
  mint2 inv() const { return pow(MOD2 - 2); }
  mint2 &operator/=(const mint2 a) { return (*this) *= a.inv(); }
  mint2 operator/(const mint2 a) const {
    mint2 res(*this);
    return res /= a;
  }
};

void solve(void) {
  int N;
  cin >> N;

  string str;
  cin >> str;
  reverse(ALL(str));
  int cnt = 0;
  int M = 200010;
  vector<int> table(M, -1);
  table[0] = 0;
  for (int i = 1; i < M; i++) {
    int p = __builtin_popcount(i);
    table[i] = table[i % p] + 1;
  }

  for (int i = 0; i < N; i++) {
    if (str[i] == '1')
      cnt++;
  }
  MOD1 = cnt - 1;
  MOD2 = cnt + 1;
  mint1 Q1 = 0;
  mint2 Q2 = 0;
  for (int i = 0; i < N; i++) {
    mint1 X1 = 2;
    mint2 X2 = 2;

    if (str[i] == '1') {
      X1 = X1.pow(i);
      X2 = X2.pow(i);
      Q1 += X1;
      Q2 += X2;
    }
  }

  for (int i = N - 1; i >= 0; i--) {
    if (str[i] == '0') {
      mint2 X = 2;
      X = X.pow(i);
      mint2 Z = Q2 + X;
      cout << table[Z.x] + 1 << endl;
    } else {

      mint1 X = 2;
      X = X.pow(i);
      mint1 Z = Q1 - X;
      cout << table[Z.x] + 1 << endl;
    }
  }

  return;
}

int32_t main(int32_t argc, const char *argv[]) {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  std::cout << std::fixed;
  std::cout << std::setprecision(11);
  solve();

  return 0;
}