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

#include <bits/stdc++.h>
using namespace std;
int mod = 100000;
int N, M, S;
int dp[2001][3001];
int dpsum[2001][3001];
// int dp1[2009][3009];
int main() {
  while (true) {
    scanf("%d%d%d", &N, &M, &S);
    if (N == 0 && M == 0 && S == 0)
      break;
    for (int i = 0; i <= M; i++) {
      for (int j = 0; j <= S; j++) {
        dp[i][j] = 0;
        dpsum[i][j] = 0;
      }
    }
    dp[0][0] = 1;
    for (int i = 1; i <= N * N; i++) {
      for (int j = i - 1; j <= M; j++) {
        int xx = S;
        for (int k = i + 1; k <= N * N; k++) {
          xx -= (j + k - i - 1);
        }
        for (int k = i * (i - 1) / 2; k <= xx; k++) {
          if (j != 0) {
            dpsum[j][k] = dpsum[j - 1][k] + dp[j][k];
            if (dpsum[j][k] >= mod)
              dpsum[j][k] -= mod;
          } else {
            dpsum[j][k] = dp[j][k];
          }
          /*
          if(j+k+1<=S){
                  dp1[j+1][j+k+1]=dpsum[j][k];
          }*/
          // dp1[l][k+l]
          /*
          if(dp[j][k]==0)continue;
          for(int l=j+1;l<=M;l++){

                  if(k+l<=S){
                          dp1[l][k+l]+=dp[j][k];
                          if(dp1[l][k+l]>=mod)dp1[l][k+l]-=mod;
                  }
          }*/
        }
      }
      for (int j = 0; j <= M; j++) {
        for (int k = j; k <= S; k++) {
          dp[j][k] = 0;
        }
      }
      for (int j = 0; j <= M; j++) {
        for (int k = 0; k <= S - j - 1; k++) {
          dp[j + 1][j + k + 1] = dpsum[j][k];
          dpsum[j][k] = 0;
        }
      }
      /*
      for(int j=0;j<=M;j++){
              for(int k=0;k<=S;k++){
                      if(dp[j][k]!=0)printf("%d %d %d:%d\\n",i,j,k,dp[j][k]);
              }
      }*/
    }
    int ans = 0;
    for (int j = 1; j <= M; j++) {
      // printf("")
      ans += dp[j][S];
      ans %= mod;
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int mod = 100000;
int N, M, S;
int dp[2001][3001];
int dpsum[2001][3001];
// int dp1[2009][3009];
int main() {
  while (true) {
    scanf("%d%d%d", &N, &M, &S);
    if (N == 0 && M == 0 && S == 0)
      break;
    dp[0][0] = 1;
    for (int i = 1; i <= N * N; i++) {
      for (int j = 0; j <= M; j++) {
        for (int k = i * (i - 1) / 2; k <= S; k++) {
          if (j != 0) {
            dpsum[j][k] = dpsum[j - 1][k] + dp[j][k];
            if (dpsum[j][k] >= mod)
              dpsum[j][k] -= mod;
          } else {
            dpsum[j][k] = dp[j][k];
          }
          /*
          if(j+k+1<=S){
                  dp1[j+1][j+k+1]=dpsum[j][k];
          }*/
          // dp1[l][k+l]
          /*
          if(dp[j][k]==0)continue;
          for(int l=j+1;l<=M;l++){

                  if(k+l<=S){
                          dp1[l][k+l]+=dp[j][k];
                          if(dp1[l][k+l]>=mod)dp1[l][k+l]-=mod;
                  }
          }*/
        }
      }
      for (int j = 0; j <= M; j++) {
        for (int k = j; k <= S; k++) {
          dp[j][k] = 0;
        }
      }
      for (int j = 0; j <= M; j++) {
        for (int k = 0; k <= S - j - 1; k++) {
          dp[j + 1][j + k + 1] = dpsum[j][k];
          dpsum[j][k] = 0;
        }
      }
      /*
      for(int j=0;j<=M;j++){
              for(int k=0;k<=S;k++){
                      if(dp[j][k]!=0)printf("%d %d %d:%d\\n",i,j,k,dp[j][k]);
              }
      }*/
    }
    int ans = 0;
    for (int j = 1; j <= M; j++) {
      // printf("")
      ans += dp[j][S];
      ans %= mod;
    }
    printf("%d\\n", ans);
  }
  return 0;
}