Problem p00460 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define min_3(a, b, c) min(a, min(b, c))
#define max_3(a, b, c) max(a, max(b, c))
#define mp1(a, b, c) P1(a, P(b, c))
#define pque(a) priority_queue<a>
#define rpque(a) priority_queue<a, vector<a>, greater<a>>

const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                         {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const int kaijou[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};

int main() {
  while (1) {
    int n, m, s;
    scanf("%d%d%d", &n, &m, &s);
    if (n == 0 && m == 0 && s == 0)
      break;

    static int dp[2002][3002] = {};
    rep(i, 2002) {
      rep(j, 3002) {
        if (j == 0)
          dp[i][j] = 1;
        else
          dp[i][j] = 0;
      }
    }

    int l = 0, r;

    rep1(i, n * n) {
      l += i;
      r = s;
      for (int j = i + 1; j <= n * n; j++) {
        r -= j;
      }
      for (int j = r; j >= l; j--) {
        rep1(k, min(m, j)) {
          dp[k][j] = dp[k - 1][j - k];
          if (dp[k][j] >= 100000)
            dp[k][j] %= 100000;
        }
        for (int k = min(m, j) + 1; k <= m; k++) {
          dp[k][j] = 0;
        }
        dp[0][j] = 0;
      }
      for (int j = l - i; j < l; j++) {
        rep1(k, m) { dp[k][j] = 0; }
        dp[0][j] = 0;
      }
      for (int j = l; j <= r; j++) {
        rep1(k, m) {
          dp[k][j] += dp[k - 1][j];
          if (dp[k][j] >= 100000)
            dp[k][j] %= 100000;
        }
      }
    }

    printf("%d\n", dp[m][s]);
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define min_3(a, b, c) min(a, min(b, c))
#define max_3(a, b, c) max(a, max(b, c))
#define mp1(a, b, c) P1(a, P(b, c))
#define pque(a) priority_queue<a>
#define rpque(a) priority_queue<a, vector<a>, greater<a>>

const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                         {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const int kaijou[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};

int main() {
  while (1) {
    int n, m, s;
    scanf("%d%d%d", &n, &m, &s);
    if (n == 0 && m == 0 && s == 0)
      break;

    static int dp[2002][3002] = {};
    rep(i, 2002) {
      rep(j, 3002) {
        if (j == 0)
          dp[i][j] = 1;
        else
          dp[i][j] = 0;
      }
    }

    int l = 0, r;

    rep1(i, n * n) {
      l += i;
      r = s;
      for (int j = i + 1; j <= n * n; j++) {
        r -= j;
      }
      for (int j = r; j >= l; j--) {
        rep1(k, min(m, j)) {
          dp[k][j] = dp[k - 1][j - k];
          if (dp[k][j] >= 100000)
            dp[k][j] %= 100000;
        }
        for (int k = min(m, j) + 1; k <= m; k++) {
          dp[k][j] = 0;
        }
        dp[0][j] = 0;
      }
      for (int j = l - i; j < l; j++) {
        rep1(k, m) { dp[k][j] = 0; }
        dp[0][j] = 0;
      }
      for (int j = l; j <= r; j++) {
        for (int k = i; k <= m && k * (n * n - i) <= s; k++) {
          // rep1(k,m){
          dp[k][j] += dp[k - 1][j];
          if (dp[k][j] >= 100000)
            dp[k][j] %= 100000;
        }
      }
    }

    printf("%d\n", dp[m][s]);
  }
}