Problem p02257 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cassert>
#include <cmath>
#include <iostream>

using namespace std;

class mybitset {
  unsigned int *mem;
  int size;
  bool ismemfree;

public:
  mybitset() { ismemfree = true; }
  void setsize(int size) {
    this->size = size;
    mem = new unsigned int[size / sizeof(unsigned int) + 1];
    ismemfree = false;
  }
  ~mybitset() {
    if (!ismemfree) {
      delete[] mem;
    }
  }
  bool get(int pos) {
    char bit = 1 << (pos % sizeof(unsigned int));
    return (mem[pos / sizeof(unsigned int)] & bit) != 0;
  };
  void set(int pos) {
    char bit = 1 << (pos % sizeof(unsigned int));
    mem[pos / sizeof(unsigned int)] |= bit;
  };
  void reset(int pos) {
    char bit = ~(1 << (pos % sizeof(unsigned int)));
    mem[pos / sizeof(unsigned int)] &= bit;
  }
};

class primer {
  mybitset isprime;
  int max;

public:
  bool getPrime(int number);
  primer(int max);
};

bool primer::getPrime(int number) {
  assert(max * max > number);
  if (max > number) {
    return isprime.get(number);
  }
  for (int i = 0; i < max; i++) {
    if (isprime.get(i)) {
      if (number % i == 0) {
        return false;
      }
    }
  }
  return true;
}
primer::primer(int max) {
  this->max = max;
  isprime.setsize(max + 1);
  //??¨??????????????????????????§?´???°?????????????????????
  isprime.reset(0);
  isprime.reset(1);
  for (int i = 2; i < max; i++) {
    isprime.set(i);
  }
  for (int i = 2; i < sqrt(max); i++) {
    if (isprime.get(i)) {
      for (int j = 2; i * j < max; j++) {
        isprime.reset(i * j);
      }
    }
  }
}

int main(int argc, char const *argv[]) {
  int count;
  cin >> count;
  int prime_count = 0;
  primer p = primer(11000);
  for (int i = 0; i < count; i++) {
    int number;
    cin >> number;
    if (p.getPrime(number)) {
      prime_count++;
    }
  }
  cout << prime_count << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <cmath>
#include <iostream>

using namespace std;

class mybitset {
  char *mem;
  int size;
  bool ismemfree;

public:
  mybitset() { ismemfree = true; }
  void setsize(int size) {
    this->size = size;
    mem = new char[size / sizeof(char) + 1];
    ismemfree = false;
  }
  ~mybitset() {
    if (!ismemfree) {
      delete[] mem;
    }
  }
  bool get(int pos) {
    char bit = 1 << (pos % sizeof(char));
    return (mem[pos / sizeof(char)] & bit) != 0;
  };
  void set(int pos) {
    char bit = 1 << (pos % sizeof(char));
    mem[pos / sizeof(char)] |= bit;
  };
  void reset(int pos) {
    char bit = ~(1 << (pos % sizeof(char)));
    mem[pos / sizeof(char)] &= bit;
  }
};

class primer {
  mybitset isprime;

public:
  bool getPrime(int number);
  primer(int max);
};

bool primer::getPrime(int number) { return isprime.get(number); }
primer::primer(int max) {
  isprime.setsize(max + 1);
  //??¨??????????????????????????§?´???°?????????????????????
  isprime.reset(0);
  isprime.reset(1);
  for (int i = 2; i < max; i++) {
    isprime.set(i);
  }
  for (int i = 2; i < sqrt(max); i++) {
    if (isprime.get(i)) {
      for (int j = 2; i * j < max; j++) {
        isprime.reset(i * j);
      }
    }
  }
}

int main(int argc, char const *argv[]) {
  int count;
  cin >> count;
  int prime_count = 0;
  primer p = primer(100000000);
  for (int i = 0; i < count; i++) {
    int number;
    cin >> number;
    if (p.getPrime(number)) {
      prime_count++;
    }
  }
  cout << prime_count << endl;
  return 0;
}