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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second
#define DBG(X) cout << " " << #X << " : " << X << endl

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;

const int INF = 1e9;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const ll LINF = 1e18;

struct Input {
  int n;
  char c;
  char str[10010];
  string s;
  ll l;
  double d;
  int nextInt() {
    scanf("%d", &n);
    return n;
  }
  ll nextLong() {
    scanf("%lld", &l);
    return l;
  }
  double nextDouble() {
    scanf("%lf", &d);
    return d;
  }
  char nextChar() {
    scanf("%c", &c);
    return c;
  }
  string next() {
    scanf("%s", str);
    return string(str);
  }
  string nextLine() {
    getline(cin, s);
    return s;
  }
};
Input in;

int dp[30][100010];

ll mod_pow(ll x, int n, int mod) {
  if (n == 0)
    return 1LL;
  int ret = mod_pow(x * x % mod, n / 2, mod);
  if (n & 1)
    ret *= x;
  return ret % mod;
}

int main() {
  while (1) {
    int n = in.nextInt(), m = in.nextInt(), K = in.nextInt();
    if (n == 0 && m == 0 && K == 0)
      break;
    for (int i = 0; i < 1e5 + 10; i++)
      for (int j = 0; j < 30; j++)
        dp[j][i] = 0;
    dp[0][0] = 1;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= m * n; j++) {
        for (int k = 1; k <= m; k++) {
          dp[i][j + k] += dp[i - 1][j];
        }
      }
    }
    double d = mod_pow(m, n, mod);
    // cout<<" "<<d<<endl;
    double ans = 0.0;
    for (int i = 0; i <= 1e5; i++)
      ans += max(1.0, (double)i - K) * dp[n][i] / d;

    printf("%.15lf\\n", ans);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second
#define DBG(X) cout << " " << #X << " : " << X << endl

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;

const int INF = 1e9;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const ll LINF = 1e18;

struct Input {
  int n;
  char c;
  char str[10010];
  string s;
  ll l;
  double d;
  int nextInt() {
    scanf("%d", &n);
    return n;
  }
  ll nextLong() {
    scanf("%lld", &l);
    return l;
  }
  double nextDouble() {
    scanf("%lf", &d);
    return d;
  }
  char nextChar() {
    scanf("%c", &c);
    return c;
  }
  string next() {
    scanf("%s", str);
    return string(str);
  }
  string nextLine() {
    getline(cin, s);
    return s;
  }
};
Input in;

int dp[30][1000010];

ll mod_pow(ll x, int n, int mod) {
  if (n == 0)
    return 1LL;
  int ret = mod_pow(x * x % mod, n / 2, mod);
  if (n & 1)
    ret *= x;
  return ret % mod;
}

int main() {
  while (1) {
    int n = in.nextInt(), m = in.nextInt(), K = in.nextInt();
    if (n == 0 && m == 0 && K == 0)
      break;
    for (int i = 0; i < 1e6 + 10; i++)
      for (int j = 0; j < 30; j++)
        dp[j][i] = 0;
    dp[0][0] = 1;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= m * n; j++) {
        for (int k = 1; k <= m; k++) {
          dp[i][j + k] += dp[i - 1][j];
        }
      }
    }
    double d = mod_pow(m, n, mod);
    // cout<<" "<<d<<endl;
    double ans = 0.0;
    for (int i = 0; i <= 1e6; i++)
      ans += max(1.0, (double)i - K) * dp[n][i] / d;

    printf("%.15lf\\n", ans);
  }
}