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

#include <bits/stdc++.h>
// #define int long long

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

using namespace std;
#define pb push_back
#define ll long long
#define fi first
#define se second
#define vi vector<int>
#define pii pair<int, int>

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int dp[3005][3005], a, b, c, d;
const int mod = 998244353;
bool vis[3005][3005];
int pw[3005][2], pr[3005][3005], P[3004][3003];
int pow(int a, int n) {
  if (a == 0)
    return 0;
  int y = 1;
  for (; n; n /= 2) {
    if (n & 1)
      y = 1ll * y * a % mod;
    a = 1ll * a * a % mod;
  }
  return y;
}
void solve(int a, int b) {
  if (vis[a][b])
    return;
  if (a > c || b > d) {
    vis[a][b] = 1;
    return;
  }
  solve(a, b + 1);
  solve(a + 1, b);
  if (a == c) {
    dp[a][b] = pw[d - b][0];
    vis[a][b] = 1;
    pr[b - 1][a] =
        (1ll * pr[b - 1][a + 1] + 1ll * dp[a][b] * P[b - 1][a] % mod) % mod;
    return;
  }
  if (b == d) {
    dp[a][b] = pw[c - a][1];
    vis[a][b] = 1;
    pr[b - 1][a] =
        (1ll * pr[b - 1][a + 1] + 1ll * dp[a][b] * P[b - 1][a] % mod) % mod;
    return;
  }
  // if (a == 1 && b== 2) cout <<'s';
  int x = 1;
  /*	 for (int i = a + 1; i <= c; i++) {
                  x = 1ll*x*b%mod;
                  dp[a][b] = (dp[a][b] + 1ll*dp[i][b+1]*x%mod)%mod;
           }        */
  dp[a][b] = 1ll * pr[b][a + 1] * pow(P[b][a], mod - 2) % mod;
  dp[a][b] = (1ll * dp[a][b] + 1ll * dp[a][b + 1] * a % mod) % mod;
  pr[b - 1][a] =
      (1ll * pr[b - 1][a + 1] + 1ll * dp[a][b] * P[b - 1][a] % mod) % mod;
  vis[a][b] = 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> a >> b >> c >> d;
  pw[0][0] = pw[0][1] = 1;
  for (int i = 1; i <= 3003; i++) {
    pw[i][0] = 1ll * pw[i - 1][0] * c % mod;
    pw[i][1] = 1ll * pw[i - 1][1] * d % mod;
  }
  for (int i = 1; i <= 3001; i++) {
    P[i][0] = 1;
    for (int j = 1; j <= 3001; j++) {
      P[i][j] = 1ll * P[i][j - 1] * i % mod;
    }
  }
  dp[c][d] = 1;
  pr[d - 1][c] = pow(d - 1, c);
  vis[c][d] = 1;
  solve(a, b);
  cout << (dp[a][b] % mod + mod) % mod << '\\n';
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #define int long long

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

using namespace std;
#define pb push_back
#define ll long long
#define fi first
#define se second
#define vi vector<int>
#define pii pair<int, int>

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int dp[3005][3005], a, b, c, d;
const int mod = 998244353;
bool vis[3005][3005];
int pw[3005][2], pr[3005][3005];
int pow(int a, int n) {
  if (a == 0)
    return 0;
  int y = 1;
  for (; n; n /= 2) {
    if (n & 1)
      y = 1ll * y * a % mod;
    a = 1ll * a * a % mod;
  }
  return y;
}
void solve(int a, int b) {
  if (vis[a][b])
    return;
  if (a > c || b > d) {
    vis[a][b] = 1;
    return;
  }
  solve(a, b + 1);
  solve(a + 1, b);
  if (a == c) {
    dp[a][b] = pw[d - b][0];
    vis[a][b] = 1;
    pr[b - 1][a] =
        (1ll * pr[b - 1][a + 1] + 1ll * dp[a][b] * pow(b - 1, a) % mod) % mod;
    return;
  }
  if (b == d) {
    dp[a][b] = pw[c - a][1];
    vis[a][b] = 1;
    pr[b - 1][a] =
        (1ll * pr[b - 1][a + 1] + 1ll * dp[a][b] * pow(b - 1, a) % mod) % mod;
    return;
  }
  // if (a == 1 && b== 2) cout <<'s';
  int x = 1;
  /*	 for (int i = a + 1; i <= c; i++) {
                  x = 1ll*x*b%mod;
                  dp[a][b] = (dp[a][b] + 1ll*dp[i][b+1]*x%mod)%mod;
           }        */
  dp[a][b] = 1ll * pr[b][a + 1] * pow(pow(b, a), mod - 2) % mod;
  dp[a][b] = (1ll * dp[a][b] + 1ll * dp[a][b + 1] * a % mod) % mod;
  pr[b - 1][a] =
      (1ll * pr[b - 1][a + 1] + 1ll * dp[a][b] * pow(b - 1, a) % mod) % mod;
  vis[a][b] = 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> a >> b >> c >> d;
  pw[0][0] = pw[0][1] = 1;
  for (int i = 1; i <= 3003; i++) {
    pw[i][0] = 1ll * pw[i - 1][0] * c % mod;
    pw[i][1] = 1ll * pw[i - 1][1] * d % mod;
  }
  dp[c][d] = 1;
  pr[d - 1][c] = pow(d - 1, c);
  vis[c][d] = 1;
  solve(a, b);
  cout << (dp[a][b] % mod + mod) % mod << '\\n';
}