Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define PR pair
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define REP(i, x, y) for (int i = (int)(x); i <= (int)(y); i++)
#define FOR(i, x, y) for (int i = (int)(x); i < (int)(y); i++)
#define PER(i, x, y) for (int i = (int)(x); i >= (int)(y); i--)
#define CH ch = getchar()
#define Exit(...) printf(__VA_ARGS__), exit(0)
#define dln() fprintf(stderr, "\n")
#define dprintf(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
typedef double db;
typedef long long LL;
typedef vector<int> VI;
typedef vector<VI> VII;
typedef PR<int, int> PII;
typedef vector<PII> VPI;
const int inf = 2e9;
const LL Inf = 1e10;
const int P = 1e9 + 7;
const int N = 2005;

inline LL IN() {
  LL x = 0;
  int ch = 0, f = 0;
  for (CH; ch != -1 && (ch < 48 || ch > 57); CH)
    f = (ch == '-');
  for (; ch >= 48 && ch <= 57; CH)
    x = (x << 1) + (x << 3) + ch - '0';
  return f ? (-x) : x;
}
template <typename T> inline int chkmin(T &a, const T &b) {
  if (b < a)
    return a = b, 1;
  return 0;
}
template <typename T> inline int chkmax(T &a, const T &b) {
  if (b > a)
    return a = b, 1;
  return 0;
}

void renew(int &x, const int &y) {
  x += y;
  if (x >= P)
    x -= P;
  if (x < 0)
    x += P;
}

int Pow(int x, int y, int p) {
  int a = 1;
  for (; y; y >>= 1, x = (LL)x * x % p)
    if (y & 1)
      a = (LL)a * x % p;
  return a;
}

int n, m, K, F[N][N], maxd, ans;

int main() {
  scanf("%d%d%d", &n, &m, &K);
  maxd = (n + m) / (K - 1) + 1;
  REP(i, 1, K - 1) F[1][i] = 1;
  REP(i, 1, maxd)
  REP(j, 0, n) REP(k, 0, K - 1) if (j + k <= n) renew(F[i + 1][j + k], F[i][j]);
  REP(i, 1, maxd) REP(j, 0, n) if (F[i][j] && j % (K - 1) == n % (K - 1)) {
    int k = i * (K - 1) + 1 - j;
    if (0 <= k && k <= m && k % (K - 1) == m % (K - 1))
      renew(ans, F[i][j]);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define PR pair
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define REP(i, x, y) for (int i = (int)(x); i <= (int)(y); i++)
#define FOR(i, x, y) for (int i = (int)(x); i < (int)(y); i++)
#define PER(i, x, y) for (int i = (int)(x); i >= (int)(y); i--)
#define CH ch = getchar()
#define Exit(...) printf(__VA_ARGS__), exit(0)
#define dln() fprintf(stderr, "\n")
#define dprintf(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
typedef double db;
typedef long long LL;
typedef vector<int> VI;
typedef vector<VI> VII;
typedef PR<int, int> PII;
typedef vector<PII> VPI;
const int inf = 2e9;
const LL Inf = 1e10;
const int P = 1e9 + 7;
const int N = 2005;

inline LL IN() {
  LL x = 0;
  int ch = 0, f = 0;
  for (CH; ch != -1 && (ch < 48 || ch > 57); CH)
    f = (ch == '-');
  for (; ch >= 48 && ch <= 57; CH)
    x = (x << 1) + (x << 3) + ch - '0';
  return f ? (-x) : x;
}
template <typename T> inline int chkmin(T &a, const T &b) {
  if (b < a)
    return a = b, 1;
  return 0;
}
template <typename T> inline int chkmax(T &a, const T &b) {
  if (b > a)
    return a = b, 1;
  return 0;
}

void renew(int &x, const int &y) {
  x += y;
  if (x >= P)
    x -= P;
  if (x < 0)
    x += P;
}

int Pow(int x, int y, int p) {
  int a = 1;
  for (; y; y >>= 1, x = (LL)x * x % p)
    if (y & 1)
      a = (LL)a * x % p;
  return a;
}

int n, m, K, F[N << 1][N], maxd, ans;

int main() {
  scanf("%d%d%d", &n, &m, &K);
  maxd = (n + m) / (K - 1) + 1;
  REP(i, 1, K - 1) F[1][i] = 1;
  REP(i, 1, maxd)
  REP(j, 0, n) REP(k, 0, K - 1) if (j + k <= n) renew(F[i + 1][j + k], F[i][j]);
  REP(i, 1, maxd) REP(j, 0, n) if (F[i][j] && j % (K - 1) == n % (K - 1)) {
    int k = i * (K - 1) + 1 - j;
    if (0 <= k && k <= m && k % (K - 1) == m % (K - 1))
      renew(ans, F[i][j]);
  }
  cout << ans << endl;
  return 0;
}