Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define TR(i, x) for (__typeof(x.begin()) i = x.begin(); i != x.end(); i++)
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define CLEAR(x) memset(x, 0, sizeof(x))
#define FILL(x, c) memset(x, c, sizeof(x))

using namespace std;

#define PB push_back
#define MP make_pair

typedef map<int, int> MII;
typedef map<string, int> MSI;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<long double> VD;
typedef pair<int, int> PII;
typedef long long int64;
typedef long long LL;
typedef unsigned int UI;
typedef long double LD;
typedef unsigned long long ULL;

const int MOD = 1000000007;

const int N = 2222;

int dp[N][N][2];

void solve(int caseId) {
  int n, m, k;
  cin >> n >> m >> k;

  int level = (n + m - 1) / (k - 1);

  int answer = 0;
  CLEAR(dp);
  dp[0][m][0] = 1;
  for (int l = 0; l < level; ++l) {
    for (int rest = 0; rest <= m; ++rest) {
      for (int empty = 0; empty < 2; ++empty) {
        for (int delta = 0; delta <= k && delta <= rest; ++delta) {
          if (delta == rest) {
            bool realEmpty = empty || n == 0 || (delta < k);
            if (realEmpty) {
              answer = (answer + dp[l][rest][empty]) % MOD;
            }
            continue;
          }
          if (delta < k) {
            if (delta < k - 1) {
              dp[l + 1][rest - delta][1] =
                  (dp[l + 1][rest - delta][1] + dp[l][rest][empty]) % MOD;
            } else {
              dp[l + 1][rest - delta][empty] =
                  (dp[l + 1][rest - delta][empty] + dp[l][rest][empty]) % MOD;
            }
          }
        }
      }
    }
  }

  cout << answer << endl;
}

int main() {
  int cases = 1;
  for (int caseNumber = 1; caseNumber <= cases; ++caseNumber) {
    solve(caseNumber);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define TR(i, x) for (__typeof(x.begin()) i = x.begin(); i != x.end(); i++)
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define CLEAR(x) memset(x, 0, sizeof(x))
#define FILL(x, c) memset(x, c, sizeof(x))

using namespace std;

#define PB push_back
#define MP make_pair

typedef map<int, int> MII;
typedef map<string, int> MSI;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<long double> VD;
typedef pair<int, int> PII;
typedef long long int64;
typedef long long LL;
typedef unsigned int UI;
typedef long double LD;
typedef unsigned long long ULL;

const int MOD = 1000000007;

const int N = 4444;

int dp[N][N][2];

void solve(int caseId) {
  int n, m, k;
  cin >> n >> m >> k;

  int level = (n + m - 1) / (k - 1);

  int answer = 0;
  CLEAR(dp);
  dp[0][m][0] = 1;
  for (int l = 0; l < level; ++l) {
    for (int rest = 0; rest <= m; ++rest) {
      for (int empty = 0; empty < 2; ++empty) {
        for (int delta = 0; delta <= k && delta <= rest; ++delta) {
          if (delta == rest) {
            bool realEmpty = empty || n == 0 || (delta < k);
            if (realEmpty) {
              answer = (answer + dp[l][rest][empty]) % MOD;
            }
            continue;
          }
          if (delta < k) {
            if (delta < k - 1) {
              dp[l + 1][rest - delta][1] =
                  (dp[l + 1][rest - delta][1] + dp[l][rest][empty]) % MOD;
            } else {
              dp[l + 1][rest - delta][empty] =
                  (dp[l + 1][rest - delta][empty] + dp[l][rest][empty]) % MOD;
            }
          }
        }
      }
    }
  }

  cout << answer << endl;
}

int main() {
  int cases = 1;
  for (int caseNumber = 1; caseNumber <= cases; ++caseNumber) {
    solve(caseNumber);
  }
  return 0;
}