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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

const int SIGDIGIT = 10010;
struct LongFloat2 {
  ll integer;
  vector<char> fractional;
  LongFloat2(int a = 0) : integer(a), fractional(SIGDIGIT) {}
  char operator[](int index) const { return fractional[index]; }
  char &operator[](int index) { return fractional[index]; }

  LongFloat2 operator+(const LongFloat2 &rhs) const {
    LongFloat2 lhs = *this;
    int c = 0;
    for (int i = SIGDIGIT - 1; i >= 0; --i) {
      int s = lhs[i] + rhs[i] + c;
      lhs[i] = s % 10;
      c = s / 10;
    }
    lhs.integer += rhs.integer + c;
    return lhs;
  }
  LongFloat2 operator/(ll rhs) const {
    LongFloat2 lhs = *this;
    ll a = lhs.integer;
    lhs.integer = a / rhs;
    a = a % rhs * 10;
    REP(i, SIGDIGIT) {
      a += lhs[i];
      lhs[i] = a / rhs;
      a = a % rhs * 10;
    }
    return lhs;
  }
  string toString() const {
    ostringstream os;
    os << integer << ".";
    FOR(it, fractional) os << (int)*it;
    return os.str();
  }
  LongFloat2 round(int r) const {
    LongFloat2 ret;
    ret[r] = 5;
    ret = *this + ret;
    return ret;
  }
};

LongFloat2 mydiv(ll a, ll b) {
  ll d = 0;
  LongFloat2 ret(a / b);
  a = a % b * 10;
  REP(i, SIGDIGIT) {
    ret[i] = a / b;
    a = a % b * 10;
  }
  return ret;
}

ostream &operator<<(ostream &os, const LongFloat2 &rhs) {
  os << rhs.toString();
  return os;
}

int main() {
  int n, k, m, r;
  while (cin >> n >> k >> m >> r, n || k || m || r) {
    LongFloat2 ans = mydiv(1, n);
    if (m) {
      LongFloat2 sum;
      for (int i = 1; i < n; ++i) {
        sum = sum + mydiv(1, i);
      }
      ans = ans + sum / n;
    }
    ans.round(r);
    string a = ans.toString();
    cout << a.substr(0, r + 2) << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

const int SIGDIGIT = 10010;
struct LongFloat2 {
  ll integer;
  vector<char> fractional;
  LongFloat2(int a = 0) : integer(a), fractional(SIGDIGIT) {}
  char operator[](int index) const { return fractional[index]; }
  char &operator[](int index) { return fractional[index]; }

  LongFloat2 operator+(const LongFloat2 &rhs) const {
    LongFloat2 lhs = *this;
    int c = 0;
    for (int i = SIGDIGIT - 1; i >= 0; --i) {
      int s = lhs[i] + rhs[i] + c;
      lhs[i] = s % 10;
      c = s / 10;
    }
    lhs.integer += rhs.integer + c;
    return lhs;
  }
  LongFloat2 operator/(ll rhs) const {
    LongFloat2 lhs = *this;
    ll a = lhs.integer;
    lhs.integer = a / rhs;
    a = a % rhs * 10;
    REP(i, SIGDIGIT) {
      a += lhs[i];
      lhs[i] = a / rhs;
      a = a % rhs * 10;
    }
    return lhs;
  }
  string toString() const {
    ostringstream os;
    os << integer << ".";
    FOR(it, fractional) os << (int)*it;
    return os.str();
  }
  LongFloat2 round(int r) const {
    LongFloat2 ret;
    ret[r] = 5;
    ret = *this + ret;
    return ret;
  }
};

LongFloat2 mydiv(ll a, ll b) {
  ll d = 0;
  LongFloat2 ret(a / b);
  a = a % b * 10;
  REP(i, SIGDIGIT) {
    ret[i] = a / b;
    a = a % b * 10;
  }
  return ret;
}

ostream &operator<<(ostream &os, const LongFloat2 &rhs) {
  os << rhs.toString();
  return os;
}

LongFloat2 hoge[10001];
int main() {
  for (int i = 1; i < 10001; ++i) {
    hoge[i] = LongFloat2(1) / i;
  }
  int n, k, m, r;
  while (cin >> n >> k >> m >> r, n || k || m || r) {
    LongFloat2 ans = hoge[n];
    if (m) {
      LongFloat2 sum;
      for (int i = 1; i < n; ++i) {
        sum = sum + hoge[i];
      }
      ans = ans + sum / n;
    }
    ans.round(r);
    string a = ans.toString();
    cout << a.substr(0, r + 2) << endl;
  }
}