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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int n, m, k;
double sum[100100];
double cntd[100100];

void solve(int num, int cnt) {
  if (cnt == n) {
    cntd[num]++;
    return;
  }
  FOR(i, 1, m + 1) solve(num + i, cnt + 1);
}

int main() {
  while (cin >> n >> m >> k && n + m + k) {
    fill(sum, sum + 100100, 0);
    fill(cntd, cntd + 100100, 0);
    double t = pow(m, n);
    ll lb = n, ub = n * m;
    double num = 1;
    solve(0, 0);
    while (1) {
      // cout << lb << " " << ub << " ";
      // cout << num << endl;
      if (lb == ub) {
        if (lb > k)
          sum[lb] += cntd[lb];
        else
          sum[k + 1] += cntd[lb];
        break;
      } else {
        if (lb > ub)
          break;
        if (lb > k)
          sum[lb] += cntd[lb];
        else
          sum[k + 1] += cntd[lb];
        if (ub > k)
          sum[ub] += cntd[lb];
        else
          sum[k + 1] += cntd[lb];
      }
      lb++;
      ub--;
    }
    double ans = 0;
    REP(i, n * m + 1) {
      double num = i;
      // cout << i << " " << sum[i] << " " << sum[i] / t << endl;
      ans += (sum[i] / t) * (num - k);
    }
    printf("%.10lf\\n", ans);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int n, m, k;
double sum[100000010];
double cntd[100100];

void solve(int num, int cnt) {
  if (cnt == n) {
    cntd[num]++;
    return;
  }
  FOR(i, 1, m + 1) solve(num + i, cnt + 1);
}

int main() {
  while (cin >> n >> m >> k && n + m + k) {
    fill(sum, sum + 100000010, 0);
    fill(cntd, cntd + 100100, 0);
    double t = pow(m, n);
    ll lb = n, ub = n * m;
    double num = 1;
    solve(0, 0);
    while (1) {
      // cout << lb << " " << ub << " ";
      // cout << num << endl;
      if (lb == ub) {
        if (lb > k)
          sum[lb] += cntd[lb];
        else
          sum[k + 1] += cntd[lb];
        break;
      } else {
        if (lb > ub)
          break;
        if (lb > k)
          sum[lb] += cntd[lb];
        else
          sum[k + 1] += cntd[lb];
        if (ub > k)
          sum[ub] += cntd[lb];
        else
          sum[k + 1] += cntd[lb];
      }
      lb++;
      ub--;
    }
    double ans = 0;
    REP(i, n * m + 1) {
      double num = i;
      // cout << i << " " << sum[i] << " " << sum[i] / t << endl;
      ans += (sum[i] / t) * (num - k);
    }
    cout << ans << endl;
  }
}