Problem p02653 (C++) — buggy submission, verdict: Time Limit Exceeded

/* be name khoda */

// #define long_enable
#include <algorithm>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <vector>
using namespace std;

#ifdef long_enable
typedef long long int ll;
#else
typedef int ll;
#endif

typedef pair<ll, ll> pii;
typedef pair<pii, ll> ppi;
typedef pair<ll, pii> pip;
typedef vector<ll> vi;
typedef vector<pii> vpii;

#define forifrom(i, s, n) for (ll i = s; i < n; ++i)
#define forirto(i, n, e) for (ll i = (n)-1; i >= e; --i)
#define fori(i, n) forifrom(i, 0, n)
#define forir(i, n) forirto(i, n, 0)

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define smin(a, b) a = min(a, (b))
#define smax(a, b) a = max(a, (b))

#define debug(x) cout << #x << " -> " << (x) << endl
#define debug2(x, y)                                                           \\
  cout << #x << ' ' << #y << " -> " << (x) << ' ' << (y) << endl
#define debug3(x, y, z)                                                        \\
  cout << #x << ' ' << #y << ' ' << #z << " -> " << (x) << ' ' << (y) << ' '   \\
       << (z) << endl
#define debug4(x, y, z, t)                                                     \\
  cout << #x << ' ' << #y << ' ' << #z << ' ' << #t << " -> " << (x) << ' '    \\
       << (y) << ' ' << (z) << ' ' << (t) << endl
#define debuga(x, n)                                                           \\
  cout << #x << " -> ";                                                        \\
  fori(i1_da, n) { cout << (x)[i1_da] << ' '; }                                \\
  cout << endl
#define debugaa(x, n, m)                                                       \\
  cout << #x << " ->\\n";                                                       \\
  fori(i1_daa, n) {                                                            \\
    fori(i2_daa, m) { cout << (x)[i1_daa][i2_daa] << ' '; }                    \\
    cout << '\\n';                                                              \\
  }                                                                            \\
  cout << endl

const ll MOD = 1000000007;
const ll INF = 2000000000;
const long long BIG = 1446803456761533460LL;

#define XL (x << 1)
#define XR (XL | 1)
#define MD ((l + r) >> 1)
#define Add(a, b) a = ((a) + (b)) % MOD
#define Mul(a, b) a = (1LL * (a) * (b)) % MOD

// -----------------------------------------------------------------------

const ll maxn = 5010;

ll N, A, B;
ll dp1[maxn]; // B blocks starting and ending at b
ll dp2[maxn]; // complement strings ending at b
ll ps[maxn];

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

  cin >> N >> A >> B;
  if (A > B)
    swap(A, B);

  dp1[1] = 1;
  for (ll i = 2; i <= B; ++i) {
    dp1[i] = dp1[i - 1];
    for (ll j = A; i - 1 - j >= 1; ++j)
      Add(dp1[i], dp1[i - 1 - j]);
  }
  dp2[0] = 1;
  ps[1] = 1;
  for (ll i = 1; i <= N; ++i) {
    for (ll j = 1; j < min(B, i + 1); ++j) {
      if (i == j) {
        Add(dp2[i], dp1[j + 1]);
      } else {
        ll s = (ps[i - j] - ps[max(0, i - j - A + 1)] + MOD) % MOD;
        Add(dp2[i], 1LL * dp1[j] * s);
      }
    }
    ps[i + 1] = (ps[i] + dp2[i]) % MOD;
  }

  ll sum = 0;
  for (ll i = N; i > N - A; --i)
    Add(sum, dp2[i]);
  for (ll i = N - A; i - 1 > N - B; --i) {
    for (ll j = i; j - 1 > N - B; --j) {
      ll s = (ps[j - 1] - ps[max(0, j - A)] + MOD) % MOD;
      Add(sum, 1LL * dp1[i - j + 1] * s);
    }
  }

  ll all = 1;
  for (ll i = 1; i <= N; ++i)
    Add(all, all);

  ll ans = (all + MOD - sum) % MOD;
  cout << ans << '\\n';
}


