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

#include <bits/stdc++.h>

using namespace std;

namespace {

typedef double real;
typedef long long ll;

template <class T> ostream &operator<<(ostream &os, const vector<T> &vs) {
  if (vs.empty())
    return os << "[]";
  os << "[" << vs[0];
  for (int i = 1; i < vs.size(); i++)
    os << " " << vs[i];
  return os << "]";
}
template <class T> istream &operator>>(istream &is, vector<T> &vs) {
  for (auto it = vs.begin(); it != vs.end(); it++)
    is >> *it;
  return is;
}

const ll MOD = ll(1e9 + 7);

int N, L;
vector<int> X, A;
void input() {
  cin >> N >> L;
  X.clear();
  X.resize(N);
  cin >> X;
  A.clear();
  A.resize(N);
  cin >> A;
}

void solve() {
  auto dp = vector<vector<ll>>(2, vector<ll>(L, 0));
  auto sum = vector<vector<ll>>(2, vector<ll>(L + 1, 0));
  for (ll i = 0; i <= L - 1; i++) {
    const ll x = X[0];
    const ll a = A[0];
    if (a == 0) {
      if (i == x) {
        dp[0][i] = 1;
      }
    } else if (i - x >= 0 && (i - x) % a == 0) {
      dp[0][i] = 1;
    }
    sum[0][i + 1] = (sum[0][i] + dp[0][i]) % MOD;
  }
  for (int i = 1; i <= N - 1; i++) {
    swap(dp[0], dp[1]);
    fill(dp[0].begin(), dp[0].end(), 0);
    swap(sum[0], sum[1]);
    fill(sum[0].begin(), sum[0].end(), 0);
    const ll x = X[i];
    const ll a = A[i];
    for (ll j = 0; j <= L - 1; j++) {
      if (a == 0) {
        if (j == x) {
          dp[0][j] = sum[1][j];
        }
      } else if (j - x >= 0 && (j - x) % a == 0) {
        assert(j - 1 >= 0);
        dp[0][j] = sum[1][j];
      }
      sum[0][j + 1] = (sum[0][j] + dp[0][j]) % MOD;
    }
  }
  // cout << sum << endl;
  cout << sum[0][L] << endl;
}
} // namespace

int main() {
  input();
  solve();
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

namespace {

typedef double real;
typedef long long ll;

template <class T> ostream &operator<<(ostream &os, const vector<T> &vs) {
  if (vs.empty())
    return os << "[]";
  os << "[" << vs[0];
  for (int i = 1; i < vs.size(); i++)
    os << " " << vs[i];
  return os << "]";
}
template <class T> istream &operator>>(istream &is, vector<T> &vs) {
  for (auto it = vs.begin(); it != vs.end(); it++)
    is >> *it;
  return is;
}

const ll MOD = ll(1e9 + 7);

int N, L;
vector<int> X, A;
void input() {
  cin >> N >> L;
  X.clear();
  X.resize(N);
  cin >> X;
  A.clear();
  A.resize(N);
  cin >> A;
}

void solve() {
  auto dp = vector<vector<ll>>(N, vector<ll>(L, 0));
  auto sum = vector<vector<ll>>(N, vector<ll>(L + 1, 0));
  for (ll i = 0; i <= L - 1; i++) {
    const ll x = X[0];
    const ll a = A[0];
    if (a == 0) {
      if (i == x) {
        dp[0][i] = 1;
      }
    } else if (i - x >= 0 && (i - x) % a == 0) {
      dp[0][i] = 1;
    }
    sum[0][i + 1] = (sum[0][i] + dp[0][i]) % MOD;
  }
  for (int i = 1; i <= N - 1; i++) {
    const ll x = X[i];
    const ll a = A[i];
    for (ll j = 0; j <= L - 1; j++) {
      if (a == 0) {
        if (j == x) {
          dp[i][j] = sum[i - 1][j];
        }
      } else if (j - x >= 0 && (j - x) % a == 0) {
        assert(j - 1 >= 0);
        dp[i][j] = sum[i - 1][j];
      }
      sum[i][j + 1] = (sum[i][j] + dp[i][j]) % MOD;
    }
  }
  // cout << sum << endl;
  cout << sum[N - 1][L] << endl;
}
} // namespace

int main() {
  input();
  solve();
  return 0;
}