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

// Template {{{
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
typedef long long LL;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  for (int i = 0; i < v.size(); i++)
    os << v[i] << (i + 1 < v.size() ? ", " : "");
  os << "}";
  return os;
}

class range {
  struct Iterator {
    int val, inc;
    int operator*() { return val; }
    bool operator!=(Iterator &rhs) { return val < rhs.val; }
    void operator++() { val += inc; }
  };
  Iterator i, n;

public:
  range(int e) : i({0, 1}), n({e, 1}) {}
  range(int b, int e) : i({b, 1}), n({e, 1}) {}
  range(int b, int e, int inc) : i({b, inc}), n({e, inc}) {}
  Iterator &begin() { return i; }
  Iterator &end() { return n; }
};

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
inline bool valid(int x, int w) { return 0 <= x && x < w; }

void iostream_init() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.setf(ios::fixed);
  cout.precision(12);
}
//}}}

static const unsigned MODVAL = 1000000007;
struct mint {
  unsigned val;
  mint() : val(0) {}
  mint(int x) : val(x % MODVAL) {}
  mint(unsigned x) : val(x % MODVAL) {}
  mint(LL x) : val(x % MODVAL) {}
};
mint &operator+=(mint &x, mint y) { return x = x.val + y.val; }
mint &operator-=(mint &x, mint y) { return x = x.val - y.val + MODVAL; }
mint &operator*=(mint &x, mint y) { return x = LL(x.val) * y.val; }
mint operator+(mint x, mint y) { return x += y; }
mint operator-(mint x, mint y) { return x -= y; }
mint operator*(mint x, mint y) { return x *= y; }
int main() {
  iostream_init();
  int N, L;
  while (cin >> N >> L) {
    vector<int> x(N);
    vector<int> a(N);
    REP(i, N) cin >> x[i];
    REP(i, N) cin >> a[i];
    vector<mint> dp(L);
    vector<mint> sum(L + 1);
    for (int d = x[0]; d < L; d += a[0]) {
      dp[d] = 1;
      if (a[0] == 0)
        break;
    }
    REP(i, L) { sum[i + 1] = sum[i] + dp[i]; }

    for (int k = 1; k < N; k++) {
      vector<mint> ndp(L);

      for (int d = x[k]; d < L; d += a[k]) {
        ndp[d] += sum[d];
        if (a[k] == 0)
          break;
      }

      dp.swap(ndp);
      sum[0] = 0;
      REP(i, L) { sum[i + 1] = sum[i] + dp[i]; }
    }

    cout << sum[L].val << endl;
  }

  return 0;
}

/* vim:set foldmethod=marker: */

Error: TLE

Fixed submission:

// Template {{{
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
typedef long long LL;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  for (int i = 0; i < v.size(); i++)
    os << v[i] << (i + 1 < v.size() ? ", " : "");
  os << "}";
  return os;
}

class range {
  struct Iterator {
    int val, inc;
    int operator*() { return val; }
    bool operator!=(Iterator &rhs) { return val < rhs.val; }
    void operator++() { val += inc; }
  };
  Iterator i, n;

public:
  range(int e) : i({0, 1}), n({e, 1}) {}
  range(int b, int e) : i({b, 1}), n({e, 1}) {}
  range(int b, int e, int inc) : i({b, inc}), n({e, inc}) {}
  Iterator &begin() { return i; }
  Iterator &end() { return n; }
};

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
inline bool valid(int x, int w) { return 0 <= x && x < w; }

void iostream_init() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.setf(ios::fixed);
  cout.precision(12);
}
//}}}

static const unsigned MODVAL = 1000000007;
struct mint {
  unsigned val;
  mint() : val(0) {}
  mint(int x) : val(x % MODVAL) {}
  mint(unsigned x) : val(x % MODVAL) {}
  mint(LL x) : val(x % MODVAL) {}
};
mint &operator+=(mint &x, mint y) { return x = x.val + y.val; }
mint &operator-=(mint &x, mint y) { return x = x.val - y.val + MODVAL; }
mint &operator*=(mint &x, mint y) { return x = LL(x.val) * y.val; }
mint operator+(mint x, mint y) { return x += y; }
mint operator-(mint x, mint y) { return x -= y; }
mint operator*(mint x, mint y) { return x *= y; }
int main() {
  iostream_init();
  int N, L;
  while (cin >> N >> L) {
    vector<int> x(N);
    vector<int> a(N);
    REP(i, N) cin >> x[i];
    REP(i, N) cin >> a[i];
    vector<mint> dp(L);
    vector<mint> sum(L + 1);
    dp[x[0]] = 1;
    for (int d = a[0]; x[0] + d < L; d += a[0]) {
      dp[x[0] + d] = 1;
    }
    REP(i, L) { sum[i + 1] = sum[i] + dp[i]; }

    for (int k = 1; k < N; k++) {
      vector<mint> ndp(L);

      for (int d = x[k]; d < L; d += a[k]) {
        ndp[d] += sum[d];
      }

      dp.swap(ndp);
      sum[0] = 0;
      REP(i, L) { sum[i + 1] = sum[i] + dp[i]; }
    }

    cout << sum[L].val << endl;
  }

  return 0;
}

/* vim:set foldmethod=marker: */