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

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

typedef long long ll;
const ll MOD = 1000000007;

ll n, a, b;
ll x[5002];
void x_init() {
  ll dp[5005][2] = {};
  dp[1][0] = 1;
  dp[1][1] = 0;
  for (int i = 1; i <= a; i++) {
    dp[i + 1][0] += dp[i][0];
    if (dp[i + 1][0] >= MOD)
      dp[i + 1][0] -= MOD;
    if (i + b <= a) {
      dp[i + b][1] += dp[i][0];
      if (dp[i + b][1] >= MOD)
        dp[i + b][1] -= MOD;
    }
    dp[i + 1][0] += dp[i][1];
    if (dp[i + 1][0] >= MOD)
      dp[i + 1][0] -= MOD;
    dp[i + 1][1] += dp[i][1];
    if (dp[i + 1][1] >= MOD)
      dp[i + 1][1] -= MOD;
  }
  for (int i = 1; i <= a; i++) {
    x[i] = dp[i][0];
    // cout<<i<<":"<<x[i]<<endl;
  }
}

int main() {
  cin >> n >> a >> b;
  if (a < b)
    swap(a, b);
  x_init();

  ll dp[5002][2] = {};
  for (int i = 1; i <= a - 1; i++) {
    dp[i][0] = x[i + 1];
  }
  for (int i = 1; i <= b - 1; i++) {
    dp[i][1] = 1;
  }
  for (int i = 1; i <= n - 1; i++) {
    for (int j = 1; j <= b - 1; j++) {
      if (i + j <= n) {
        dp[i + j][1] += dp[i][0];
        if (dp[i + j][1] >= MOD)
          dp[i + j][1] -= MOD;
      }
    }
    for (int j = 1; j <= a - 1; j++) {
      if (i + j < n) {
        dp[i + j][0] += dp[i][1] * x[j];
        dp[i + j][0] %= MOD;
      } else if (i + j == n) {
        dp[i + j][0] += dp[i][1] * x[j + 1];
        dp[i + j][0] %= MOD;
      }
    }
  }
  ll ret = 1;
  for (int i = 0; i < n; i++) {
    ret *= 2;
    ret %= MOD;
  }
  ret += MOD - dp[n][0];
  ret += MOD - dp[n][1];
  cout << ret % MOD << endl;
  // cout<<(dp[n][0]+dp[n][1])%MOD<<endl;
}


Error: 0

Fixed submission:

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

typedef long long ll;
const ll MOD = 1000000007;

ll n, a, b;
ll x[5002];
void x_init() {
  ll dp[5005][2];
  dp[1][0] = 1;
  dp[1][1] = 0;
  for (int i = 1; i <= a; i++) {
    dp[i + 1][0] += dp[i][0];
    if (dp[i + 1][0] >= MOD)
      dp[i + 1][0] -= MOD;
    if (i + b <= a) {
      dp[i + b][1] += dp[i][0];
      if (dp[i + b][1] >= MOD)
        dp[i + b][1] -= MOD;
    }
    dp[i + 1][0] += dp[i][1];
    if (dp[i + 1][0] >= MOD)
      dp[i + 1][0] -= MOD;
    dp[i + 1][1] += dp[i][1];
    if (dp[i + 1][1] >= MOD)
      dp[i + 1][1] -= MOD;
  }
  for (int i = 1; i <= a; i++) {
    x[i] = dp[i][0];
    // cout<<i<<":"<<x[i]<<endl;
  }
}

int main() {
  cin >> n >> a >> b;
  if (a < b)
    swap(a, b);
  x_init();

  ll dp[5002][2] = {};
  for (int i = 1; i <= a - 1; i++) {
    dp[i][0] = x[i + 1];
  }
  for (int i = 1; i <= b - 1; i++) {
    dp[i][1] = 1;
  }
  for (int i = 1; i <= n - 1; i++) {
    for (int j = 1; j <= b - 1; j++) {
      if (i + j <= n) {
        dp[i + j][1] += dp[i][0];
        if (dp[i + j][1] >= MOD)
          dp[i + j][1] -= MOD;
      }
    }
    for (int j = 1; j <= a - 1; j++) {
      if (i + j < n) {
        dp[i + j][0] += dp[i][1] * x[j];
        dp[i + j][0] %= MOD;
      } else {
        dp[i + j][0] += dp[i][1] * x[j + 1];
        dp[i + j][0] %= MOD;
      }
    }
  }
  ll ret = 1;
  for (int i = 0; i < n; i++) {
    ret *= 2;
    ret %= MOD;
  }
  ret += MOD - dp[n][0];
  ret += MOD - dp[n][1];
  cout << ret % MOD << endl;
  // cout<<(dp[n][0]+dp[n][1])%MOD<<endl;
}