alexjercan/bugnet huggingface dataset:
Problem p01768 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;
map<string, int> cost;
multimap<string, string> rule;
set<string> his, his2;
int flg;

void update(string a) {
  his2.insert(a);
  multimap<string, string>::iterator it = rule.find(a);
  while (it != rule.end() && it->first == a) {
    if (his2.count(it->second) == 0 && cost[it->second] > cost[a])
      cost[it->second] = cost[a], update(it->second);
    it++;
  }
}

int mk_rule(string a) {
  if (his.count(a) == 0) {
    his.insert(a);
    multimap<string, string>::iterator it = rule.find(a);
    while (it != rule.end() && it->first == a) {
      cost[a] = min(cost[a], mk_rule(it->second));
      it++;
    }
    his2.clear();
    update(a);
    it = rule.find(a);
    while (it != rule.end() && it->first == a) {
      if (cost[it->second] > cost[a])
        exit(1);
      it++;
    }
  }
  return cost[a];
}

int main() {
  int n, m, b;
  cin >> n;
  string a, c, ls[5002];
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    cost[a] = b, ls[i] = a;
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> c;
    rule.insert(pair<string, string>(a, c));
    rule.insert(pair<string, string>(c, a));
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    ans += mk_rule(ls[i]);
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;
map<string, int> cost;
multimap<string, string> rule;
set<string> his, his2;
int flg;

void update(string a) {
  his2.insert(a);
  multimap<string, string>::iterator it = rule.find(a);
  while (it != rule.end() && it->first == a) {
    if (his2.count(it->second) == 0 && cost[it->second] > cost[a])
      cost[it->second] = cost[a], update(it->second);
    it++;
  }
}

int mk_rule(string a) {
  if (his.count(a) == 0) {
    his.insert(a);
    multimap<string, string>::iterator it = rule.find(a);
    while (it != rule.end() && it->first == a) {
      cost[a] = min(cost[a], mk_rule(it->second));
      it++;
    }
    update(a);
    it = rule.find(a);
    while (it != rule.end() && it->first == a) {
      if (cost[it->second] > cost[a])
        exit(1);
      it++;
    }
  }
  return cost[a];
}

int main() {
  int n, m, b;
  cin >> n;
  string a, c, ls[5002];
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    cost[a] = b, ls[i] = a;
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> c;
    rule.insert(pair<string, string>(a, c));
    rule.insert(pair<string, string>(c, a));
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    ans += mk_rule(ls[i]);
  cout << ans << endl;
  return 0;
}
Problem p01768 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, N) for (ll i = 0; i < N; ++i)
#define FOR(i, a, b) for (ll i = a; i < b; ++i)
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

vector<bool> d;
vector<vector<ll>> r;

int dfs(int roc) {
  if (d[roc])
    return 0;
  int res = 1;
  d[roc] = true;
  REP(i, r[roc].size()) {
    if (!d[r[roc][i]]) {
      res += dfs(r[roc][i]);
    }
  }
  return res;
}

