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

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

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 100000;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int main() {
  int N, M, S;
  while (cin >> N >> M >> S && N) {
    N *= N;
    static int dp[2][3001][2001] = {};
    dp[0][0][0] = 1;
    for (int i = 0; i < N; i++) {
      for (int sum = 1; sum <= S; sum++) {
        for (int last = 1; last <= min(M, sum); last++) {
          dp[(i + 1) & 1][sum][last] = (dp[(i + 1) & 1][sum - 1][last - 1] +
                                        dp[i & 1][sum - last][last - 1]) %
                                       MOD;
        }
      }
      memset(dp[i & 1], 0, sizeof(dp[i & 1]));
    }
    int ans = 0;
    for (int last = 1; last <= M; last++)
      ans = (ans + dp[N & 1][S][last]) % MOD;
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

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

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 100000;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int N, M, S;
typedef pair<int, int> P;
int memo[50][2001][3001];
int dfs(int k, int last, int sum) {
  // printf("dfs k = %d last = %d sum = %d\\n", k, last, sum);
  int &res = memo[k][last][sum];
  if (res != -1)
    return res;
  if (k == N)
    return res = 1;
  res = 0;
  int n = N - k;
  int first = max(S - ((n - 1) * (2 * M - (n - 2)) / 2 + sum), last + 1);
  for (int a = first; a <= M - n + 1; a++) {
    if (sum + n * (2 * a + n - 1) / 2 > S)
      break;
    res += dfs(k + 1, a, sum + a);
    res %= MOD;
  }
  return res;
}

int main() {
  while (cin >> N >> M >> S && N) {
    N *= N;
    memset(memo, -1, sizeof(memo));
    cout << dfs(0, 0, 0) << endl;
  }
  return 0;
}