Problem p03825 (C++)

Buggy submission (status: Runtime Error):
/* --- author: dxm --- */
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;

#define INF 1e9
#define Inf 1e18
#define rep(i, n) for (int(i) = 0; (i) < n; i++)
#define REP(i, n) for (int(i) = 1; (i) <= n; i++)
#define mp make_pair
#define pb push_back
#define fst first
#define snd second
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

void qread(int &x) {
  int neg = 1;
  x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      neg = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = 10 * x + c - '0', c = getchar();
  x *= neg;
}

const int mod = 1e9 + 7;
int Add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }

const int maxn = 2005;
int n, m, k, ans;
int dp[maxn][maxn], cur[maxn];

int main() {
#ifdef FILE
  freopen("FILE.in", "r", stdin);
  freopen("FILE.out", "w", stdout);
#endif
  scanf("%d%d%d", &n, &m, &k);
  int len = (n + m - 1) / (k - 1);
  dp[0][0] = 1;
  rep(i, len) rep(j, m + 1) {
    int cur = dp[i][j];
    if (!cur)
      continue;
    rep(p, k) if (j + p <= m) dp[i + 1][j + p] = Add(dp[i + 1][j + p], cur);
  }
  rep(i, len) {
    rep(j, m + 1) cur[j] = 0;
    rep(j, m + 1) REP(p, k - 1) if (j + p <= m) cur[j + p] =
        Add(cur[j + p], dp[i][j]);
    rep(j, m + 1) {
      int b = k * (i + 1) - j - i;
      if (j % (k - 1) != m % (k - 1) || b > n || b % (k - 1) != n % (k - 1))
        continue;
      ans = Add(ans, cur[j]);
    }
  }
  printf("%d\n", ans);
#ifdef TIME
  printf("Running Time = %d ms\n", int(clock() * 1000.0 / CLOCKS_PER_SEC));
#endif
  return 0;
}

/*
Input:
-----------------
Output:
*/


Fixed submission:
/* --- author: dxm --- */
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;

#define INF 1e9
#define Inf 1e18
#define rep(i, n) for (int(i) = 0; (i) < n; i++)
#define REP(i, n) for (int(i) = 1; (i) <= n; i++)
#define mp make_pair
#define pb push_back
#define fst first
#define snd second
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

void qread(int &x) {
  int neg = 1;
  x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      neg = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = 10 * x + c - '0', c = getchar();
  x *= neg;
}

const int mod = 1e9 + 7;
int Add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }

const int maxn = 4005;
int n, m, k, ans;
int dp[maxn][maxn], cur[maxn];

int main() {
#ifdef FILE
  freopen("FILE.in", "r", stdin);
  freopen("FILE.out", "w", stdout);
#endif
  scanf("%d%d%d", &n, &m, &k);
  int len = (n + m - 1) / (k - 1);
  dp[0][0] = 1;
  rep(i, len) rep(j, m + 1) {
    int cur = dp[i][j];
    if (!cur)
      continue;
    rep(p, k) if (j + p <= m) dp[i + 1][j + p] = Add(dp[i + 1][j + p], cur);
  }
  rep(i, len) {
    rep(j, m + 1) cur[j] = 0;
    rep(j, m + 1) REP(p, k - 1) if (j + p <= m) cur[j + p] =
        Add(cur[j + p], dp[i][j]);
    rep(j, m + 1) {
      int b = k * (i + 1) - j - i;
      if (j % (k - 1) != m % (k - 1) || b > n || b % (k - 1) != n % (k - 1))
        continue;
      ans = Add(ans, cur[j]);
    }
  }
  printf("%d\n", ans);
#ifdef TIME
  printf("Running Time = %d ms\n", int(clock() * 1000.0 / CLOCKS_PER_SEC));
#endif
  return 0;
}

/*
Input:
-----------------
Output:
*/