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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

int main() {

  int N, L;
  scanf("%d %d", &N, &L);

  int loc[N], move[N];

  for (int i = 0; i < N; i++)
    scanf("%d", &loc[i]);
  for (int i = 0; i < N; i++)
    scanf("%d", &move[i]);

  reverse(loc, loc + N);
  reverse(move, move + N);

  ll pre[L], current[L];

  for (int i = 0; i < L; i++)
    pre[i] = 0;

  for (int i = 0; loc[0] + i * move[0] <= L - 1; i++) {
    pre[loc[0] + i * move[0]] = 1;
    if (move[0] == 0)
      break;
  }

  for (int i = 1; i <= L - 1; i++) {
    pre[i] += pre[i - 1];
  }

  if (N == 1) {
    printf("%lld\\n", pre[L - 1]);
    return 0;
  }

  int pos, right = L - 1;

  for (int rabbit = 1; rabbit <= N - 1; rabbit++) {
    for (int i = 0; i <= L - 1; i++)
      current[i] = 0;

    if (move[rabbit] == 0) {
      current[loc[rabbit]] = pre[right] - pre[loc[rabbit]];
    } else {

      for (int i = 0; (pos = loc[rabbit] + i * move[rabbit]) < L - 1; i++) {
        current[pos] = pre[right] - pre[pos];
        if (current[pos] < 0)
          current[pos] += MOD;
      }
    }

    for (int i = 0; i <= L - 1; i++)
      pre[i] = current[i];
    for (int i = 1; i <= L - 1; i++) {
      pre[i] += pre[i - 1];
      pre[i] %= MOD;
    }
  }

  ll ans = 0;
  for (int i = 0; i < L; i++) {
    ans += current[i];
    ans %= MOD;
  }

  printf("%lld\\n", ans % MOD);

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

int main() {

  int N, L;
  scanf("%d %d", &N, &L);

  int loc[N], move[N];

  for (int i = 0; i < N; i++)
    scanf("%d", &loc[i]);
  for (int i = 0; i < N; i++)
    scanf("%d", &move[i]);

  reverse(loc, loc + N);
  reverse(move, move + N);

  ll pre[L], current[L];

  for (int i = 0; i < L; i++)
    pre[i] = 0;

  for (int i = 0; loc[0] + i * move[0] <= L - 1; i++) {
    pre[loc[0] + i * move[0]] = 1;
  }

  for (int i = 1; i <= L - 1; i++) {
    pre[i] += pre[i - 1];
  }

  if (N == 1) {
    printf("%lld\\n", pre[L - 1]);
    return 0;
  }

  int pos, right = L - 1;

  for (int rabbit = 1; rabbit <= N - 1; rabbit++) {
    for (int i = 0; i <= L - 1; i++)
      current[i] = 0;

    if (move[rabbit] == 0) {
      current[loc[rabbit]] = pre[right] - pre[loc[rabbit]];
      continue;
    }

    for (int i = 0; (pos = loc[rabbit] + i * move[rabbit]) < L - 1; i++) {
      current[pos] = pre[right] - pre[pos];
      if (current[pos] < 0)
        current[pos] += MOD;
    }
    for (int i = 0; i <= L - 1; i++)
      pre[i] = current[i];
    for (int i = 1; i <= L - 1; i++) {
      pre[i] += pre[i - 1];
      pre[i] %= MOD;
    }
  }

  ll ans = 0;
  for (int i = 0; i < L; i++) {
    ans += current[i];
    ans %= MOD;
  }

  printf("%lld\\n", ans % MOD);

  return 0;
}