Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define gep(i, g, j) for (int i = g.head[j]; i != -1; i = g.e[i].next)
#define each(it, c)                                                            \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define rng(a) a.begin(), a.end()
#define maxs(x, y) x = max(x, y)
#define mins(x, y) x = min(x, y)
#define pb push_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcount
#define uni(x) x.erase(unique(rng(x)), x.end())
#define snuke srand((unsigned)clock() + (unsigned)time(NULL));
#define df(x) int x = in()
#define dame                                                                   \
  {                                                                            \
    puts("-1");                                                                \
    return 0;                                                                  \
  }
#define show(x) cout << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, vector<T>, greater<T>>
#define bn(x) ((1 << x) - 1)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
inline int in() {
  int x;
  scanf("%d", &x);
  return x;
}
inline void priv(vi a) {
  rep(i, sz(a)) printf("%d%c", a[i], i == sz(a) - 1 ? '\n' : ' ');
}
template <typename T> istream &operator>>(istream &i, vector<T> &v) {
  rep(j, sz(v)) i >> v[j];
  return i;
}
template <typename T> string join(vector<T> &v) {
  stringstream s;
  rep(i, sz(v)) s << ' ' << v[i];
  return s.str().substr(1);
}
template <typename T> ostream &operator<<(ostream &o, vector<T> &v) {
  if (sz(v))
    o << join(v);
  return o;
}
template <typename T1, typename T2>
istream &operator>>(istream &i, pair<T1, T2> &v) {
  return i >> v.fi >> v.se;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &o, pair<T1, T2> &v) {
  return o << v.fi << "," << v.se;
}
const int MX = 2005, INF = 1001001001;
const ll LINF = 1e18;
const double eps = 1e-10;

// Mod int
const int mod = 1000000007;
struct mint {
  ll x;
  mint() : x(0) {}
  mint(ll x) : x((x % mod + mod) % mod) {}
  // mint(ll x):x(x){}
  mint &fix() {
    x = (x % mod + mod) % mod;
    return *this;
  }
  mint &operator+=(const mint &a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint &a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint &a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  bool operator<(const mint &a) const { return x < a.x; }
  bool operator==(const mint &a) const { return x == a.x; }
};
istream &operator>>(istream &i, mint &a) {
  i >> a.x;
  return i;
}
ostream &operator<<(ostream &o, const mint &a) {
  o << a.x;
  return o;
}
typedef vector<mint> vm;
//

mint dp[MX][MX];
mint s[MX];

int main() {
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);
  int d = (n + m - 1) / (k - 1);
  dp[0][0] = 1;
  rep(i, d) {
    rep(j, m + 1) s[j] = dp[i][j];
    rep(j, m) s[j + 1] += s[j];
    rep(j, m + 1) {
      dp[i + 1][j] = s[j];
      if (j - k >= 0)
        dp[i + 1][j] -= s[j - k];
      // rep(l,k) {
      //   int nj = l+j;
      //   if (nj > m) continue;
      //   dp[i+1][nj] += dp[i][l];
      // }
    }
  }
  mint ans;
  for (int i = m, j = d; i >= 1; i -= k - 1, j--) {
    ans += dp[j][i];
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define gep(i, g, j) for (int i = g.head[j]; i != -1; i = g.e[i].next)
#define each(it, c)                                                            \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define rng(a) a.begin(), a.end()
#define maxs(x, y) x = max(x, y)
#define mins(x, y) x = min(x, y)
#define pb push_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcount
#define uni(x) x.erase(unique(rng(x)), x.end())
#define snuke srand((unsigned)clock() + (unsigned)time(NULL));
#define df(x) int x = in()
#define dame                                                                   \
  {                                                                            \
    puts("-1");                                                                \
    return 0;                                                                  \
  }
#define show(x) cout << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, vector<T>, greater<T>>
#define bn(x) ((1 << x) - 1)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
inline int in() {
  int x;
  scanf("%d", &x);
  return x;
}
inline void priv(vi a) {
  rep(i, sz(a)) printf("%d%c", a[i], i == sz(a) - 1 ? '\n' : ' ');
}
template <typename T> istream &operator>>(istream &i, vector<T> &v) {
  rep(j, sz(v)) i >> v[j];
  return i;
}
template <typename T> string join(vector<T> &v) {
  stringstream s;
  rep(i, sz(v)) s << ' ' << v[i];
  return s.str().substr(1);
}
template <typename T> ostream &operator<<(ostream &o, vector<T> &v) {
  if (sz(v))
    o << join(v);
  return o;
}
template <typename T1, typename T2>
istream &operator>>(istream &i, pair<T1, T2> &v) {
  return i >> v.fi >> v.se;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &o, pair<T1, T2> &v) {
  return o << v.fi << "," << v.se;
}
const int MX = 4005, INF = 1001001001;
const ll LINF = 1e18;
const double eps = 1e-10;

// Mod int
const int mod = 1000000007;
struct mint {
  ll x;
  mint() : x(0) {}
  mint(ll x) : x((x % mod + mod) % mod) {}
  // mint(ll x):x(x){}
  mint &fix() {
    x = (x % mod + mod) % mod;
    return *this;
  }
  mint &operator+=(const mint &a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint &a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint &a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  bool operator<(const mint &a) const { return x < a.x; }
  bool operator==(const mint &a) const { return x == a.x; }
};
istream &operator>>(istream &i, mint &a) {
  i >> a.x;
  return i;
}
ostream &operator<<(ostream &o, const mint &a) {
  o << a.x;
  return o;
}
typedef vector<mint> vm;
//

mint dp[MX][MX];
mint s[MX];

int main() {
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);
  int d = (n + m - 1) / (k - 1);
  dp[0][0] = 1;
  rep(i, d) {
    rep(j, m + 1) s[j] = dp[i][j];
    rep(j, m) s[j + 1] += s[j];
    rep(j, m + 1) {
      dp[i + 1][j] = s[j];
      if (j - k >= 0)
        dp[i + 1][j] -= s[j - k];
      // rep(l,k) {
      //   int nj = l+j;
      //   if (nj > m) continue;
      //   dp[i+1][nj] += dp[i][l];
      // }
    }
  }
  mint ans;
  for (int i = m, j = d; i >= 1; i -= k - 1, j--) {
    ans += dp[j][i];
  }
  cout << ans << endl;
  return 0;
}