Problem p02653 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define upd(a, b) a = add(a + b)
using namespace std;
inline char nc() {
  //	return getchar();
  static char buf[100000], *l = buf, *r = buf;
  return l == r && (r = (l = buf) + fread(buf, 1, 100000, stdin), l == r)
             ? EOF
             : *l++;
}
template <class T> void read(T &x) {
  x = 0;
  int f = 1, ch = nc();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = nc();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 - '0' + ch;
    ch = nc();
  }
  x *= f;
}
typedef long long ll;
const int mod = 1e9 + 7;
const int maxn = 5000 + 5;
int n, A, B;
int f[maxn][2], g[maxn][2];
int dp[maxn][2][2];
inline int add(int x) { return x >= mod ? x - mod : x; }
void DP(int dp[maxn][2]) {
  for (int i = 0; i < n; ++i) {
    upd(dp[i + 1][1], dp[i][0]);
    upd(dp[i + 1][1], dp[i][1]);
    for (int k = A; i + k <= n; ++k)
      upd(dp[i + k][0], dp[i][1]);
  }
}
int main() {
  read(n), read(A), read(B);
  if (A > B)
    swap(A, B);
  f[0][0] = 1, DP(f);
  g[0][0] = 1;
  for (int i = A; i <= n; ++i)
    g[i][0] = 1;
  DP(g);
  dp[n][1][0] = g[n][0];
  for (int i = 1; i <= n; ++i)
    dp[i][i >= B][1] = g[i][1];
  for (int i = 1; i < A; ++i)
    dp[i][0][0] = 1;
  for (int i = 1; i <= n; ++i)
    for (int k = 0; k < 2; ++k) {
      for (int j = 1; j < A && i + j <= n; ++j)
        upd(dp[i + j][k][0], dp[i][k][1]);
      for (int j = 1; i + j <= n; ++j) {
        int r = f[j][1];
        if (i + j == n)
          upd(r, f[j][0]);
        upd(dp[i + j][k || j >= B][1], (ll)dp[i][k][0] * r % mod);
      }
    }
  printf("%d\\n", add(dp[n][1][0] + dp[n][1][1]));
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define upd(a, b) a = add(a + b)
using namespace std;
inline char nc() {
  return getchar();
  static char buf[100000], *l = buf, *r = buf;
  return l == r && (r = (l = buf) + fread(buf, 1, 100000, stdin), l == r)
             ? EOF
             : *l++;
}
template <class T> void read(T &x) {
  x = 0;
  int f = 1, ch = nc();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = nc();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 - '0' + ch;
    ch = nc();
  }
  x *= f;
}
typedef long long ll;
const int mod = 1e9 + 7;
const int maxn = 5000 + 5;
int n, A, B;
int f[maxn][2], g[maxn][2];
int dp[maxn][2][2];
inline int add(int x) { return x >= mod ? x - mod : x; }
void DP(int dp[maxn][2]) {
  for (int i = 0; i < n; ++i) {
    upd(dp[i + 1][1], dp[i][0]);
    upd(dp[i + 1][1], dp[i][1]);
    for (int k = A; i + k <= n; ++k)
      upd(dp[i + k][0], dp[i][1]);
  }
}
int main() {
  read(n), read(A), read(B);
  if (A > B)
    swap(A, B);
  f[0][0] = 1, DP(f);
  g[0][0] = 1;
  for (int i = A; i <= n; ++i)
    g[i][0] = 1;
  DP(g);
  dp[n][1][0] = g[n][0];
  for (int i = 1; i <= n; ++i)
    dp[i][i >= B][1] = g[i][1];
  for (int i = 1; i < A; ++i)
    dp[i][0][0] = 1;
  for (int i = 1; i <= n; ++i)
    for (int k = 0; k < 2; ++k) {
      for (int j = 1; j < A; ++j)
        upd(dp[i + j][k][0], dp[i][k][1]);
      for (int j = 1; i + j <= n; ++j) {
        int r = f[j][1];
        if (i + j == n)
          upd(r, f[j][0]);
        upd(dp[i + j][k || j >= B][1], (ll)dp[i][k][0] * r % mod);
      }
    }
  printf("%d\\n", add(dp[n][1][0] + dp[n][1][1]));
  return 0;
}