Problem p01769 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef pair<ll, P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep1(i, x) for (ll i = 1; i <= x; i++)
#define rrep(i, x) for (ll i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (ll i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define min_3(a, b, c) min(a, min(b, c))
#define max_3(a, b, c) max(a, max(b, c))
#define mp1(a, b, c) P1(a, P(b, c))
#define pque(a) priority_queue<a>
#define rpque(a) priority_queue<a, vector<a>, greater<a>>

const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                         {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const int kaijou[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};

const ll M = 1000000007;

int main() {
  static ll n, l;
  static ll x[5002], a[5002];
  scanf("%lld%lld", &n, &l);
  rep(i, n) { scanf("%lld", &x[i]); }
  rep(i, n) { scanf("%lld", &a[i]); }

  static ll dp[2][5002] = {};
  int t = 0, t_ = 1;
  dp[t][l] = 1;
  rrep(i, n) {
    rep(j, 5002) { dp[t_][j] = 0; }
    rrep(j, l) {
      dp[t_][j] = dp[t_][j + 1] + dp[t][j + 1];
      dp[t_][j] %= M;
    }
    rep(j, 5002) {
      if (j < x[i] || j > l - 1)
        dp[t_][j] = 0;
      else if (a[i] == 0) {
        if (j != x[i])
          dp[t_][j] = 0;
      } else {
        if ((j - x[i]) % a[i] != 0)
          dp[t_][j] = 0;
      }
    }
    swap(t, t_);
  }

  /*rep(i,n){
          rep(j,l){
                  printf("%lld ",dp[i][j]);
          }puts("");
  }*/

  ll ret = 0;
  rep(i, 5002) { ret += dp[t][i]; }
  cout << ret % M << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef pair<ll, P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep1(i, x) for (ll i = 1; i <= x; i++)
#define rrep(i, x) for (ll i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (ll i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define min_3(a, b, c) min(a, min(b, c))
#define max_3(a, b, c) max(a, max(b, c))
#define mp1(a, b, c) P1(a, P(b, c))
#define pque(a) priority_queue<a>
#define rpque(a) priority_queue<a, vector<a>, greater<a>>

const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                         {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const int kaijou[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};

const ll M = 1000000007;

int main() {
  static ll n, l;
  static ll x[5002], a[5002];
  scanf("%lld%lld", &n, &l);
  rep(i, n) { scanf("%lld", &x[i]); }
  rep(i, n) { scanf("%lld", &a[i]); }

  static ll dp[5002][5002] = {};
  dp[n][l] = 1;
  rrep(i, n) {
    rrep(j, l) {
      dp[i][j] = dp[i][j + 1] + dp[i + 1][j + 1];
      dp[i][j] %= M;
    }
    rep(j, 5002) {
      if (j < x[i] || j > l - 1)
        dp[i][j] = 0;
      else if (a[i] == 0) {
        if (j != x[i])
          dp[i][j] = 0;
      } else {
        if ((j - x[i]) % a[i] != 0)
          dp[i][j] = 0;
      }
    }
  }

  /*rep(i,n){
          rep(j,l){
                  printf("%lld ",dp[i][j]);
          }puts("");
  }*/

  ll ret = 0;
  rep(i, 5002) { ret += dp[0][i]; }
  cout << ret % M << endl;
}