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

#include <bits/stdc++.h>
#define pb push_back
#define pll pair<ll, ll>
#define mp make_pair
#define pyshnapyshnakaa                                                        \\
  ios_base ::sync_with_stdio(0);                                               \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define x first
#define y second
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define plll pair<pair<ll, ll>, ll>
#define pllll pair<pair<ll, ll>, pair<ll, ll>>
#define psl pair<string, ll>
#define all(a) a.begin(), a.end()
#define vvl vector<vector<ll>>

typedef long long ll;

typedef long double ld;

using namespace std;

const ll maxn = 5e3 + 100;
const ll mod = 1e9 + 7;

ll n, m, k, t;

ll C[2];

string s;

ll dp0[maxn][2];

ll dp[maxn][2];

inline ll step(ll a, ll x) {
  if (x == 0) {
    return 1;
  }
  if (x % 2 == 1) {
    return step(a, x - 1) * a % mod;
  }
  ll t = step(a, x / 2);
  return t * t % mod;
}

inline ll del(ll a, ll b) { return a * step(b, mod - 2) % mod; }

void count_dp0() {
  ll q, w, e;
  // dp0[0][0] = dp0[0][1] = 1;
  for (q = 0; q < n; q++) {
    /// last are 0s;
    // dp0[q][0] = 1;
    for (w = C[0]; w <= q; w++) {
      dp0[q][0] += dp0[q - w][1];
      dp0[q][0] %= mod;
    }

    /// last are 1s;
    dp0[q][1] = 1;
    for (w = 1; w <= q; w++) {
      dp0[q][1] += dp0[q - w][0];
      dp0[q][1] %= mod;
    }
  }
}

// set <ll> S;

ll getcnt(ll len, bool f) {
  // if (S.find(len) == S.end()) {
  //     cout << "DP0 " << len << " " << dp0[len - 1][1] << endl;
  // }
  // S.insert(len);
  ll ans = dp0[len - 1][1];
  if (f) {
    ans += dp0[len - 1][0];
  }
  ans %= mod;
  return ans;
}

void count_dp() {
  ll q, w, e;
  for (q = 0; q < n; q++) {
    /// last are 0s;
    if (q + 1 < C[0]) {
      dp[q][0] = 1;
    }
    for (w = 1; w < C[0] && w <= q; w++) {
      dp[q][0] += dp[q - w][1];
      dp[q][0] %= mod;
    }
    /// last are pseudo-1s;
    if (q + 1 < C[1]) {
      dp[q][1] = getcnt(q + 1, 1);
    }
    for (w = 1; w < C[1] && w <= q; w++) {
      dp[q][1] += dp[q - w][0] * getcnt(w, q == (n - 1));
      dp[q][1] %= mod;
    }
  }
}

int main() {
  pyshnapyshnakaa;
  ll q, w, e, a, b, c;
  cin >> n >> a >> b;
  if (a > b) {
    swap(a, b);
  }
  C[0] = a;
  C[1] = b;
  count_dp0();
  count_dp();
  // cout << dp[n - 1][0] << " " << dp[n - 1][1] << " DPS" << endl;
  ll cnt_bad = dp[n - 1][0] + dp[n - 1][1];
  // cout << "CNT_BAD " << cnt_bad << endl;
  ll ans = (step(2, n) - cnt_bad % mod + mod) % mod;
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define pll pair<ll, ll>
#define mp make_pair
#define pyshnapyshnakaa                                                        \\
  ios_base ::sync_with_stdio(0);                                               \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define x first
#define y second
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define plll pair<pair<ll, ll>, ll>
#define pllll pair<pair<ll, ll>, pair<ll, ll>>
#define psl pair<string, ll>
#define all(a) a.begin(), a.end()
#define vvl vector<vector<ll>>

typedef long long ll;

typedef long double ld;

using namespace std;

const ll maxn = 5e3 + 100;
const ll mod = 1e9 + 7;

ll n, m, k, t;

ll C[2];

string s;

ll dp0[maxn][2];

ll dp[maxn][2];

inline ll step(ll a, ll x) {
  if (x == 0) {
    return 1;
  }
  if (x % 2 == 1) {
    return step(a, x - 1) * a % mod;
  }
  ll t = step(a, x / 2);
  return t * t % mod;
}

inline ll del(ll a, ll b) { return a * step(b, mod - 2) % mod; }

void count_dp0() {
  ll q, w, e;
  // dp0[0][0] = dp0[0][1] = 1;
  for (q = 0; q < n; q++) {
    /// last are 0s;
    // dp0[q][0] = 1;
    for (w = C[0]; w <= q; w++) {
      dp0[q][0] += dp0[q - w][1];
      dp0[q][0] %= mod;
    }

    /// last are 1s;
    dp0[q][1] = 1;
    for (w = 1; w <= q; w++) {
      dp0[q][1] += dp0[q - w][0];
      dp0[q][1] %= mod;
    }
  }
}

// set <ll> S;

ll getcnt(ll len, bool f) {
  // if (S.find(len) == S.end()) {
  //     cout << "DP0 " << len << " " << dp0[len - 1][1] << endl;
  // }
  // S.insert(len);
  ll ans = dp0[len - 1][1];
  if (f) {
    ans += dp0[len - 1][0];
  }
  return ans;
}

void count_dp() {
  ll q, w, e;
  for (q = 0; q < n; q++) {
    /// last are 0s;
    if (q + 1 < C[0]) {
      dp[q][0] = 1;
    }
    for (w = 1; w < C[0]; w++) {
      dp[q][0] += dp[q - w][1];
      dp[q][0] %= mod;
    }
    /// last are pseudo-1s;
    if (q + 1 < C[1]) {
      dp[q][1] = getcnt(q + 1, 1);
    }
    for (w = 1; w < C[1]; w++) {
      dp[q][1] += dp[q - w][0] * getcnt(w, q == (n - 1));
      dp[q][1] %= mod;
    }
  }
}

int main() {
  pyshnapyshnakaa;
  ll q, w, e, a, b, c;
  cin >> n >> a >> b;
  if (a > b) {
    swap(a, b);
  }
  C[0] = a;
  C[1] = b;
  count_dp0();
  count_dp();
  // cout << dp[n - 1][0] << " " << dp[n - 1][1] << " DPS" << endl;
  ll cnt_bad = dp[n - 1][0] + dp[n - 1][1];
  // cout << "CNT_BAD " << cnt_bad << endl;
  ll ans = (step(2, n) - cnt_bad % mod + mod) % mod;
  cout << ans;
  return 0;
}