Problem p02653 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

const ll MOD = 1e9 + 7;
void add(ll &lhs, ll rhs) { lhs = (lhs + rhs) % MOD; }
int main() {
  int n, a, b;
  cin >> n >> a >> b;
  if (b < a)
    swap(a, b);
  if (a == 1 || b == 1) {

    assert(false);
  }
  vector<vector<ll>> Adp(n + 1, vector<ll>(b));
  vector<vector<ll>> Bdp(n + 1, vector<ll>(b));
  vector<vector<ll>> Cdp(n + 1, vector<ll>(b));
  vector<ll> ok(n + 1);
  Adp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < a; j++) {
      if (j + 1 < a) {
        add(Adp[i + 1][j + 1], Adp[i][j]);
      }
      add(Bdp[i + 1][1], Adp[i][j]);
      if (i == 0 && j == 0) {
        if (a == b) {
          add(ok[i + a], Adp[i][0]);
        } else {
          add(Cdp[i + a][a], Adp[i][0]);
        }
      }
    }
    for (int j = 0; j < b; j++) {
      add(Adp[i + 1][1], Bdp[i][j]);
      if (j + 1 < b) {
        add(Bdp[i + 1][j + 1], Bdp[i][j]);
      } else {
        add(ok[i + 1], Bdp[i][j]);
      }
      if (i + a <= n) {
        if (j + a < b) {
          add(Cdp[i + a][j + a], Bdp[i][j]);
        } else {
          add(ok[i + a], Bdp[i][j]);
        }
      }
    }
    for (int j = 0; j < b; j++) {
      if (j + 1 < b) {
        add(Bdp[i + 1][j + 1], Cdp[i][j]);
        add(Cdp[i + 1][j + 1], Cdp[i][j]);
      } else {
        add(ok[i + 1], Cdp[i][j]);
        add(ok[i + 1], Cdp[i][j]);
      }
    }
    add(ok[i + 1], ok[i]);
    add(ok[i + 1], ok[i]);
  }

  cout << ok[n] << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

const ll MOD = 1e9 + 7;
void add(ll &lhs, ll rhs) { lhs = (lhs + rhs) % MOD; }
int main() {
  int n, a, b;
  cin >> n >> a >> b;
  if (b < a)
    swap(a, b);
  if (a == 1 || b == 1) {
    ll res = 1;
    for (int i = 0; i < n; i++) {
      res = res * 2 % MOD;
    }
    cout << res << endl;
    return 0;
  }
  vector<vector<ll>> Adp(n + 1, vector<ll>(b));
  vector<vector<ll>> Bdp(n + 1, vector<ll>(b));
  vector<vector<ll>> Cdp(n + 1, vector<ll>(b));
  vector<ll> ok(n + 1);
  Adp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < a; j++) {
      if (j + 1 < a) {
        add(Adp[i + 1][j + 1], Adp[i][j]);
      }
      add(Bdp[i + 1][1], Adp[i][j]);
      if (i == 0 && j == 0) {
        if (a == b) {
          add(ok[i + a], Adp[i][0]);
        } else {
          add(Cdp[i + a][a], Adp[i][0]);
        }
      }
    }
    for (int j = 0; j < b; j++) {
      add(Adp[i + 1][1], Bdp[i][j]);
      if (j + 1 < b) {
        add(Bdp[i + 1][j + 1], Bdp[i][j]);
      } else {
        add(ok[i + 1], Bdp[i][j]);
      }
      if (i + a <= n) {
        if (j + a < b) {
          add(Cdp[i + a][j + a], Bdp[i][j]);
        } else {
          add(ok[i + a], Bdp[i][j]);
        }
      }
    }
    for (int j = 0; j < b; j++) {
      if (j + 1 < b) {
        add(Bdp[i + 1][j + 1], Cdp[i][j]);
        add(Cdp[i + 1][j + 1], Cdp[i][j]);
      } else {
        add(ok[i + 1], Cdp[i][j]);
        add(ok[i + 1], Cdp[i][j]);
      }
    }
    add(ok[i + 1], ok[i]);
    add(ok[i + 1], ok[i]);
  }

  cout << ok[n] << endl;

  return 0;
}