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

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <cstdint>  // int64_t, int*_t
#include <cstdio>   // printf
#include <deque>    // deque
#include <iostream> // cout, endl, cin
#include <limits>
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define ll long long
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define int_INF 2147483647
#define pint_INF 2000000000
#define ll_INF 9223372036854775807
#define MOD 1000000007
#define vi vector<int>
#define vvi vector<vector<int>>
#define vvll vector<vector<long long>>
#define vvc vector<vector<char>>
#define vll vector<long long>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define sysp system("PAUSE")
#define pno cout << "no" << endl
#define pyes cout << "yes" << endl
#define pYes cout << "Yes" << endl
#define pNo cout << "No" << endl
using namespace std;

int ctoi(char c) { return c - '0'; }

ll two_pow(ll b, ll base) {
  ll ans = 1;
  rep(i, b) {
    ans *= 2;
    ans %= base;
  }

  return ans;
}

const int M = 200005;
int N;
class Pop {
private:
  vector<int> SmallerMaxN;
  bitset<20> bs;
  vector<ll> smallerbox;
  vector<ll> powmin;
  vector<ll> powmax;

public:
  Pop() {
    SmallerMaxN.resize(M);
    SmallerMaxN[1] = 1;
    smallerbox.resize(N);
    for (int i = 1; i < M; i++) {
      SmallerMaxN[i] = calc(i);
    }
  }

  int calc(int x) {
    if (x == 0)
      return 1;
    if (SmallerMaxN[x] != 0) {
      return SmallerMaxN[x];
    } else {
      bs = x;
      int y = x % bs.count();

      if (SmallerMaxN[y] != 0) {
        SmallerMaxN[x] = SmallerMaxN[y] + 1;
        return SmallerMaxN[x];
      } else if (y == 0) {
        return 1;
      } else {
        SmallerMaxN[y] = calc(y) + 1;
        return -1;
      }
    }
  }

  void solve(string s) {
    ll basemin, basemax;
    ll N_bmin = 0, N_bmax = 0;
    ll base = 0;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '1')
        base++;
    }
    basemin = base - 1;
    basemax = base + 1;

    powmin.resize(N), powmax.resize(N);

    powmin[0] = 1, powmax[0] = 1;
    for (int i = 1; basemin != 0 && i < s.size(); i++) {
      powmin[i] = powmin[i - 1] * 2;
      powmin[i] %= basemin;

      powmax[i] = powmax[i - 1] * 2;
      powmax[i] %= basemax;
    }

    for (int i = 0; i < s.size(); i++) {
      N_bmin += ctoi(s[i]) * powmin[s.size() - 1 - i];
      N_bmax += ctoi(s[i]) * powmax[s.size() - 1 - i];
    }
    N_bmax %= basemax;
    if (basemin != 0) {
      N_bmin %= basemin;
    }

    ll temp;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '0') {
        temp = N_bmax;
        temp += powmax[s.size() - i - 1];
        temp %= basemax;
        smallerbox[i] = temp;
      } else {
        temp = N_bmin;
        temp -= powmin[s.size() - 1 - i];
        if (temp < 0)
          temp += basemin;
        if (basemin == 0) {
          smallerbox[i] = -1;
        } else {
          smallerbox[i] = temp;
        }
      }
    }

    for (int i = 0; i < s.size(); i++) {
      if (smallerbox[i] != -1) {
        cout << SmallerMaxN[smallerbox[i]] + 1 << endl;
      } else {
        cout << 0 << endl;
      }
    }
  }
};

signed main() {
  cin >> N;
  string s;
  cin >> s;

  Pop pp;
  pp.solve(s);
}

Error: 0

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <cstdint>  // int64_t, int*_t
#include <cstdio>   // printf
#include <deque>    // deque
#include <iostream> // cout, endl, cin
#include <limits>
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define ll long long
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define int_INF 2147483647
#define pint_INF 2000000000
#define ll_INF 9223372036854775807
#define MOD 1000000007
#define vi vector<int>
#define vvi vector<vector<int>>
#define vvll vector<vector<long long>>
#define vvc vector<vector<char>>
#define vll vector<long long>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define sysp system("PAUSE")
#define pno cout << "no" << endl
#define pyes cout << "yes" << endl
#define pYes cout << "Yes" << endl
#define pNo cout << "No" << endl
using namespace std;

int ctoi(char c) { return c - '0'; }

ll two_pow(ll b, ll base) {
  ll ans = 1;
  rep(i, b) {
    ans *= 2;
    ans %= base;
  }

  return ans;
}

const int M = 200005;
int N;
class Pop {
private:
  vector<int> SmallerMaxN;
  bitset<20> bs;
  vector<ll> smallerbox;

public:
  Pop() {
    SmallerMaxN.resize(M);
    SmallerMaxN[1] = 1;
    smallerbox.resize(N);
    for (int i = 1; i < M; i++) {
      SmallerMaxN[i] = calc(i);
    }
  }

  int calc(int x) {
    if (x == 0)
      return 1;
    if (SmallerMaxN[x] != 0) {
      return SmallerMaxN[x];
    } else {
      bs = x;
      int y = x % bs.count();

      if (SmallerMaxN[y] != 0) {
        SmallerMaxN[x] = SmallerMaxN[y] + 1;
        return SmallerMaxN[x];
      } else if (y == 0) {
        return 1;
      } else {
        SmallerMaxN[y] = calc(y) + 1;
        return -1;
      }
    }
  }

  void solve(string s) {
    ll basemin, basemax;
    ll N_bmin = 0, N_bmax = 0;
    ll base = 0;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '1')
        base++;
    }
    basemin = base - 1;
    basemax = base + 1;
    for (int i = 0; i < s.size(); i++) {
      N_bmin += ctoi(s[i]) * two_pow(s.size() - 1 - i, basemin);
      N_bmax += ctoi(s[i]) * two_pow(s.size() - 1 - i, basemax);
    }
    N_bmax %= basemax;
    N_bmin %= basemin;

    ll temp;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '0') {
        temp = N_bmax;
        temp += two_pow(s.size() - 1 - i, basemax);
        temp %= basemax;
        smallerbox[i] = temp;
      } else {
        temp = N_bmin;
        temp -= two_pow(s.size() - 1 - i, basemin);
        if (temp < 0)
          temp += basemin;
        smallerbox[i] = temp;
      }
    }

    for (int i = 0; i < s.size(); i++) {
      cout << SmallerMaxN[smallerbox[i]] + 1 << endl;
    }
  }
};

signed main() {
  cin >> N;
  string s;
  cin >> s;
  Pop pp;
  pp.solve(s);
}