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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

using vvll = vector<vector<ll>>;
using vll = vector<ll>;

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define I insert
#define endl '\\n'
#define all(x) x.begin(), x.end()
#define sz(x) ((long long)x.size())

const ll maxs = 1e6 + 5;
const ll lmaxs = 20;
ll mod = 1e9 + 7;
ll oo = 1e15;

void pre() {}

int a, b, c, d;
const int N = 3005;
int vis[N][N];
int dp[N][N];

long go(int i, int j) {
  if (i > c) {
    return 0;
  }
  if (j > d) {
    return 0;
  }
  if (i == c and j == d)
    return 1;
  if (vis[i][j])
    return dp[i][j];
  vis[i][j] = true;
  long ans = j * go(i + 1, j) % mod;
  ans += i * go(i, j + 1) % mod;
  ans -= i * j * go(i + 1, j + 1) % mod;
  ans %= mod;
  ans += mod;
  ans %= mod;
  return dp[i][j] = ans;
}

void solve() {
  mod = 998244353;
  cin >> a >> b >> c >> d;
  long ans = go(a, b);
  cout << ans << endl;
}

int main() {

  IOS;
  pre();
  ll T = 1;
  // cin>>T;
  ll tt = 1;
  while (T--) {
    // cout << "Case #" << tt << ": ";
    tt++;
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

using vvll = vector<vector<ll>>;
using vll = vector<ll>;

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define I insert
#define endl '\\n'
#define all(x) x.begin(), x.end()
#define sz(x) ((long long)x.size())

const ll maxs = 1e6 + 5;
const ll lmaxs = 20;
ll mod = 1e9 + 7;
ll oo = 1e15;

void pre() {}

int a, b, c, d;
const int N = 3005;
int vis[N][N];
int dp[N][N];

long go(int i, int j) {
  if (i > c) {
    return 0;
  }
  if (j > d) {
    return 0;
  }
  if (i == c and j == d)
    return 1;
  long ans = j * go(i + 1, j) % mod;
  ans += i * go(i, j + 1) % mod;
  ans -= i * j * go(i + 1, j + 1) % mod;
  ans %= mod;
  ans += mod;
  ans %= mod;
  return ans;
}

void solve() {
  mod = 998244353;
  cin >> a >> b >> c >> d;
  long ans = go(a, b);
  cout << ans << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input00.txt", "r", stdin);
#endif
  IOS;
  pre();
  ll T = 1;
  // cin>>T;
  ll tt = 1;
  while (T--) {
    // cout << "Case #" << tt << ": ";
    tt++;
    solve();
  }
  return 0;
}