Error: TLE

Fixed submission:

/* be name khoda */

// #define long_enable
#include <algorithm>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <vector>
using namespace std;

#ifdef long_enable
typedef long long int ll;
#else
typedef int ll;
#endif

typedef pair<ll, ll> pii;
typedef pair<pii, ll> ppi;
typedef pair<ll, pii> pip;
typedef vector<ll> vi;
typedef vector<pii> vpii;

#define forifrom(i, s, n) for (ll i = s; i < n; ++i)
#define forirto(i, n, e) for (ll i = (n)-1; i >= e; --i)
#define fori(i, n) forifrom(i, 0, n)
#define forir(i, n) forirto(i, n, 0)

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define smin(a, b) a = min(a, (b))
#define smax(a, b) a = max(a, (b))

#define debug(x) cout << #x << " -> " << (x) << endl
#define debug2(x, y)                                                           \\
  cout << #x << ' ' << #y << " -> " << (x) << ' ' << (y) << endl
#define debug3(x, y, z)                                                        \\
  cout << #x << ' ' << #y << ' ' << #z << " -> " << (x) << ' ' << (y) << ' '   \\
       << (z) << endl
#define debug4(x, y, z, t)                                                     \\
  cout << #x << ' ' << #y << ' ' << #z << ' ' << #t << " -> " << (x) << ' '    \\
       << (y) << ' ' << (z) << ' ' << (t) << endl
#define debuga(x, n)                                                           \\
  cout << #x << " -> ";                                                        \\
  fori(i1_da, n) { cout << (x)[i1_da] << ' '; }                                \\
  cout << endl
#define debugaa(x, n, m)                                                       \\
  cout << #x << " ->\\n";                                                       \\
  fori(i1_daa, n) {                                                            \\
    fori(i2_daa, m) { cout << (x)[i1_daa][i2_daa] << ' '; }                    \\
    cout << '\\n';                                                              \\
  }                                                                            \\
  cout << endl

const ll MOD = 1000000007;
const ll INF = 2000000000;
const long long BIG = 1446803456761533460LL;

#define XL (x << 1)
#define XR (XL | 1)
#define MD ((l + r) >> 1)
#define Add(a, b) a = ((a) + (b)) % MOD
#define Mul(a, b) a = (1LL * (a) * (b)) % MOD

// -----------------------------------------------------------------------

const ll maxn = 5010;

ll N, A, B;
ll dp1[maxn]; // B blocks starting and ending at b
ll dp2[maxn]; // complement strings ending at b

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

  cin >> N >> A >> B;
  if (A > B)
    swap(A, B);

  dp1[1] = 1;
  for (ll i = 2; i <= B; ++i) {
    dp1[i] = dp1[i - 1];
    for (ll j = A; i - 1 - j >= 1; ++j)
      Add(dp1[i], dp1[i - 1 - j]);
  }
  dp2[0] = 1;
  for (ll i = 1; i <= N; ++i) {
    for (ll j = 1; j < B; ++j) {
      if (i == j) {
        Add(dp2[i], dp1[j + 1]);
      } else {
        ll s = 0;
        for (ll k = i - j - 1; k >= 0 && (i - j) - k < A; --k) {
          Add(s, dp2[k]);
        }
        Add(dp2[i], 1LL * dp1[j] * s);
      }
    }
  }

  ll sum = 0;
  for (ll i = N; i > N - A; --i)
    Add(sum, dp2[i]);
  for (ll i = N - A; i - 1 > N - B; --i) {
    for (ll j = i; j - 1 > N - B; --j) {
      ll s = 0;
      for (ll k = j - 1; k >= 1 && j - k < A; --k) {
        Add(s, dp2[k - 1]);
      }
      Add(sum, 1LL * dp1[i - j + 1] * s);
    }
  }

  ll all = 1;
  for (ll i = 1; i <= N; ++i)
    Add(all, all);

  ll ans = (all + MOD - sum) % MOD;
  cout << ans << '\\n';
}