int main(void) {
  ll n, m;
  cin >> n;
  vector<pair<ll, string>> c(n);
  REP(i, n) {
    string gomi;
    ll hoge;
    cin >> gomi >> hoge;
    c[i] = {hoge, gomi};
  }
  sort(ALL(c));
  map<string, ll> s;
  REP(i, n) { s[c[i].second] = i; }
  d.resize(n);
  REP(i, n) d[i] = false;
  cin >> m;
  r.resize(n);
  REP(i, m) {
    string foo1, foo2;
    cin >> foo1 >> foo2;
    r[s[foo1]].pb(s[foo2]);
    r[s[foo2]].pb(s[foo1]);
  }
  ll ans = 0;
  REP(i, n) {
    if (!d[i]) {
      int buf = dfs(i);
      ans += buf * c[i].first;
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, N) for (ll i = 0; i < N; ++i)
#define FOR(i, a, b) for (ll i = a; i < b; ++i)
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

vector<bool> d;
vector<vector<ll>> r;

int dfs(int roc) {
  if (d[roc])
    return 0;
  int res = 1;
  d[roc] = true;
  REP(i, r[roc].size()) {
    if (!d[r[roc][i]]) {
      res += dfs(r[roc][i]);
    }
  }
  return res;
}

int main(void) {
  while (true) {
    ll n, m;
    cin >> n;
    vector<pair<ll, string>> c(n);
    REP(i, n) {
      string gomi;
      ll hoge;
      cin >> gomi >> hoge;
      c[i] = {hoge, gomi};
    }
    sort(ALL(c));
    map<string, ll> s;
    REP(i, n) { s[c[i].second] = i; }
    d.resize(n);
    REP(i, n) d[i] = false;
    cin >> m;
    r.resize(n);
    REP(i, m) {
      string foo1, foo2;
      cin >> foo1 >> foo2;
      r[s[foo1]].pb(s[foo2]);
      r[s[foo2]].pb(s[foo1]);
    }
    ll ans = 0;
    REP(i, n) {
      if (!d[i]) {
        int buf = dfs(i);
        ans += buf * c[i].first;
      }
    }
    cout << ans << endl;
  }
}
Problem p01768 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;
map<string, int> cost;
multimap<string, string> rule;
set<string> his;
void update(string a) {
  multimap<string, string>::iterator it = rule.find(a);
  while (it != rule.end() && it->first == a) {
    if (cost[it->second] > cost[a])
      cost[it->second] = cost[a], update(it->second);
    it++;
  }
}
int mk_rule(string a) {
  if (his.count(a) == 0) {
    his.insert(a);
    multimap<string, string>::iterator it = rule.find(a);
    while (it != rule.end() && it->first == a)
      cost[a] = min(cost[a], mk_rule(it->second)), it++;
    update(a);
  }
  return cost[a];
}
int main() {
  int n, m, b;
  cin >> n;
  string a, c, ls[5002];
  for (int i = 0; i < n; i++)
    cin >> ls[i] >> b, cost[ls[i]] = b;

  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> c;
    rule.insert(pair<string, string>(a, c));
    rule.insert(pair<string, string>(c, a));
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    ans += mk_rule(ls[i]);
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;
map<string, int> cost;
multimap<string, string> rule;
set<string> his;
void update(string a) {
  multimap<string, string>::iterator it = rule.find(a);
  while (it != rule.end() && it->first == a)
    if (cost[it++->second] > cost[a])
      cost[it->second] = cost[a], update(it->second);
}
int mk_rule(string a, int mae) {
  if (his.count(a) == 0 || cost[a] > mae) {
    his.insert(a);
    multimap<string, string>::iterator it = rule.find(a);
    while (it != rule.end() && it->first == a)
      cost[a] = min(cost[a], mk_rule(it++->second, cost[a]));
  }
  return cost[a];
}
int main() {
  int n, m, b;
  cin >> n;
  string a, c, ls[5002];
  for (int i = 0; i < n; i++)
    cin >> ls[i] >> b, cost[ls[i]] = b;

  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> c;
    rule.insert(pair<string, string>(a, c));
    rule.insert(pair<string, string>(c, a));
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    ans += mk_rule(ls[i], cost[ls[i]]), update(ls[i]);
  cout << ans << endl;
  return 0;
}
Problem p01768 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int N, M;
map<string, int> node;
multimap<int, string> rnode;
map<string, vector<string>> graph;
set<string> used;
int dfs(string name, int cost) {
  if (used.find(name) != used.end())
    return (node.find(name)->second);
  used.insert(name);
  node.find(name)->second = cost;
  if (graph.find(name) != graph.end()) {
    vector<string>::iterator Begin = graph.find(name)->second.begin();
    vector<string>::iterator End = graph.find(name)->second.end();
    for (; Begin != End; ++Begin)
      dfs(*Begin, cost);
  }
  return (cost);
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    string S;
    int V;
    cin >> S >> V;
    node.insert(make_pair(S, V));
    rnode.insert(make_pair(V, S));
  }
  cin >> M;
  for (int i = 0; i < M; i++) {
    string U, V;
    cin >> U >> V;
    graph[U].push_back(V);
    graph[V].push_back(U);
  }
  int ret = 0;
  for (multimap<int, string>::iterator it = rnode.begin(); it != rnode.end();
       ++it) {
    ret += dfs(it->second, it->first);
  }
  cout << ret << endl;
  return (0);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int N, M;
map<string, int> node;
map<string, vector<string>> graph;
set<string> used;
int rec(string name) {
  if (used.find(name) != used.end())
    return (node.find(name)->second);
  used.insert(name);
  int ret = node.find(name)->second;
  if (graph.find(name) != graph.end()) {
    vector<string>::iterator Begin = graph.find(name)->second.begin();
    vector<string>::iterator End = graph.find(name)->second.end();
    for (; Begin != End; ++Begin)
      ret = min(rec(*Begin), ret);
  }
  used.erase(name);
  return (node.find(name)->second = ret);
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    string S;
    int V;
    cin >> S >> V;
    node.insert(make_pair(S, V));
  }
  cin >> M;
  for (int i = 0; i < M; i++) {
    string U, V;
    cin >> U >> V;
    graph[U].push_back(V);
    graph[V].push_back(U);
  }
  int ret = 0;
  for (map<string, int>::iterator it = node.begin(); it != node.end(); ++it) {
    ret += rec(it->first);
  }
  cout << ret << endl;
  return (0);
}
Problem p01769 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

static const int MOD = 1000000007;
static int dp[5001][5001];

int main() {
  ios_base::sync_with_stdio(false);
  int n, l;
  cin >> n >> l;
  vector<int> x(n), a(n);
  for (int i = 0; i < n; ++i) {
    cin >> x[i];
  }
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  memset(dp, 0, sizeof(dp));
  dp[n][l] = 1;
  for (int i = n - 1; i >= 0; --i) {
    int sum = dp[i + 1][l];
    for (int j = l - 1; j >= x[i]; --j) {
      if (a[i] == 0) {
        if (j == x[i]) {
          dp[i][j] = (dp[i][j] + sum) % MOD;
        }
      } else if ((j - x[i]) % a[i] == 0) {
        dp[i][j] = (dp[i][j] + sum) % MOD;
      }
      sum = (sum + dp[i + 1][j]) % MOD;
    }
  }
  int answer = 0;
  for (int i = 0; i <= l; ++i) {
    answer = (answer + dp[0][i]) % MOD;
  }
  cout << answer << endl;
  return 0;
}

Error: 127
/tmp/c54e2686-767a-44b2-ae52-59a56130571f.out: error while loading shared libraries: libc.so.6: failed to map segment from shared object


Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

static const int MOD = 1000000007;
static int dp[5001][5001];

int main() {
  ios_base::sync_with_stdio(false);
  int n, l;
  cin >> n >> l;
  vector<int> x(n), a(n);
  for (int i = 0; i < n; ++i) {
    cin >> x[i];
  }
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  memset(dp, 0, sizeof(dp));
  dp[n][l] = 1;
  for (int i = n - 1; i >= 0; --i) {
    int sum = dp[i + 1][l];
    for (int j = l - 1; j >= x[i]; --j) {
      if ((j - x[i]) % a[i] == 0) {
        dp[i][j] = (dp[i][j] + sum) % MOD;
      }
      sum = (sum + dp[i + 1][j]) % MOD;
    }
  }
  int answer = 0;
  for (int i = 0; i <= l; ++i) {
    answer = (answer + dp[0][i]) % MOD;
  }
  cout << answer << endl;
  return 0;
}
Problem p01769 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(c) begin(c), end(c)
#define dump(x) cerr << __LINE__ << ":\\t" #x " = " << (x) << endl

const int MOD = 1000000007;
int n, l;
int x[5010], a[5010];

int dp[5010][5010];

signed main() {
  while (cin >> n >> l) {
    rep(i, n) cin >> x[i], ++x[i];
    rep(i, n) cin >> a[i], a[i] ? 0 : a[i] = l;
    memset(dp, 0, sizeof dp);
    for (int i = 0; i <= l; ++i)
      dp[0][i] = 1;
    for (int i = 1; i <= n; ++i) {
      int a = ::a[i - 1], x = ::x[i - 1];
      for (int j = x; j <= l; ++j) {
        if (j == x) {
          dp[i][j] = dp[i - 1][j - 1];
        } else {
          if ((j - x) % a == 0) {
            dp[i][j] += dp[i - 1][j - 1];
          }
          dp[i][j] += dp[i][j - 1];
          dp[i][j] %= MOD;
        }
      }
    }

    // for(int i = 0; i <= n; ++i){
    //     for(int j = 0; j <= l; ++j){
    //         cout << dp[i][j] << ' ';
    //     }
    //     cout << endl;
    // }

    cout << dp[n][l] << endl;
  }
}

Error: MLE

Fixed submission:

long long dp[5000][5000];
int main() {
  for (int i = 0; i < 5000; i++)
    for (int j = 0; j < 5000; j++)
      dp[i][j] = 1;
}
Problem p01769 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
#define REP(i, N) for (int i = 0; i < N; ++i)
#define REPA(i, N) for (int i = 1; i < N; ++i)
#define MOD ((int)1e9 + 7)
using namespace std;
int main() {
  int N, L;
  cin >> N >> L;
  vector<int> sp(N), ran(N);
  REP(i, N) { cin >> sp[i]; }
  REP(i, N) { cin >> ran[i]; }
  vector<vector<int>> dp(N, vector<int>(L, 0));
  for (int i = sp[0]; i < L; i += ran[0]) {
    dp[0][i] = 1;
    if (ran[0] == 0)
      break;
  }
  REPA(i, N) {
    for (int j = sp[i]; j < L; j += ran[i]) {
      int sum = 0;
      if (ran[i] == 0) {
        REP(k, sp[i]) { sum = (sum + dp[i - 1][k]) % MOD; }
        dp[i][j] = sum;
        break;
      } else if (j == sp[i]) {
        REP(k, sp[i]) { sum = (sum + dp[i - 1][k]) % MOD; }
        dp[i][j] = sum;
      } else {
        for (int k = j - ran[i]; k < j; ++k) {
          sum = (sum + dp[i - 1][k]) % MOD;
        }
        dp[i][j] = (dp[i][j - ran[i]] + sum) % MOD;
      }
    }
  }
  int ans = 0;
  REP(i, L) { ans = (ans + dp[N - 1][i]) % MOD; }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define REP(i, N) for (int i = 0; i < N; ++i)
#define REPA(i, N) for (int i = 1; i < N; ++i)
#define MOD ((int)1e9 + 7)
using namespace std;
int main() {
  int N, L;
  cin >> N >> L;
  vector<int> sp(N), ran(N);
  REP(i, N) { cin >> sp[i]; }
  REP(i, N) { cin >> ran[i]; }
  vector<vector<int>> dp(N, vector<int>(L, 0));
  for (int i = sp[0]; i < L; i += ran[0]) {
    dp[0][i] = 1;
  }
  REPA(i, N) {
    for (int j = sp[i]; j < L; j += ran[i]) {
      int sum = 0;
      if (ran[i] == 0) {
        REP(k, sp[i]) { sum = (sum + dp[i - 1][k]) % MOD; }
        dp[i][j] = sum;
        break;
      } else if (j == sp[i]) {
        REP(k, sp[i]) { sum = (sum + dp[i - 1][k]) % MOD; }
        dp[i][j] = sum;
      } else {
        for (int k = j - ran[i]; k < j; ++k) {
          sum = (sum + dp[i - 1][k]) % MOD;
        }
        dp[i][j] = (dp[i][j - ran[i]] + sum) % MOD;
      }
    }
  }
  int ans = 0;
  REP(i, L) { ans = (ans + dp[N - 1][i]) % MOD; }
  cout << ans << endl;
  return 0;
}
Problem p01769 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)

int r[5005][5005];
int n, k;
int pos[5005], a[5005];
bool ok(int a, int b) {
  if (b == 0)
    return !a;
  return (a % b == 0);
}
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> pos[i];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 0; i < k; i++) {
    int x = (i >= pos[1] && ok(i - pos[1], a[1]));
    if (i == 0)
      r[1][i] = x;
    else
      r[1][i] = r[1][i - 1] + x;
  }
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      int x = 0;
      if (j >= pos[i] && ok(j - pos[i], a[i]))
        x = r[i - 1][j - 1];
      if (j == 0)
        r[i][j] = x;
      else
        r[i][j] = (r[i][j - 1] + x) % mod;
    }
  }
  ll res = 0;
  cout << r[n][k - 1] << endl;
}

