Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fast_IO ios::sync_with_stdio(false);
#define DEBUG                                                                  \
  fprintf(stderr, "Running on Line %d in Function %s\n", __LINE__, __FUNCTION__)
// mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define fir first
#define sec second
#define mod 1000000007
#define ll long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
inline int read() {
  char ch = getchar();
  int nega = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      nega = -1;
    ch = getchar();
  }
  int ans = 0;
  while (isdigit(ch)) {
    ans = ans * 10 + ch - 48;
    ch = getchar();
  }
  if (nega == -1)
    return -ans;
  return ans;
}
typedef pair<int, int> pii;
inline int add(int x, int y) { return x + y >= mod ? x + y - mod : x + y; }
inline int add(int x, int y, int z) { return add(add(x, y), z); }
inline int sub(int x, int y) { return x - y < 0 ? x - y + mod : x - y; }
inline int mul(int x, int y) { return 1LL * x * y % mod; }
inline int mul(int x, int y, int z) { return mul(mul(x, y), z); }
#define N 2005
int n, m, k;
int f[N][N][2];
signed main() {
  cin >> n >> m >> k;
  if (n < m)
    swap(n, m);
  f[0][0][0] = 1;
  int lim = (n + m - 1) / (k - 1);
  for (int i = 1; i <= lim; i++) {
    for (int j = 0; j <= n; j++) {
      for (int c = 1; c <= k - 1; c++) {
        if (j >= c)
          f[i][j][1] = add(f[i - 1][j - c][0], f[i - 1][j - c][1], f[i][j][1]);
      }
      f[i][j][0] = add(f[i - 1][j][0], f[i - 1][j][1], f[i][j][0]);
    }
  }
  int ans = 0;
  for (int i = 1; i <= lim; i++) {
    for (int j = n % (k - 1); j <= n; j += k - 1) {
      if (i * (k - 1) - j + 1 <= m)
        ans = add(ans, f[i][j][1]);
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fast_IO ios::sync_with_stdio(false);
#define DEBUG                                                                  \
  fprintf(stderr, "Running on Line %d in Function %s\n", __LINE__, __FUNCTION__)
// mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define fir first
#define sec second
#define mod 1000000007
#define ll long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
inline int read() {
  char ch = getchar();
  int nega = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      nega = -1;
    ch = getchar();
  }
  int ans = 0;
  while (isdigit(ch)) {
    ans = ans * 10 + ch - 48;
    ch = getchar();
  }
  if (nega == -1)
    return -ans;
  return ans;
}
typedef pair<int, int> pii;
inline int add(int x, int y) { return x + y >= mod ? x + y - mod : x + y; }
inline int add(int x, int y, int z) { return add(add(x, y), z); }
inline int sub(int x, int y) { return x - y < 0 ? x - y + mod : x - y; }
inline int mul(int x, int y) { return 1LL * x * y % mod; }
inline int mul(int x, int y, int z) { return mul(mul(x, y), z); }
#define N 4005
int n, m, k;
int f[N][N][2];
signed main() {
  cin >> n >> m >> k;
  if (n < m)
    swap(n, m);
  f[0][0][0] = 1;
  int lim = (n + m - 1) / (k - 1);
  for (int i = 1; i <= lim; i++) {
    for (int j = 0; j <= n; j++) {
      for (int c = 1; c <= k - 1; c++) {
        if (j >= c)
          f[i][j][1] = add(f[i - 1][j - c][0], f[i - 1][j - c][1], f[i][j][1]);
      }
      f[i][j][0] = add(f[i - 1][j][0], f[i - 1][j][1], f[i][j][0]);
    }
  }
  int ans = 0;
  for (int i = 1; i <= lim; i++) {
    for (int j = n % (k - 1); j <= n; j += k - 1) {
      if (i * (k - 1) - j + 1 <= m)
        ans = add(ans, f[i][j][1]);
    }
  }
  cout << ans << endl;
  return 0;
}