prithivMLmods/Coder-Stat huggingface dataset:
Problem p03825 (C++)

Buggy submission (status: Runtime Error):
//============================================================================
// Author       : Sun YaoFeng
//============================================================================

// #pragma 	comment(linker, "/STACK:100240000,100240000")
// #include	<cstdio>
// #include	<cstdlib>
// #include	<cstring>
// #include	<algorithm>

#include <bits/stdc++.h>

using namespace std;

#define DB double
#define lf else if
#define I64 long long
#define Rd() (rand() << 15 | rand())
#define For(i, a, b) for (int i = a, lim = b; i <= lim; i++)
#define Rep(i, a, b) for (int i = a, lim = b; i >= lim; i--)

#define fi first
#define se second
#define MK make_pair
#define PA pair<int, int>

// #define	min(a,b)	((a)<(b)?(a):(b))
// #define	max(a,b)	((a)<(b)?(b):(a))

#define CH (ch = getchar())
int IN() {
  int x = 0, f = 0, ch;
  for (; CH < '0' || ch > '9';)
    f = (ch == '-');
  for (; ch >= '0' && ch <= '9'; CH)
    x = x * 10 + ch - '0';
  return f ? -x : x;
}

#define n 2005
#define P 1000000007

int N, M, K, D, F[n][n];

inline void Plu(int &a, int b) {
  a += b;
  if (a >= P)
    a -= P;
}

int main(int argc, char *argv[]) {
  N = IN();
  M = IN();
  K = IN();
  D = (N + M) / (K - 1) + 1;

  For(i, 1, K - 1) F[1][i] = 1;
  For(i, 2, D) For(j, 0, K - 1) For(k, 0, N - j) Plu(F[i][j + k], F[i - 1][k]);

  int Ans = 0;
  For(i, 1, D) For(j, 0, N) {
    int k = i * (K - 1) + 1 - j;
    if (k <= M && j % (K - 1) == N % (K - 1) && k % (K - 1) == M % (K - 1))
      Plu(Ans, F[i][j]);
  }

  printf("%d\n", Ans);

  return 0;
}

Fixed submission:
//============================================================================
// Author       : Sun YaoFeng
//============================================================================

// #pragma 	comment(linker, "/STACK:100240000,100240000")
// #include	<cstdio>
// #include	<cstdlib>
// #include	<cstring>
// #include	<algorithm>

#include <bits/stdc++.h>

using namespace std;

#define DB double
#define lf else if
#define I64 long long
#define Rd() (rand() << 15 | rand())
#define For(i, a, b) for (int i = a, lim = b; i <= lim; i++)
#define Rep(i, a, b) for (int i = a, lim = b; i >= lim; i--)

#define fi first
#define se second
#define MK make_pair
#define PA pair<int, int>

// #define	min(a,b)	((a)<(b)?(a):(b))
// #define	max(a,b)	((a)<(b)?(b):(a))

#define CH (ch = getchar())
int IN() {
  int x = 0, f = 0, ch;
  for (; CH < '0' || ch > '9';)
    f = (ch == '-');
  for (; ch >= '0' && ch <= '9'; CH)
    x = x * 10 + ch - '0';
  return f ? -x : x;
}

#define n 4005
#define P 1000000007

int N, M, K, D, F[n][n];

inline void Plu(int &a, int b) {
  a += b;
  if (a >= P)
    a -= P;
}

int main(int argc, char *argv[]) {
  N = IN();
  M = IN();
  K = IN();
  D = (N + M) / (K - 1) + 1;

  For(i, 1, K - 1) F[1][i] = 1;
  For(i, 2, D) For(j, 0, K - 1) For(k, 0, N - j) Plu(F[i][j + k], F[i - 1][k]);

  int Ans = 0;
  For(i, 1, D) For(j, 0, N) {
    int k = i * (K - 1) + 1 - j;
    if (k <= M && j % (K - 1) == N % (K - 1) && k % (K - 1) == M % (K - 1))
      Plu(Ans, F[i][j]);
  }

  printf("%d\n", Ans);

  return 0;
}
Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 2005, P = 1e9 + 7;
int n, m, k, s, f[N][N], ans;