Error: 127
/tmp/28f29d61-fa54-49e0-a36b-4745e0f8b571.out: error while loading shared libraries: libc.so.6: failed to map segment from shared object


Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)

int r[5005][5005];
int n, k;
int pos[5005], a[5005];

int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> pos[i];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 0; i < k; i++) {
    int x = (i >= pos[1] && (i - pos[1]) % a[1] == 0);
    if (i == 0)
      r[1][i] = x;
    else
      r[1][i] = r[1][i - 1] + x;
  }
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      int x = 0;
      if (j >= pos[i] && (j - pos[i]) % a[i] == 0)
        x = r[i - 1][j - 1];
      if (j == 0)
        r[i][j] = x;
      else
        r[i][j] = (r[i][j - 1] + x) % mod;
    }
  }
  ll res = 0;
  cout << r[n][k - 1] << endl;
}
Problem p01769 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int N, L;
int xx[5000];
int x[5000];
int a[5000];
const ll M = 1000000007LL;
ll sum[5001];
ll dp[5000];

int main() {
  cin >> N >> L;
  for (int i = 0; i < N; ++i) {
    cin >> x[i];
    xx[i] = x[i];
  }
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
  }
  int pre = L;
  for (int i = N - 1; i >= 0; --i) {
    if (a[i] != 0) {
      int r = (pre - 1 - x[i]) / a[i];
      x[i] += a[i] * r;
    }
    pre = x[i];
  }

  memset(dp, 0, sizeof(dp));
  for (int i = 0; x[0] - a[0] * i >= xx[0]; ++i) {
    dp[x[0] - a[0] * i] = 1;
    if (a[0] == 0)
      break;
  }
  for (int i = 1; i < N; ++i) {
    sum[0] = 0;
    for (int j = 1; j <= x[i]; ++j) {
      sum[j] = (sum[j - 1] + dp[j - 1]) % M;
    }
    memset(dp, 0, sizeof(dp));
    for (int j = 0; x[i] - a[i] * j >= xx[i]; ++j) {
      int id = x[i] - a[i] * j;
      dp[id] = sum[id];
      if (a[i] == 0)
        break;
    }
  }
  ll ans = 0;
  for (int i = 0; i < L; ++i) {
    ans = (ans + dp[i]) % M;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int N, L;
int x[5000];
int a[5000];
const ll M = 1000000007LL;
ll sum[5001];
ll dp[5000];

int main() {
  cin >> N >> L;
  for (int i = 0; i < N; ++i) {
    cin >> x[i];
  }
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
  }
  int pre = L;
  for (int i = N - 1; i >= 0; --i) {
    if (a[i] != 0) {
      int r = (pre - 1 - x[i]) / a[i];
      x[i] += a[i] * r;
    }
    pre = x[i];
  }

  memset(dp, 0, sizeof(dp));
  for (int i = 0; x[0] - a[0] * i >= 0; ++i) {
    dp[x[0] - a[0] * i] = 1;
  }
  for (int i = 0; i < N - 1; ++i) {
    sum[0] = 0;
    for (int j = 1; j <= x[i + 1]; ++j) {
      sum[j] = (sum[j - 1] + dp[j - 1]) % M;
    }
    memset(dp, 0, sizeof(dp));
    for (int j = 0; x[i + 1] - a[i + 1] * j >= 0; ++j) {
      int id = x[i + 1] - a[i + 1] * j;
      dp[id] = sum[id];
      if (a[i + 1] == 0) {
        break;
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < L; ++i) {
    ans = (ans + dp[i]) % M;
  }
  cout << ans << endl;
}
Problem p01769 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define repi(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) repi(i, 0, n)
using namespace std;
using ll = long long;
const int INF = 1 << 30;
const int MOD = 1e9 + 7;

#define int long long

#define MAX 5100

#define x first
#define a second

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, L;
  vector<pair<int, int>> t, z;
  static int dp[MAX][MAX];
  // vector<vector<int>> dp;
  int ans = 0;

  cin >> N >> L;

  t.resize(N + 10);
  // dp.resize(N+5,vector<int>(L+5,0));

  for (int i = 0; i < N; i++) {
    cin >> t[i].x;
  }
  for (int i = 0; i < N; i++) {
    cin >> t[i].a;
  }

  for (int i = N - 1; i >= 0; i--) {
    z.push_back(t[i]);
  }

  for (int i = z[0].x; i < L; i += z[0].a) {
    dp[0][i] = 1;
    // cout<<0<<" "<<i<<" "<<dp[0][i]<<endl;
    if (z[0].a == 0)
      break;
  }

  for (int i = 1; i < N; i++) {
    for (int j = L - i, sum = 0; j >= z[i].x; j--) {
      if (z[i].a == 0) {
        if (z[i].x == j) {
          dp[i][j] = sum % MOD;
        }
      } else if ((j - z[i].x) % z[i].a == 0) {
        dp[i][j] = sum % MOD;
        // cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
      }
      sum += dp[i - 1][j];
      sum %= MOD;
    }
  }

  for (int i = 0; i < L; i++) {
    ans += dp[N - 1][i];
    ans %= MOD;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define repi(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) repi(i, 0, n)
using namespace std;
using ll = long long;
const int INF = 1 << 30;
const int MOD = 1e9 + 7;

#define int long long

#define MAX 5100

#define x first
#define a second

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, L;
  vector<pair<int, int>> t, z;
  static int dp[MAX][MAX];
  // vector<vector<int>> dp;
  int ans = 0;

  cin >> N >> L;

  t.resize(N + 10);
  // dp.resize(N+5,vector<int>(L+5,0));

  for (int i = 0; i < N; i++) {
    cin >> t[i].x;
  }
  for (int i = 0; i < N; i++) {
    cin >> t[i].a;
  }

  for (int i = N - 1; i >= 0; i--) {
    z.push_back(t[i]);
  }

  for (int i = z[0].x; i < L; i += z[0].a) {
    dp[0][i] = 1;
    // cout<<0<<" "<<i<<" "<<dp[0][i]<<endl;
  }

  for (int i = 1; i < N; i++) {
    for (int j = L - i, sum = 0; j >= z[i].x; j--) {
      if ((j - z[i].x) % z[i].a == 0) {
        dp[i][j] = sum % MOD;
        // cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
      }
      sum += dp[i - 1][j];
      sum %= MOD;
    }
  }

  for (int i = 0; i < L; i++) {
    ans += dp[N - 1][i];
    ans %= MOD;
  }

  cout << ans << endl;

  return 0;
}