Problem p02979 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld double

#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()

#define pb emplace_back
#define X first
#define Y second

const int N = 205;

int mod;

void add(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
}
void sub(int &a, int b) {
  a -= b;
  if (a < 0)
    a += mod;
}
int mul(int a, int b) { return 1ll * a * b % mod; }
int Pow(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = mul(ans, a);
    a = mul(a, a);
    b >>= 1;
  }
  return ans;
}
int inv(int a, int p) { return a == 1 ? 1 : p - 1ll * p * inv(p % a, a) / a; }

typedef pair<int, int> ii;

int f[N][N][N];

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

  int n;
  cin >> n;
  int k;
  cin >> k >> mod;

  if (k % 2 == 0) {
    k /= 2;
    vector<int> f(n + 1, 0);
    vector<int> g(n + 1, 0);

    g[0] = 1;

    for (int i = 1; i <= n; ++i) {
      add(g[i], f[i - 1]);
      add(g[i], g[i - 1]);

      for (int j = 1; j <= k; ++j)
        if (j <= i)
          add(f[i], g[i - j]);
    }
    for (int i = 0; i <= n; ++i)
      add(f[i], g[i]);

    cout << mul(f[n / 2], f[n - n / 2]) << endl;
    return 0;
  }
  f[0][0][0] = 1;

  for (int i = 2; i <= n + k; i += 2) {
    for (int j = 0; j <= n; ++j)
      for (int t = 0; t <= k + 1; ++t)
        add(f[i][0][0], f[i - 2][j][t]);

    if (i <= n) {
      for (int j = 0; j <= n; ++j)
        for (int t = 0; t <= k + 1; ++t)
          add(f[i][j + 1][0], f[i - 2][j][t]);
    }
    if (i > k) {
      for (int j = 0; j <= n; add(f[i][0][0], f[i - 2][j++][0]))
        for (int t = 1; t <= k; ++t)
          add(f[i][0][t + 1], f[i - 2][j][t]);
    }
    if (i <= n && i > k) {
      for (int j = 0; j <= n; ++j)
        if (j < k)
          for (int t = 0; t <= k; ++t)
            add(f[i][j + 1][max(j + 2, t + 1)], f[i - 2][j][t]);
    }
  }
  int ans = 0;

  for (int i = 0; i <= n; ++i)
    for (int j = 0; j <= k + 1; ++j)
      add(ans, f[(n + k) / 2 * 2][i][j]);

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld double

#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()

#define pb emplace_back
#define X first
#define Y second

const int N = 305;

int mod;

void add(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
}
void sub(int &a, int b) {
  a -= b;
  if (a < 0)
    a += mod;
}
int mul(int a, int b) { return 1ll * a * b % mod; }
int Pow(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = mul(ans, a);
    a = mul(a, a);
    b >>= 1;
  }
  return ans;
}
int inv(int a, int p) { return a == 1 ? 1 : p - 1ll * p * inv(p % a, a) / a; }

typedef pair<int, int> ii;

int f[N][N][N];

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

  int n;
  cin >> n;
  int k;
  cin >> k >> mod;

  if (k % 2 == 0) {
    k /= 2;
    vector<int> f(n + 1, 0);
    vector<int> g(n + 1, 0);

    g[0] = 1;

    for (int i = 1; i <= n; ++i) {
      add(g[i], f[i - 1]);
      add(g[i], g[i - 1]);

      for (int j = 1; j <= k; ++j)
        if (j <= i)
          add(f[i], g[i - j]);
    }
    for (int i = 0; i <= n; ++i)
      add(f[i], g[i]);

    cout << mul(f[n / 2], f[n - n / 2]) << endl;
    return 0;
  }
  f[0][0][0] = 1;

  for (int i = 2; i <= n + k; i += 2) {
    for (int j = 0; j <= n; ++j)
      for (int t = 0; t <= k + 1; ++t)
        add(f[i][0][0], f[i - 2][j][t]);

    if (i <= n) {
      for (int j = 0; j <= n; ++j)
        for (int t = 0; t <= k + 1; ++t)
          add(f[i][j + 1][0], f[i - 2][j][t]);
    }
    if (i > k) {
      for (int j = 0; j <= n; add(f[i][0][0], f[i - 2][j++][0]))
        for (int t = 1; t <= k; ++t)
          add(f[i][0][t + 1], f[i - 2][j][t]);
    }
    if (i <= n && i > k) {
      for (int j = 0; j <= n; ++j)
        if (j < k)
          for (int t = 0; t <= k; ++t)
            add(f[i][j + 1][max(j + 2, t + 1)], f[i - 2][j][t]);
    }
  }
  int ans = 0;

  for (int i = 0; i <= n; ++i)
    for (int j = 0; j <= k + 1; ++j)
      add(ans, f[(n + k) / 2 * 2][i][j]);

  cout << ans << endl;
}