int main() {
  scanf("%d%d%d", &n, &m, &k);
  s = (n + m - 1) / (k - 1);
  f[0][0] = 1;
  for (int i = 1; i <= s; ++i) {
    for (int j = 0, sum = 0; j <= m; ++j) {
      sum = (sum + f[i - 1][j]) % P;
      if (i > 1) {
        if (j >= k)
          sum = (sum - f[i - 1][j - k] + P) % P;
      } else {
        if (j >= k - 1)
          sum = (sum - f[i - 1][j - k + 1]) % P;
      }
      f[i][j] = sum;
      //			if ((m - j) % (k - 1) == 0 && (n + j) % (k - 1)
      //== 0 && (i - 1) * (k - 1) + k - j <= n) ans = (ans + f[i][j] - f[i -
      //1][j]) % P;
      if (n + j - (k - 1) * i < 0 || j >= m) {
        f[i][j] = 0;
      }
      if ((m - j - 1) % (k - 1) == 0 && (n + j) % (k - 1) == 0)
        ans = (ans + f[i][j]) % P;
    }
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 4005, P = 1e9 + 7;
int n, m, k, s, f[N][N], ans;

int main() {
  scanf("%d%d%d", &n, &m, &k);
  s = (n + m - 1) / (k - 1);
  f[0][0] = 1;
  for (int i = 1; i <= s; ++i) {
    for (int j = 0, sum = 0; j <= m; ++j) {
      sum = (sum + f[i - 1][j]) % P;
      if (i > 1) {
        if (j >= k)
          sum = (sum - f[i - 1][j - k] + P) % P;
      } else {
        if (j >= k - 1)
          sum = (sum - f[i - 1][j - k + 1]) % P;
      }
      f[i][j] = sum;
      //			if ((m - j) % (k - 1) == 0 && (n + j) % (k - 1)
      //== 0 && (i - 1) * (k - 1) + k - j <= n) ans = (ans + f[i][j] - f[i -
      //1][j]) % P;
      if (n + j - (k - 1) * i < 0 || j >= m) {
        f[i][j] = 0;
      }
      if ((m - j - 1) % (k - 1) == 0 && (n + j) % (k - 1) == 0)
        ans = (ans + f[i][j]) % P;
    }
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03825 (C++)

Buggy submission (status: Runtime Error):
// Δ
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long LL;
typedef double DB;
const int N = 2222;
const int MO = 1e9 + 7;
int n, m, k, f[N][N];
int main() {
  int i, j, x;
  cin >> n >> m >> k;
  k--;
  n = (n + m - 1) / k;
  f[0][0] = 1;
  for (i = 1; i <= n; i = i + 1) {
    x = 0;
    for (j = 0; j <= m; j = j + 1) {
      x = (x + f[i - 1][j]) % MO;
      if (j > k)
        x = (x - f[i - 1][j - k - 1] + MO) % MO;
      f[i][j] = x;
    }
  }
  x = 0;
  while (n > 0 && m > 0) {
    x = (x + f[n][m]) % MO;
    n--, m -= k;
  }
  cout << x;
  return 0;
}

Fixed submission:
// Δ
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long LL;
typedef double DB;
const int N = 4444;
const int MO = 1e9 + 7;
int n, m, k, f[N][N];
int main() {
  int i, j, x;
  cin >> n >> m >> k;
  k--;
  n = (n + m - 1) / k;
  f[0][0] = 1;
  for (i = 1; i <= n; i = i + 1) {
    x = 0;
    for (j = 0; j <= m; j = j + 1) {
      x = (x + f[i - 1][j]) % MO;
      if (j > k)
        x = (x - f[i - 1][j - k - 1] + MO) % MO;
      f[i][j] = x;
    }
  }
  x = 0;
  while (n > 0 && m > 0) {
    x = (x + f[n][m]) % MO;
    n--, m -= k;
  }
  cout << x;
  return 0;
}
Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#define MOD 1000000007
int dp[2005][2005], pre[2005][2005];
int main() {
  // freopen("AGC009-E.in", "r", stdin);
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);
  dp[0][0] = 1;
  for (int i = 1; i < n + m; i++) {
    for (int j = 0; j <= m; j++) {
      pre[i][j] = ((j ? pre[i][j - 1] : 0) + dp[i - 1][j]) % MOD;
      dp[i][j] = (pre[i][j] - (j >= k ? pre[i][j - k] : 0) + MOD) % MOD;
    }
  }
  int ans = 0;
  while (m > 0) {
    (ans += dp[(n + m - 1) / (k - 1)][m]) %= MOD;
    m -= k - 1;
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <cstdio>
#define MOD 1000000007
int dp[4005][2005], pre[4005][2005];
int main() {
  // freopen("AGC009-E.in", "r", stdin);
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);
  dp[0][0] = 1;
  for (int i = 1; i < n + m; i++) {
    for (int j = 0; j <= m; j++) {
      pre[i][j] = ((j ? pre[i][j - 1] : 0) + dp[i - 1][j]) % MOD;
      dp[i][j] = (pre[i][j] - (j >= k ? pre[i][j - k] : 0) + MOD) % MOD;
    }
  }
  int ans = 0;
  while (m > 0) {
    (ans += dp[(n + m - 1) / (k - 1)][m]) %= MOD;
    m -= k - 1;
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

const int mod = 1000000007, N = 2005;

int n, m, k, len, dp[N][N], ans = 0;

void reduce(int &x) { x += x >> 31 & mod; }

int main() {
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  std::cin >> n >> m >> k, len = (n + m - 1) / (k - 1);
  dp[0][0] = 1;
  for (int i = 1; i <= len; ++i) {
    for (int j = 0; j <= m; ++j) {
      for (int l = 0; l < k && j + l <= m; ++l)
        reduce(dp[i][j + l] += dp[i - 1][j] - mod);
    }
    int sum = i * (k - 1) + 1;
    for (int j = m; j >= sum - n && j >= 0; j -= k - 1)
      if ((sum - j - n) % (k - 1) == 0)
        reduce(ans += dp[i][j] - mod), reduce(ans -= dp[i - 1][j]);
  }
  std::cout << ans << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

const int mod = 1000000007, N = 4005;

int n, m, k, len, dp[N][N], ans = 0;

void reduce(int &x) { x += x >> 31 & mod; }

int main() {
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  std::cin >> n >> m >> k, len = (n + m - 1) / (k - 1);
  dp[0][0] = 1;
  for (int i = 1; i <= len; ++i) {
    for (int j = 0; j <= m; ++j) {
      for (int l = 0; l < k && j + l <= m; ++l)
        reduce(dp[i][j + l] += dp[i - 1][j] - mod);
    }
    int sum = i * (k - 1) + 1;
    for (int j = m; j >= sum - n && j >= 0; j -= k - 1)
      if ((sum - j - n) % (k - 1) == 0)
        reduce(ans += dp[i][j] - mod), reduce(ans -= dp[i - 1][j]);
  }
  std::cout << ans << '\n';
  return 0;
}
Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
const int MAXN = 4005, MOD = 1000000007;

int N, M, K;
int dp[MAXN][MAXN], sum[MAXN][MAXN];

int main() {
  scanf("%d%d%d", &N, &M, &K);
  dp[0][0] = 1;
  for (int i = 1; i < N + M; i++)
    for (int j = 0; j <= M; j++) {
      sum[i][j] = ((j > 0 ? sum[i][j - 1] : 0) + dp[i - 1][j]) % MOD;
      dp[i][j] = (sum[i][j] - (j - K >= 0 ? sum[i][j - K] : 0) + MOD) % MOD;
    }
  int ans = 0;
  while (M) {
    ans = (ans + dp[(N + M - 1) / (K - 1)][M]) % MOD;
    M -= K - 1;
  }
  printf("%d\n", ans);

  return 0;
}


Fixed submission:
#include <cstdio>
const int MAXN = 4005, MOD = 1000000007;

int N, M, K;
int dp[MAXN][MAXN], sum[MAXN][MAXN];

int main() {
  scanf("%d%d%d", &N, &M, &K);
  dp[0][0] = 1;
  for (int i = 1; i < N + M; i++)
    for (int j = 0; j <= M; j++) {
      sum[i][j] = ((j > 0 ? sum[i][j - 1] : 0) + dp[i - 1][j]) % MOD;
      dp[i][j] = (sum[i][j] - (j - K >= 0 ? sum[i][j - K] : 0) + MOD) % MOD;
    }
  int ans = 0;
  while (M > 0) {
    ans = (ans + dp[(N + M - 1) / (K - 1)][M]) % MOD;
    M -= K - 1;
  }
  printf("%d\n", ans);

  return 0;
}
Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#define Max(_A, _B) (_A > _B ? _A : _B)
const int Mod = 1e9 + 7;
int N, M, K, f[2010][2010], s[2010][2010];
int main() {
  scanf("%d %d %d", &N, &M, &K);
  for (int i = 1; i < K; ++i)
    f[i][K - i - 1] = 1;
  for (int i = 0; i <= N; ++i)
    for (int j = 0; j < M; ++j) {
      if (j >= K - 1)
        (f[i][j] += s[i][j - K + 1]) %= Mod;
      else
        (f[i][j] += s[i + j - K + 1][0]) %= Mod;
      if (i >= K)
        (f[i][j] -= s[i - K][j + 1]) %= Mod;
      s[i][j] = (f[i][j] + (i ? s[i - 1][j + 1] : 0)) % Mod;
    }
  f[0][0] = 0;
  int Ans = 0;
  for (int i = N % (K - 1); i <= N; i += K - 1)
    for (int j = (M - 1) % (K - 1); j < M; j += K - 1)
      (Ans += f[i][j]) %= Mod;
  printf("%d\n", (Ans + Mod) % Mod);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#define Max(_A, _B) (_A > _B ? _A : _B)
const int Mod = 1e9 + 7;
int N, M, K, f[2010][2010], s[2010][2010];
int main() {
  scanf("%d %d %d", &N, &M, &K);
  for (int i = 1; i < K; ++i)
    f[i][K - i - 1] = 1;
  for (int i = 0; i <= N; ++i)
    for (int j = 0; j < M; ++j) {
      if (j >= K - 1)
        (f[i][j] += s[i][j - K + 1]) %= Mod;
      else if (i + j - K + 1 >= 0)
        (f[i][j] += s[i + j - K + 1][0]) %= Mod;
      if (i >= K)
        (f[i][j] -= s[i - K][j + 1]) %= Mod;
      s[i][j] = (f[i][j] + (i ? s[i - 1][j + 1] : 0)) % Mod;
    }
  f[0][0] = 0;
  int Ans = 0;
  for (int i = N % (K - 1); i <= N; i += K - 1)
    for (int j = (M - 1) % (K - 1); j < M; j += K - 1)
      (Ans += f[i][j]) %= Mod;
  printf("%d\n", (Ans + Mod) % Mod);
  return 0;
}
Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int q = 1000000007;
int n, m, t, f[2010][2010], p;
int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  int i, j, k;
  scanf("%d%d%d", &n, &m, &t);
  if (!n || !m)
    p = 1;
  f[0][0] = 1;
  for (i = 1; i * (t - 1) <= n + m - 1; i++)
    for (j = 0; j <= (i - 1) * (t - 1); j++)
      for (k = 0; k <= t - 1; k++)
        if (j + k <= n && i * (t - 1) + 1 - j - k <= m) {
          f[i][j + k] = (f[i][j + k] + f[i - 1][j]) % q;
          if (k && (n - j - k) % (t - 1) == 0)
            p = (p + f[i - 1][j]) % q;
        }
  printf("%d\n", p);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int q = 1000000007;
int n, m, t, f[4010][2010], p;
int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  int i, j, k;
  scanf("%d%d%d", &n, &m, &t);
  if (!n || !m)
    p = 1;
  f[0][0] = 1;
  for (i = 1; i * (t - 1) <= n + m - 1; i++)
    for (j = 0; j <= (i - 1) * (t - 1); j++)
      for (k = 0; k <= t - 1; k++)
        if (j + k <= n && i * (t - 1) + 1 - j - k <= m) {
          f[i][j + k] = (f[i][j + k] + f[i - 1][j]) % q;
          if (k && (n - j - k) % (t - 1) == 0)
            p = (p + f[i - 1][j]) % q;
        }
  printf("%d\n", p);
  return 0;
}
Problem p03825 (C++)

Buggy submission (status: Time Limit Exceeded):
// 修改自提交记录：227576
// 原记录作者：E.Space (UID: 1579)
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#define cmin(a, b) (a > (b) ? a = (b), 1 : 0)
#define cmax(a, b) (a < (b) ? a = (b), 1 : 0)
#define dmin(a, b) ((a) < (b) ? (a) : (b))
#define dmax(a, b) ((a) > (b) ? (a) : (b))
namespace io {
int F() {
  int n = 0, F = 1;
  char ch;
  while ((ch = getchar()) != '-' && (ch < '0' || ch > '9'))
    ;
  ch == '-' ? F = 0 : n = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    n = n * 10 + ch - '0';
  return F ? n : -n;
}
} // namespace io
const int M = 1000000007;
int f[2222][2222];
int main() {
  freopen("slime.in", "r", stdin);
  freopen("slime.ans", "w", stdout);
  int n = io::F(), m = io::F(), k = io::F();
  for (register int i = 1; i < k; ++i)
    if (k - i <= n && i <= m)
      f[k - i][i] = 1;
  for (int i = k; i < n + m; i += k - 1)
    for (int j = 1; j <= n; ++j)
      if (i - j > 0 && i - j <= m && f[j][i - j]) {
        register int F = f[j][i - j];
        for (register int l = 0; l < k; ++l)
          if (j + l <= n && i - j + k - 1 - l > 0 && i - j + k - 1 - l <= m)
            (f[j + l][i - j + k - 1 - l] += F) %= M;
      }
  int ans = 0;
  for (register int i = n; i >= 0; i -= k - 1)
    for (register int j = m; j >= 0; j -= k - 1)
      (ans += f[i][j]) %= M;
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
// 修改自提交记录：227576
// 原记录作者：E.Space (UID: 1579)
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#define cmin(a, b) (a > (b) ? a = (b), 1 : 0)
#define cmax(a, b) (a < (b) ? a = (b), 1 : 0)
#define dmin(a, b) ((a) < (b) ? (a) : (b))
#define dmax(a, b) ((a) > (b) ? (a) : (b))
namespace io {
int F() {
  int n = 0, F = 1;
  char ch;
  while ((ch = getchar()) != '-' && (ch < '0' || ch > '9'))
    ;
  ch == '-' ? F = 0 : n = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    n = n * 10 + ch - '0';
  return F ? n : -n;
}
} // namespace io
const int M = 1000000007;
int f[2222][2222];
int main() {
  // freopen("slime.in","r",stdin);
  // freopen("slime.ans","w",stdout);
  int n = io::F(), m = io::F(), k = io::F();
  for (register int i = 1; i < k; ++i)
    if (k - i <= n && i <= m)
      f[k - i][i] = 1;
  for (int i = k; i < n + m; i += k - 1)
    for (int j = 1; j <= n; ++j)
      if (i - j > 0 && i - j <= m && f[j][i - j]) {
        register int F = f[j][i - j];
        for (register int l = 0; l < k; ++l)
          if (j + l <= n && i - j + k - 1 - l > 0 && i - j + k - 1 - l <= m)
            (f[j + l][i - j + k - 1 - l] += F) %= M;
      }
  int ans = 0;
  for (register int i = n; i >= 0; i -= k - 1)
    for (register int j = m; j >= 0; j -= k - 1)
      (ans += f[i][j]) %= M;
  printf("%d\n", ans);
  return 0;
}
Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define N 2333
#define mod 1000000007

int n, m, k, f[N][N], ans;

void pls(int &x, int y) { x = (x + y) % mod; }

int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -f;
  for (; isdigit(ch); ch = getchar())
    x = x * 10 + ch - '0';
  return x * f;
}

int main() {
  n = read();
  m = read() - 1;
  k = read() - 1;
  f[0][0] = 1;
  for (int i = 0; i < (n + m) / k; i++)
    for (int j = 0; j < n; j++) {
      for (int l = 0; l <= k; l++)
        if (j + l < n && (i + 1) * k - j - l <= m)
          pls(f[i + 1][j + l], f[i][j]);
      pls(ans, f[i][j]);
    }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define N 2333
#define mod 1000000007

int n, m, k, f[2 * N][N], ans;

void pls(int &x, int y) { x = (x + y) % mod; }

int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -f;
  for (; isdigit(ch); ch = getchar())
    x = x * 10 + ch - '0';
  return x * f;
}

int main() {
  n = read();
  m = read() - 1;
  k = read() - 1;
  f[0][0] = 1;
  for (int i = 0; i < (n + m) / k; i++)
    for (int j = 0; j < n; j++) {
      for (int l = 0; l <= k; l++)
        if (j + l < n && (i + 1) * k - j - l <= m)
          pls(f[i + 1][j + l], f[i][j]);
      pls(ans, f[i][j]);
    }
  printf("%d\n", ans);
  return 0;
}
Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int maxn = 2010, P = 1000000007;
int n, m, K, ans, f[maxn][maxn][2], s[maxn];

int main() {
  scanf("%d %d %d", &n, &m, &K);
  f[0][0][0] = 1;
  for (int i = 1; i <= n + m; i++) {
    for (int j = 0; j <= m; j++) {
      s[j] = (1LL * (j ? s[j - 1] : 0) + f[i - 1][j][0] + f[i - 1][j][1]) % P;
    }
    for (int j = 0; j <= m; j++) {
      f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1]) % P;
      if (j)
        f[i][j][1] = (s[j - 1] - (j >= K ? s[j - K] : 0) + P) % P;
    }
    for (int j = 0; j <= m; j++) {
      int t = i * (K - 1) - j + 1;
      if (!((j - m) % (K - 1)) && t <= n && !((t - n) % (K - 1))) {
        (ans += f[i][j][1]) %= P;
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int maxn = 2010, P = 1000000007;
int n, m, K, ans, f[4010][maxn][2], s[maxn];

int main() {
  scanf("%d %d %d", &n, &m, &K);
  f[0][0][0] = 1;
  for (int i = 1; i <= n + m; i++) {
    for (int j = 0; j <= m; j++) {
      s[j] = (1LL * (j ? s[j - 1] : 0) + f[i - 1][j][0] + f[i - 1][j][1]) % P;
    }
    for (int j = 0; j <= m; j++) {
      f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1]) % P;
      if (j)
        f[i][j][1] = (s[j - 1] - (j >= K ? s[j - K] : 0) + P) % P;
    }
    for (int j = 0; j <= m; j++) {
      int t = i * (K - 1) - j + 1;
      if (!((j - m) % (K - 1)) && t <= n && !((t - n) % (K - 1))) {
        (ans += f[i][j][1]) %= P;
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#define LL long long
#define fo(i, j, k) for (int i = j; i <= k; i++)
#define fd(i, j, k) for (int i = j; i >= k; i--)
#define fr(i, j) for (int i = beg[j]; i; i = nex[i])
using namespace std;
int const mn = 2e3 + 3, mo = 1e9 + 7;
int n, m, K, f[mn][mn][2], g[mn];
int main() {
  scanf("%d%d%d", &n, &m, &K);
  m--;
  K--;
  int lim = (n + m) / K, ans = 0;
  fo(j, 0, K) {
    f[1][j][j != 0] = 1;
    if ((!((j - n) % K)) && (!((K - j - m) % K)) && (K - j <= m))
      ans = (ans + f[1][j][1]) % mo;
  }
  fo(i, 2, lim) {
    fo(j, 0, n) g[j] =
        ((j ? g[j - 1] : 0) + 1ll * f[i - 1][j][0] + f[i - 1][j][1]) % mo;
    fo(j, 0, n) {
      f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1]) % mo;
      if (j)
        f[i][j][1] = (g[j - 1] - ((j - K - 1 >= 0) ? g[j - K - 1] : 0)) % mo;
      if ((!((j - n) % K)) && (!((K * i - j - m) % K)) && (K * i - j <= m))
        ans = (ans + f[i][j][1]) % mo;
    }
  }
  printf("%d", (ans + mo) % mo);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#define LL long long
#define fo(i, j, k) for (int i = j; i <= k; i++)
#define fd(i, j, k) for (int i = j; i >= k; i--)
#define fr(i, j) for (int i = beg[j]; i; i = nex[i])
using namespace std;
int const mn = 2e3 + 3, mo = 1e9 + 7;
int n, m, K, f[mn * 2][mn][2], g[mn];
int main() {
  scanf("%d%d%d", &n, &m, &K);
  m--;
  K--;
  int lim = (n + m) / K, ans = 0;
  fo(j, 0, K) {
    f[1][j][j != 0] = 1;
    if ((!((j - n) % K)) && (!((K - j - m) % K)) && (K - j <= m))
      ans = (ans + f[1][j][1]) % mo;
  }
  fo(i, 2, lim) {
    fo(j, 0, n) g[j] =
        ((j ? g[j - 1] : 0) + 1ll * f[i - 1][j][0] + f[i - 1][j][1]) % mo;
    fo(j, 0, n) {
      f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1]) % mo;
      if (j)
        f[i][j][1] = (g[j - 1] - ((j - K - 1 >= 0) ? g[j - K - 1] : 0)) % mo;
      if ((!((j - n) % K)) && (!((K * i - j - m) % K)) && (K * i - j <= m))
        ans = (ans + f[i][j][1]) % mo;
    }
  }
  printf("%d", (ans + mo) % mo);
  return 0;
}
Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#define rep(a, b, c) for (int a = b; a <= c; a++)
#define per(a, b, c) for (int a = b; a >= c; a--)
#define go(u) for (int o = ft[u], v; v = E[o].t; o = E[o].n)
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int, int> par;
const int N = 2010, P = 1000000007;
void upd(LL &x, LL y) { x = (x + y) % P; }
int n, m, p;
LL f[N][N], g[N][N], ans;
bool equ(int a, int b) {
  if ((a == 0) ^ (b == 0))
    return 0;
  a = (a - 1) % (p - 1) + 1;
  b = (b - 1) % (p - 1) + 1;
  return a == b;
}
int main() {
  //	freopen("t.txt","r",stdin);
  scanf("%d%d%d", &n, &m, &p);
  int len = (n + m - 1) / (p - 1);
  f[0][0] = 1;
  rep(i, 1, len) rep(j, 0, n) {
    rep(k, 0, p - 1) {
      if (j < k)
        continue;
      upd(f[i][j], f[i - 1][j - k]);
    }
    rep(k, 1, p - 1) {
      if (j < k)
        continue;
      upd(g[i][j], f[i - 1][j - k]);
    }
    int x = j, y = i * (p - 1) - j + 1;
    if (equ(x, n) && equ(y, m) && y <= m) {
      upd(ans, g[i][j]);
      //			printf("ans=%lld ,from
      //g[%d][%d]=%lld\n",ans,i,j,g[i][j]);
    }
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#define rep(a, b, c) for (int a = b; a <= c; a++)
#define per(a, b, c) for (int a = b; a >= c; a--)
#define go(u) for (int o = ft[u], v; v = E[o].t; o = E[o].n)
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int, int> par;
const int N = 4010, P = 1000000007;
void upd(LL &x, LL y) { x = (x + y) % P; }
int n, m, p;
LL f[N][N], g[N][N], ans;
bool equ(int a, int b) {
  if ((a == 0) ^ (b == 0))
    return 0;
  a = (a - 1) % (p - 1) + 1;
  b = (b - 1) % (p - 1) + 1;
  return a == b;
}
int main() {
  //	freopen("t.txt","r",stdin);
  scanf("%d%d%d", &n, &m, &p);
  int len = (n + m - 1) / (p - 1);
  f[0][0] = 1;
  rep(i, 1, len) rep(j, 0, n) {
    rep(k, 0, p - 1) {
      if (j < k)
        continue;
      upd(f[i][j], f[i - 1][j - k]);
    }
    rep(k, 1, p - 1) {
      if (j < k)
        continue;
      upd(g[i][j], f[i - 1][j - k]);
    }
    int x = j, y = i * (p - 1) - j + 1;
    if (equ(x, n) && equ(y, m) && y <= m) {
      upd(ans, g[i][j]);
      //			printf("ans=%lld ,from
      //g[%d][%d]=%lld\n",ans,i,j,g[i][j]);
    }
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#define fo(i, a, b) for (int i = a; i <= b; i++)
#define fd(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

const int N = 2 * 1e3 + 5, Mo = 1e9 + 7;

int n, m, k, f[N][N][2], ans;

void inc(int &x, int y) {
  x = x + y;
  if (x >= Mo)
    x = x - Mo;
}

int main() {
  scanf("%d%d%d", &n, &m, &k);
  m--;
  k--;
  f[0][0][0] = 1;
  fo(i, 0, (n + m) / k) {
    fo(j, 0, n) fo(z, 0, 1) if (f[i][j][z]) {
      inc(f[i + 1][j + 1][1], f[i][j][z]);
      inc(f[i + 1][min(n + 1, j + k + 1)][1], Mo - f[i][j][z]);
      inc(f[i + 1][j][0], f[i][j][z]);
      inc(f[i + 1][j + 1][0], Mo - f[i][j][z]);
    }
    fo(j, 1, n) fo(z, 0, 1) inc(f[i + 1][j][z], f[i + 1][j - 1][z]);
  }
  fo(i, 1, (n + m) / k)
      fo(j, 0, n) if (j % k == n % k && (i * k - j) % k == m % k &&
                      i * k - j <= m) inc(ans, f[i][j][1]);
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#define fo(i, a, b) for (int i = a; i <= b; i++)
#define fd(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

const int N = 4 * 1e3 + 5, Mo = 1e9 + 7;

int n, m, k, f[N][N][2], ans;

void inc(int &x, int y) {
  x = x + y;
  if (x >= Mo)
    x = x - Mo;
}

int main() {
  scanf("%d%d%d", &n, &m, &k);
  m--;
  k--;
  f[0][0][0] = 1;
  fo(i, 0, (n + m) / k) {
    fo(j, 0, n) fo(z, 0, 1) if (f[i][j][z]) {
      inc(f[i + 1][j + 1][1], f[i][j][z]);
      inc(f[i + 1][min(n + 1, j + k + 1)][1], Mo - f[i][j][z]);
      inc(f[i + 1][j][0], f[i][j][z]);
      inc(f[i + 1][j + 1][0], Mo - f[i][j][z]);
    }
    fo(j, 1, n) fo(z, 0, 1) inc(f[i + 1][j][z], f[i + 1][j - 1][z]);
  }
  fo(i, 1, (n + m) / k)
      fo(j, 0, n) if (j % k == n % k && (i * k - j) % k == m % k &&
                      i * k - j <= m) inc(ans, f[i][j][1]);
  printf("%d\n", ans);
  return 0;
}
Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
#define mod 1000000007
typedef long long ll;

using namespace std;
#define N 2010
class CS {
public:
  ll cs[N];
  void init(ll a[]) {
    cs[0] = 0;
    for (int i = 0; i < N - 1; i++) {
      cs[i + 1] = cs[i] + a[i];
    }
  }
  ll qry(ll l, ll r) { return (cs[r + 1] - cs[l]) % mod; }
};
CS cs;

ll f[N][N], n, m, k;
int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < N; i++)
    f[0][i] = 0;
  f[0][0] = 1;
  for (int i = 1; i < N; i++) {
    cs.init(f[i - 1]);
    for (int j = 0; j < N; j++) {
      f[i][j] = cs.qry(max(0LL, j - k + 1), j);
    }
  }
  ll ans = 0;
  for (; n > 0; n -= (k - 1)) {
    ll s = (n + m - 1) / (k - 1);
    // cout<<s<<" "<<n<<" "<<f[s][n]<<endl;
    ans = (ans + f[s][n]) % mod;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
#define mod 1000000007
typedef long long ll;

using namespace std;
#define N 4010
class CS {
public:
  ll cs[N];
  void init(ll a[]) {
    cs[0] = 0;
    for (int i = 0; i < N - 1; i++) {
      cs[i + 1] = cs[i] + a[i];
    }
  }
  ll qry(ll l, ll r) { return (cs[r + 1] - cs[l]) % mod; }
};
CS cs;

ll f[N][N], n, m, k;
int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < N; i++)
    f[0][i] = 0;
  f[0][0] = 1;
  for (int i = 1; i < N; i++) {
    cs.init(f[i - 1]);
    for (int j = 0; j < N; j++) {
      f[i][j] = cs.qry(max(0LL, j - k + 1), j);
    }
  }
  ll ans = 0;
  for (; n > 0; n -= (k - 1)) {
    ll s = (n + m - 1) / (k - 1);
    // cout<<s<<" "<<n<<" "<<f[s][n]<<endl;
    ans = (ans + f[s][n]) % mod;
  }
  cout << ans << endl;
  return 0;
}
Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
typedef long long ll;
#define rep(i, a, b) for (int i = a; i <= b; ++i)
const int N = 2005, mo = 1000000007;
using namespace std;

int n, m, k, f[N][N];

inline void upd(int &x, int y) { x = (x + y) % mo; }

int main() {
  scanf("%d%d%d", &n, &m, &k);
  int L = (n + m - 1) / (k - 1);
  f[0][0] = 1;
  int res = 0;
  rep(i, 1, L) {
    rep(j, 0, k - 1) rep(p, 0, n) {
      if (p + j > n)
        continue;
      upd(f[i][p + j], f[i - 1][p]);
      if (j == 0)
        continue;
      int x = p + j;
      if (x % (k - 1) != n % (k - 1))
        continue;
      if (i * (k - 1) - x + 1 > m)
        continue;
      if ((i * (k - 1) - x + 1) % (k - 1) != m % (k - 1))
        continue;
      upd(res, f[i - 1][p]);
    }
  }
  printf("%d\n", res);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
typedef long long ll;
#define rep(i, a, b) for (int i = a; i <= b; ++i)
const int N = 4005, mo = 1000000007;
using namespace std;

int n, m, k, f[N][N];

inline void upd(int &x, int y) { x = (x + y) % mo; }

int main() {
  scanf("%d%d%d", &n, &m, &k);
  int L = (n + m - 1) / (k - 1);
  f[0][0] = 1;
  int res = 0;
  rep(i, 1, L) {
    rep(j, 0, k - 1) rep(p, 0, n) {
      if (p + j > n)
        continue;
      upd(f[i][p + j], f[i - 1][p]);
      if (j == 0)
        continue;
      int x = p + j;
      if (x % (k - 1) != n % (k - 1))
        continue;
      if (i * (k - 1) - x + 1 > m)
        continue;
      if ((i * (k - 1) - x + 1) % (k - 1) != m % (k - 1))
        continue;
      upd(res, f[i - 1][p]);
    }
  }
  printf("%d\n", res);
  return 0;
}
Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 10;
const int mod = 1e9 + 7;
int n, m, k, f[N][N][2], sum[N], ans = 0;
int main() {
  cin >> n >> m >> k;
  m--;
  k--;
  int up = (n + m) / k;
  f[0][0][0] = 1;
  for (int i = 1; i <= up; i++) {
    for (int j = 0; j <= n; j++)
      sum[j] = (((j - 1 >= 0 ? sum[j - 1] : 0) + f[i - 1][j][0]) % mod +
                f[i - 1][j][1]) %
               mod;
    for (int j = 0; j <= n; j++) {
      f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1]) % mod;
      int t = max(0, j - k);
      f[i][j][1] = (sum[j - 1] - (t - 1 >= 0 ? sum[t - 1] : 0) + mod) % mod;
    }
    for (int j = 0; j <= n; j++)
      if ((j % k == n % k) && ((i * k - j) % k == m % k) && i * k - j <= m)
        ans = (ans + f[i][j][1]) % mod;
  }
  printf("%d\n", ans);
  return 0;
}
// orzgzy
// 鸡贼明年进队超稳


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 10;
const int mod = 1e9 + 7;
int n, m, k, f[N * 2][N][2], sum[N], ans = 0;
int main() {
  cin >> n >> m >> k;
  m--;
  k--;
  int up = (n + m) / k;
  f[0][0][0] = 1;
  for (int i = 1; i <= up; i++) {
    for (int j = 0; j <= n; j++)
      sum[j] = (((j - 1 >= 0 ? sum[j - 1] : 0) + f[i - 1][j][0]) % mod +
                f[i - 1][j][1]) %
               mod;
    for (int j = 0; j <= n; j++) {
      f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1]) % mod;
      int t = max(0, j - k);
      f[i][j][1] = (sum[j - 1] - (t - 1 >= 0 ? sum[t - 1] : 0) + mod) % mod;
    }
    for (int j = 0; j <= n; j++)
      if ((j % k == n % k) && ((i * k - j) % k == m % k) && i * k - j <= m)
        ans = (ans + f[i][j][1]) % mod;
  }
  printf("%d\n", ans);
  return 0;
}
// orzgzy
// 鸡贼明年进队超稳
Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void Add(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  if (x < 0)
    x += mod;
}
int n, m, k, F[3000][3000];
int main() {
  F[0][0] = 1;
  n = read();
  m = read();
  k = read();
  for (int i = 0; i < (n + m - 1) / (k - 1); i++)
    for (int j = 0; j <= m; j++)
      for (int l = 0; l < k; l++)
        if (j + l <= m)
          Add(F[i + 1][j + l], F[i][j]);
  int Ans = 0;
  while (m > 0)
    Add(Ans, F[(n + m - 1) / (k - 1)][m]), m -= k - 1;
  cout << Ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void Add(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  if (x < 0)
    x += mod;
}
int n, m, k, F[6000][6000];
int main() {
  F[0][0] = 1;
  n = read();
  m = read();
  k = read();
  for (int i = 0; i < (n + m - 1) / (k - 1); i++)
    for (int j = 0; j <= m; j++)
      for (int l = 0; l < k; l++)
        if (j + l <= m)
          Add(F[i + 1][j + l], F[i][j]);
  int Ans = 0;
  while (m > 0)
    Add(Ans, F[(n + m - 1) / (k - 1)][m]), m -= k - 1;
  cout << Ans << endl;
}
Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

const int maxN = 2010;
const int Mod = 1e9 + 7;

int n, m, K, L;
int F[maxN][maxN];

void Plus(int &x, int y);
int main() {
  scanf("%d%d%d", &n, &m, &K);
  --m;
  L = (n + m) / (K - 1);
  F[0][0] = 1;
  int Ans = 0;
  for (int i = 1; i <= L; i++)
    for (int j = 0; j <= min(i * (K - 1), n); j++) {
      for (int k = 0; k <= K - 1 && k <= j; k++)
        Plus(F[i][j], F[i - 1][j - k]);
      if (j % (K - 1) == n % (K - 1) && (K - 1) * i - j <= m &&
          ((K - 1) * i - j) % (K - 1) == m % (K - 1))
        Plus(Ans, F[i][j] - F[i - 1][j]); //,cout<<i<<" "<<j<<endl;
    }
  printf("%d\n", Ans);
  return 0;
}
void Plus(int &x, int y) {
  x += y;
  if (x >= Mod)
    x -= Mod;
  if (x < 0)
    x += Mod;
  return;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

const int maxN = 4010;
const int Mod = 1e9 + 7;

int n, m, K, L;
int F[maxN][maxN];

void Plus(int &x, int y);
int main() {
  scanf("%d%d%d", &n, &m, &K);
  --m;
  L = (n + m) / (K - 1);
  F[0][0] = 1;
  int Ans = 0;
  for (int i = 1; i <= L; i++)
    for (int j = 0; j <= min(i * (K - 1), n); j++) {
      for (int k = 0; k <= K - 1 && k <= j; k++)
        Plus(F[i][j], F[i - 1][j - k]);
      if (j % (K - 1) == n % (K - 1) && (K - 1) * i - j <= m &&
          ((K - 1) * i - j) % (K - 1) == m % (K - 1))
        Plus(Ans, F[i][j] - F[i - 1][j]); //,cout<<i<<" "<<j<<endl;
    }
  printf("%d\n", Ans);
  return 0;
}
void Plus(int &x, int y) {
  x += y;
  if (x >= Mod)
    x -= Mod;
  if (x < 0)
    x += Mod;
  return;
}
Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int N = 2e3 + 1;
const int mod = 1e9 + 7;

void add(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
  if (a < 0)
    a += mod;
}

int f[N][N];
int g[N][N];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n, m, k;

  cin >> n >> m >> k;

  f[0][0] = 1;

  for (int i = 1; i < n + m; ++i)
    for (int j = 0; j <= n; ++j) {
      g[i][j] = (j ? g[i][j - 1] : 0);
      add(g[i][j], f[i - 1][j]);
      f[i][j] = g[i][j];
      if (j >= k)
        add(f[i][j], -g[i][j - k]);
    }

  int ans = 0;

  while (n > 0) {
    add(ans, f[(n + m - 1) / (k - 1)][n]);
    n -= k - 1;
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int N = 4e3 + 1;
const int mod = 1e9 + 7;

void add(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
  if (a < 0)
    a += mod;
}

int f[N][N];
int g[N][N];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n, m, k;

  cin >> n >> m >> k;

  f[0][0] = 1;

  for (int i = 1; i < n + m; ++i)
    for (int j = 0; j <= n; ++j) {
      g[i][j] = (j ? g[i][j - 1] : 0);
      add(g[i][j], f[i - 1][j]);
      f[i][j] = g[i][j];
      if (j >= k)
        add(f[i][j], -g[i][j - k]);
    }

  int ans = 0;

  while (n > 0) {
    add(ans, f[(n + m - 1) / (k - 1)][n]);
    n -= k - 1;
  }

  cout << ans << endl;
}
Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>

const int MOD = 1e9 + 7;

int dp[2005][2005];

int main() {
  int N, M, K;
  scanf("%d %d %d", &N, &M, &K);
  int nodes = (N + M - 1) / (K - 1);
  dp[0][0] = 1;
  for (int i = 0; i < nodes; i++) {
    for (int j = 0; j <= N; j++) {
      // printf("dp[%d][%d]=%d\n",i,j,dp[i][j]);
      for (int k = 0; k < K && j + k <= N; k++) {
        dp[i + 1][j + k] = (dp[i + 1][j + k] + dp[i][j]) % MOD;
      }
    }
  }
  int total = dp[nodes][N];
  if (N >= K) {
    while (M >= 1) {
      total = (total + dp[nodes - 1][N - K]) % MOD;
      nodes--;
      M -= K - 1;
    }
  }
  printf("%d\n", total);
  return 0;
}


Fixed submission:
#include <cstdio>

const int MOD = 1e9 + 7;

int dp[4005][2005];

int main() {
  int N, M, K;
  scanf("%d %d %d", &N, &M, &K);
  int nodes = (N + M - 1) / (K - 1);
  dp[0][0] = 1;
  for (int i = 0; i < nodes; i++) {
    for (int j = 0; j <= N; j++) {
      // printf("dp[%d][%d]=%d\n",i,j,dp[i][j]);
      for (int k = 0; k < K && j + k <= N; k++) {
        dp[i + 1][j + k] = (dp[i + 1][j + k] + dp[i][j]) % MOD;
      }
    }
  }
  int total = dp[nodes][N];
  if (N >= K) {
    while (M >= 1) {
      total = (total + dp[nodes - 1][N - K]) % MOD;
      nodes--;
      M -= K - 1;
    }
  }
  printf("%d\n", total);
  return 0;
}
Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
#define MAXN 2010
#define MO 1000000007
#define LL long long
int n, m, k, M, N, K;
LL dp[MAXN][MAXN], ans;
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i <= m; i++)
    dp[0][i] = 1;
  M = m % (k - 1);
  N = (n - 1) % (k - 1);
  K = (n + m - 1) / (k - 1);
  for (int i = 1, ed = k - 1; i <= K; ed += k - 1, i++) {
    for (int j = 0; j <= m && j <= ed; j++) {
      if (j - k + 1 <= 0)
        dp[i][j] = dp[i - 1][j];
      else
        dp[i][j] = dp[i - 1][j] - dp[i - 1][j - k];
      if (j % (k - 1) == M && j && ed - j < n && (ed - j) % (k - 1) == N) {
        if (j - k + 1 <= 0)
          ans += dp[i - 1][j - 1];
        else
          ans += dp[i - 1][j - 1] - dp[i - 1][j - k];
        ans = (ans % MO + MO) % MO;
      }
      if (j != 0)
        dp[i][j] += dp[i][j - 1];
      dp[i][j] = (dp[i][j] % MO + MO) % MO;
    }
    for (int j = ed + 1; j <= m; j++)
      dp[i][j] = dp[i][j - 1];
  }
  printf("%lld\n", ans);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
#define MAXN 2010
#define MO 1000000007
#define LL long long
int n, m, k, M, N, K;
LL dp[MAXN * 2][MAXN], ans;
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i <= m; i++)
    dp[0][i] = 1;
  M = m % (k - 1);
  N = (n - 1) % (k - 1);
  K = (n + m - 1) / (k - 1);
  for (int i = 1, ed = k - 1; i <= K; ed += k - 1, i++) {
    for (int j = 0; j <= m && j <= ed; j++) {
      if (j - k + 1 <= 0)
        dp[i][j] = dp[i - 1][j];
      else
        dp[i][j] = dp[i - 1][j] - dp[i - 1][j - k];
      if (j % (k - 1) == M && j && ed - j < n && (ed - j) % (k - 1) == N) {
        if (j - k + 1 <= 0)
          ans += dp[i - 1][j - 1];
        else
          ans += dp[i - 1][j - 1] - dp[i - 1][j - k];
        ans = (ans % MO + MO) % MO;
      }
      if (j != 0)
        dp[i][j] += dp[i][j - 1];
      dp[i][j] = (dp[i][j] % MO + MO) % MO;
    }
    for (int j = ed + 1; j <= m; j++)
      dp[i][j] = dp[i][j - 1];
  }
  printf("%lld\n", ans);
}
Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define P 1000000007
#define ll long long
using namespace std;
int n, m, K, S;
ll f[2005][2005], g[2005][2005], Ans;
int main() {
  scanf("%d%d%d", &n, &m, &K);
  f[0][0] = 1;
  for (int i = 0; i <= n; i++)
    g[0][i] = 1;
  S = (n + m - 1) / (K - 1);
  for (int i = 1; i <= S; i++) {
    for (int j = 0; j <= n; j++) {
      /*
      for (int t=0;t<=j&&t<K;t++)
              f[i][j]=f[i][j]+f[i-1][j-t];*/
      if (j >= K)
        f[i][j] = (g[i - 1][j] - g[i - 1][j - K]) % P;
      else
        f[i][j] = g[i - 1][j];
      if (j)
        g[i][j] = (g[i][j - 1] + f[i][j]) % P;
      else
        g[i][j] = f[i][j];
      int tmp = i * (K - 1) - j;
      if (j % (K - 1) == n % (K - 1) && tmp <= m - 1 &&
          tmp % (K - 1) == (m - 1) % (K - 1)) {
        Ans = (Ans + f[i][j] - f[i - 1][j]) % P;
      }
    }
  }
  printf("%lld\n", (Ans + P) % P);
}

Fixed submission:
#include <bits/stdc++.h>
#define P 1000000007
#define ll long long
using namespace std;
int n, m, K, S;
ll f[4005][2005], g[4005][2005], Ans;
int main() {
  scanf("%d%d%d", &n, &m, &K);
  f[0][0] = 1;
  for (int i = 0; i <= n; i++)
    g[0][i] = 1;
  S = (n + m - 1) / (K - 1);
  for (int i = 1; i <= S; i++) {
    for (int j = 0; j <= n; j++) {
      /*
      for (int t=0;t<=j&&t<K;t++)
              f[i][j]=f[i][j]+f[i-1][j-t];*/
      if (j >= K)
        f[i][j] = (g[i - 1][j] - g[i - 1][j - K]) % P;
      else
        f[i][j] = g[i - 1][j];
      if (j)
        g[i][j] = (g[i][j - 1] + f[i][j]) % P;
      else
        g[i][j] = f[i][j];
      int tmp = i * (K - 1) - j;
      if (j % (K - 1) == n % (K - 1) && tmp <= m - 1 &&
          tmp % (K - 1) == (m - 1) % (K - 1)) {
        Ans = (Ans + f[i][j] - f[i - 1][j]) % P;
      }
    }
  }
  printf("%lld\n", (Ans + P) % P);
}
Problem p03825 (C++)

Buggy submission (status: Runtime Error):
// Eternal Average
// * frank_c1
// * 2017 / 09 / 29

#include <bits/stdc++.h>
using namespace std;

const int maxn = 2005;
const int mo = (int)(1e9) + 7;
int f[maxn][maxn];

inline void add(int &x, int v) {
  x += v;
  if (x >= mo)
    x -= mo;
}

int main() {
  int n, m, K, ret = 0;
  scanf("%d%d%d", &n, &m, &K);
  f[0][0] = 1;
  for (int i = 1;; ++i) {
    int mi = (K - 1) * i + 1 - n;
    if (mi > m)
      break;
    for (int w = 1; w < K; ++w) {
      for (int j = w; j <= m; ++j) {
        add(f[i][j], f[i - 1][j - w]);
      }
    }
    for (int j = 0; j <= m; ++j)
      if (!((j - m) % (K - 1)) && !((j + n - (K - 1) * i - 1) % (K - 1)) &&
          j >= mi)
        add(ret, f[i][j]);
    for (int j = 0; j <= m; ++j)
      add(f[i][j], f[i - 1][j]);
  }
  return printf("%d\n", ret), 0;
}

Fixed submission:
// Eternal Average
// * frank_c1
// * 2017 / 09 / 29

#include <bits/stdc++.h>
using namespace std;

const int maxn = 6005;
const int mo = (int)(1e9) + 7;
int f[maxn][maxn];

inline void add(int &x, int v) {
  x += v;
  if (x >= mo)
    x -= mo;
}

int main() {
  int n, m, K, ret = 0;
  scanf("%d%d%d", &n, &m, &K);
  f[0][0] = 1;
  for (int i = 1;; ++i) {
    int mi = (K - 1) * i + 1 - n;
    if (mi > m)
      break;
    for (int w = 1; w < K; ++w) {
      for (int j = w; j <= m; ++j) {
        add(f[i][j], f[i - 1][j - w]);
      }
    }
    for (int j = 0; j <= m; ++j)
      if (!((j - m) % (K - 1)) && !((j + n - (K - 1) * i - 1) % (K - 1)) &&
          j >= mi)
        add(ret, f[i][j]);
    for (int j = 0; j <= m; ++j)
      add(f[i][j], f[i - 1][j]);
  }
  return printf("%d\n", ret), 0;
}
Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;

typedef long long LL;

const int N = 2010;
const LL mod = 1e9 + 7;

int n, m, k;
LL f[N][N], sum[N];

int main() {
  scanf("%d %d %d", &n, &m, &k);
  LL ans = 0;
  f[0][0] = 1;
  for (int i = 1; i <= (n + m - 1) / (k - 1); ++i) {
    for (int j = 1; j <= n + 1; ++j)
      sum[j] = (sum[j - 1] + f[i - 1][j - 1]) % mod;
    for (int j = 0; j <= n; ++j)
      f[i][j] = (sum[j + 1] - sum[max(j - k + 1, 0)] + mod) % mod;
    for (int j = 1; j <= n; ++j)
      if (j % (k - 1) == n % (k - 1) &&
          (k - 1 - j % (k - 1)) % (k - 1) == (m - 1) % (k - 1) &&
          (k - 1) * i - j <= m - 1)
        (ans += f[i][j] - f[i - 1][j] + mod) %= mod;
  }
  printf("%lld\n", ans);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;

typedef long long LL;

const int N = 4010;
const LL mod = 1e9 + 7;

int n, m, k;
LL f[N][N], sum[N];

int main() {
  scanf("%d %d %d", &n, &m, &k);
  LL ans = 0;
  f[0][0] = 1;
  for (int i = 1; i <= (n + m - 1) / (k - 1); ++i) {
    for (int j = 1; j <= n + 1; ++j)
      sum[j] = (sum[j - 1] + f[i - 1][j - 1]) % mod;
    for (int j = 0; j <= n; ++j)
      f[i][j] = (sum[j + 1] - sum[max(j - k + 1, 0)] + mod) % mod;
    for (int j = 1; j <= n; ++j)
      if (j % (k - 1) == n % (k - 1) &&
          (k - 1 - j % (k - 1)) % (k - 1) == (m - 1) % (k - 1) &&
          (k - 1) * i - j <= m - 1)
        (ans += f[i][j] - f[i - 1][j] + mod) %= mod;
  }
  printf("%lld\n", ans);
}
Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int maxn = 2010;
const int mod = 1e9 + 7;

int n, m, K, ans;
int f[maxn][maxn << 1];

int main() {
  scanf("%d%d%d", &n, &m, &K);
  f[0][0] = 1;
  for (int i = 1; i <= (n + m - 1) / (K - 1); i++) {
    for (int j = 0; j <= n + m; j++) {
      for (int k = 0; k < K && k <= j; k++) {
        f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod;
      }
    }
  }
  for (int i = 0; i * (K - 1) < m; i++) {
    ans = (ans + f[(n + m - 1) / (K - 1) - i][m - i * (K - 1)]) % mod;
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int maxn = 2010;
const int mod = 1e9 + 7;

int n, m, K, ans;
int f[maxn << 1][maxn << 1];

int main() {
  scanf("%d%d%d", &n, &m, &K);
  f[0][0] = 1;
  for (int i = 1; i <= (n + m - 1) / (K - 1); i++) {
    for (int j = 0; j <= n + m; j++) {
      for (int k = 0; k < K && k <= j; k++) {
        f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod;
      }
    }
  }
  for (int i = 0; i * (K - 1) < m; i++) {
    ans = (ans + f[(n + m - 1) / (K - 1) - i][m - i * (K - 1)]) % mod;
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03825 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define INF (1 << 29)
#define LINF (1LL << 60)
#define EPS (1e-10)
#define PI 3.1415926535897932384626433832795028
typedef long long Int;
typedef pair<Int, Int> P;
typedef long double Real;
typedef complex<Real> CP;

Int n, m, k;
Int dp[2200][2200];
Int dp_sum[2200][2200];

int main() {
  cin >> n >> m >> k;
  int l = (n + m - 1) / (k - 1);
  dp[0][0] = 1;
  for (int i = 1; i <= l; i++) {
    for (int j = 0; j <= n; j++) {
      dp_sum[i - 1][j] = dp[i - 1][j];
      if (j)
        (dp_sum[i - 1][j] += dp_sum[i - 1][j - 1]) %= MOD;
    }
    for (int j = 0; j <= n; j++) {
      dp[i][j] += dp_sum[i - 1][j];
      if (j - k >= 0)
        dp[i][j] -= dp_sum[i - 1][j - k];
      dp[i][j] %= MOD;
    }
  }
  Int ans = 0;
  for (int i = l; i >= 1; i--) {
    int sum = n - (k - 1) * (l - i);
    if (sum <= 0)
      break;
    ans += dp[i][sum];
    ans %= MOD;
  }
  if (ans < 0)
    ans += MOD;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define INF (1 << 29)
#define LINF (1LL << 60)
#define EPS (1e-10)
#define PI 3.1415926535897932384626433832795028
typedef long long Int;
typedef pair<Int, Int> P;
typedef long double Real;
typedef complex<Real> CP;

Int n, m, k;
Int dp[4200][2200];
Int dp_sum[4200][2200];

int main() {
  cin >> n >> m >> k;
  int l = (n + m - 1) / (k - 1);
  dp[0][0] = 1;
  for (int i = 1; i <= l; i++) {
    for (int j = 0; j <= n; j++) {
      dp_sum[i - 1][j] = dp[i - 1][j];
      if (j)
        (dp_sum[i - 1][j] += dp_sum[i - 1][j - 1]) %= MOD;
    }
    for (int j = 0; j <= n; j++) {
      dp[i][j] += dp_sum[i - 1][j];
      if (j - k >= 0)
        dp[i][j] -= dp_sum[i - 1][j - k];
      dp[i][j] %= MOD;
    }
  }
  Int ans = 0;
  for (int i = l; i >= 1; i--) {
    int sum = n - (k - 1) * (l - i);
    if (sum <= 0)
      break;
    ans += dp[i][sum];
    ans %= MOD;
  }
  if (ans < 0)
    ans += MOD;
  cout << ans << endl;
  return 0;
}