alexjercan/bugnet huggingface dataset:
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
int main() {
  ll n, k, i, j, mod = 998244353;
  cin >> n >> k;
  vector<ll> l(k), r(k), dp(n), d(n);
  for (i = 0; i < k; i++)
    cin >> l[i] >> r[i];
  dp[0] = 1;
  d[1] = -1;
  for (i = 0; i < n; i++) {
    if (i > 0)
      dp[i] = (dp[i - 1] + d[i] + mod) % mod;
    if (i < n - 1) {
      for (j = 0; j < k; j++) {
        if (i + l[j] < n) {
          d[i + l[j]] = (d[i + l[j]] + dp[i] + mod) % mod;
          if (i + r[j] + 1 < n)
            d[i + r[j] + 1] = (d[i + r[j] + 1] - dp[i] + mod) % mod;
        }
      }
    }
  }
  cout << dp[n - 1] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
int main() {
  ll n, k, l, r, i, j, mod = 998244353;
  set<ll> s;
  cin >> n >> k;
  vector<ll> dp(n);
  for (i = 0; i < k; i++) {
    cin >> l >> r;
    for (j = l; j <= r; j++)
      s.insert(j);
  }
  dp[0] = 1;
  for (i = 1; i < n; i++) {
    for (auto x : s) {
      if (x <= i)
        dp[i] = (dp[i] + dp[i - x]) % mod;
      else
        break;
    }
  }
  cout << dp[n - 1] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
const ll M = 998244353;
int si[] = {-1, 0, 1, 0};
int sj[] = {0, -1, 0, 1};

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> l(k), r(k);
  rep(i, k) cin >> l[i] >> r[i];

  vector<ll> dp(n + 1, 0);
  vector<ll> dpsum(n + 1, 0);
  dp[1] = 1;
  dpsum[1] = 1;
  for (int i = 2; i <= n; i++) {
    rep(j, k) {
      int li = i - r[j];
      int ri = i - l[j];
      if (ri < 0)
        continue;
      li = max(li, 1);
      dp[i] += dpsum[ri] - dpsum[li - 1];
      dp[i] += M;
      dp[i] %= M;
    }
    dpsum[i] = dpsum[i - 1] + dp[i];
    dpsum[i] %= M;
  }

  dp[n] += M;
  cout << dp[n] % M << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
const ll M = 998244353;
int si[] = {-1, 0, 1, 0};
int sj[] = {0, -1, 0, 1};

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> l(k), r(k);
  vector<ll> s;
  rep(i, k) {
    cin >> l[i] >> r[i];
    rep(j, r[i] - l[i] + 1) s.push_back(l[i] + j);
  }

  vector<ll> dp(n + 1, 0);
  dp[1] = 1;
  for (int i = 1; i <= n; i++) {
    auto itr = s.begin();
    rep(j, s.size()) {
      if (i - *itr > 0)
        dp[i] += dp[i - *itr];
      dp[i] %= M;
      itr++;
    }
  }

  cout << dp[n] % M << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

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

long long GCD(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}
ll lcm(ll x, ll y) {
  return x / GCD(x, y) *
         y; // 先に割り算をして掛けられる数を小さくして掛け算を行う
}
const int MAX = 510000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> dp(N + 100), sdp(N + 100);
  vector<int> L(K), R(K);
  ll mod = 998244353;
  for (int i = 0; i < K; i++) {
    cin >> L[i] >> R[i];
  }

  dp[1] = 1;
  sdp[1] = 1;
  for (int j = 2; j <= N; j++) {
    for (int i = 0; i < K; i++) {
      int right = max(0, j - L[i]);
      int left = max(0, j - R[i] - 1);
      dp[j] += sdp[right] - sdp[left] + mod;
      // cout << left << " " << right << endl;
      dp[j] %= mod;
    }
    dp[j] %= mod;
    sdp[j] = sdp[j - 1] + dp[j];
    sdp[j] %= mod;
  }

  cout << dp[N] << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

long long GCD(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}
ll lcm(ll x, ll y) {
  return x / GCD(x, y) *
         y; // 先に割り算をして掛けられる数を小さくして掛け算を行う
}
const int MAX = 510000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
int main() {
  int N, K;
  cin >> N >> K;
  vector<int> L(K), R(K), dp(N + 100);
  set<int> s;
  for (int i = 0; i < K; i++) {
    cin >> L[i] >> R[i];
  }
  for (int i = 0; i < K; i++) {
    for (int j = L[i]; j <= R[i]; j++) {
      s.insert(j);
    }
  }
  dp[1] = 1;
  for (int i = 1; i <= N; i++) {
    for (auto d : s) {
      if (i + d <= N) {
        dp[i + d] += dp[i];
        dp[i + d] %= 998244353;
      }
    }
  }

  cout << dp[N] << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using P = pair<int, int>;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

const int mod = 998244353;

int main() {
  int n, k;
  cin >> n >> k;
  vector<P> ran(k);
  rep(i, k) cin >> ran[i].first >> ran[i].second;

  vector<ll> dp(n + 1);
  vector<ll> s(n + 2);
  dp[0] = 1;
  s[1] = 1;
  for (int i = 1; i <= n; ++i) {
    rep(j, k) {
      int li = i - ran[j].second;
      int ri = i - ran[j].first;
      if (ri < 0)
        continue;
      li = max(li, 0);
      (dp[i] += s[ri + 1] - s[li] + mod) %= mod;
    }
    s[i + 1] = (s[i] + dp[i]) % mod;
  }

  cout << dp[n - 1] % mod << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using P = pair<int, int>;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

const int mod = 998244353;

int main() {
  int n, k;
  cin >> n >> k;
  vector<P> ran(k);
  rep(i, k) cin >> ran[i].first >> ran[i].second;

  vector<ll> dp(n + 1);
  vector<ll> s(n + 1);
  dp[0] = 1;
  s[1] = 1;
  for (int i = 1; i <= n; ++i) {
    rep(j, k) {
      int li = i - ran[j].second;
      int ri = i - ran[j].first;
      if (ri < 0)
        continue;
      li = max(li, 0);
      (dp[i] += s[ri + 1] - s[li]) %= mod;
    }
    s[i + 1] = s[i] + dp[i];
  }

  cout << dp[n - 1] % mod << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>

int main() {
  int N, K;
  std::cin >> N >> K;
  typedef std::pair<int, int> RANGE;
  std::vector<RANGE> LR(K);
  for (int i = 0; i < K; ++i) {
    std::cin >> LR[i].first >> LR[i].second;
  }

  std::vector<int64_t> memo(N + 1);
  std::vector<int64_t> sum(N + 1);
  memo[N] = 1;
  sum[N] = 1;
  for (int pos = N - 1; pos >= 1; --pos) {
    int64_t count = 0;
    for (auto itr = LR.begin(); itr != LR.end(); ++itr) {
      const int begin_pos = pos + itr->first;
      if (begin_pos > N) {
        continue;
      }
      const int end_pos = pos + itr->second + 1;
      const int64_t begin_value = sum[begin_pos];
      const int64_t end_value = (end_pos > N) ? 0 : sum[end_pos];
      count += begin_value - end_value;
    }
    memo[pos] = count % 998244353;
    sum[pos] = sum[pos + 1] + memo[pos];
  }
  std::cout << memo[1] << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>

int main() {
  int N, K;
  std::cin >> N >> K;
  std::vector<int> S;
  S.reserve(N);
  for (int i = 0; i < K; ++i) {
    int L, R;
    std::cin >> L >> R;
    for (int j = L; j <= R; ++j) {
      S.push_back(j);
    }
  }
  std::sort(S.begin(), S.end());

  std::vector<int64_t> memo(N + 1);
  memo[N] = 1;
  for (int pos = N - 1; pos >= 1; --pos) {
    int64_t count = 0;
    for (auto itr = S.begin(); itr != S.end(); ++itr) {
      const int pos_move = pos + *itr;
      if (pos_move > N) {
        break;
      }
      count += memo[pos_move];
    }
    memo[pos] = count % 998244353;
  }
  std::cout << memo[1] << std::endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(v) v.begin(), v.end()

int main() {
  ll mod = 998244353;
  ll N, K;
  cin >> N >> K;
  vector<pair<ll, ll>> vec(K);
  for (ll i = 0; i < K; i++) {
    ll L, R;
    cin >> L >> R;
    vec[i] = make_pair(L, R);
  }
  sort(all(vec));
  vector<ll> dp(N, 0);

  for (ll i = N - 2; i >= 0; i--) {

    dp[i] = dp[i + 1];
    dp[i] %= mod;
    for (ll j = 0; j < K; j++) {
      ll A, B;
      tie(A, B) = vec[j];
      if (i + A > N - 1) {
        break;
      }
      if (i + A == N - 1) {
        dp[i]++;
      } else {

        dp[i] += dp[i + A];
        dp[i] %= mod;
      }

      if (i + B + 1 > N - 1) {
        break;
      }
      if (i + B + 1 == N - 1) {
        dp[i]--;
      } else {

        dp[i] -= dp[i + B + 1];
        dp[i] %= mod;
      }
    }
  }
  if (dp[0] < 0) {
    while (dp[0] < 0) {
      dp[0] += mod;
    }
  }
  cout << dp[0] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(v) v.begin(), v.end()
ll mod = 998244353;
vector<ll> note;
ll N, K;
vector<ll> vec;

ll dp(ll i) {
  if (note[i - 1] >= 0) {
    return note[i - 1];
  }

  ll s = 0;
  ll j = 0;
  while (j < vec.size()) {
    if (i + vec[j] > N) {
      break;
    }
    s += dp(i + vec[j]);
    s %= mod;
    j++;
  }

  note[i - 1] = s;
  return s;
}

int main() {

  cin >> N >> K;
  note = vector<ll>(N, -1);
  note[N - 1] = 1;

  vec = vector<ll>(0);
  vector<pair<ll, ll>> A(K);
  for (ll i = 0; i < K; i++) {
    ll L, R;
    cin >> L >> R;
    A[i] = make_pair(L, R);
  }
  sort(all(A));

  for (ll i = 0; i < K; i++) {
    ll L, R;
    tie(L, R) = A[i];
    for (ll j = L; j <= R; j++) {
      vec.push_back(j);
    }
  }

  cout << dp(1) << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(c) begin(c), end(c)
#define PI acos(-1)
#define oo LLONG_MAX
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
/*
N^2は無理

*/
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  vector<ll> pos(400010, 0), sum(400010, 0);
  vector<pair<ll, ll>> jump;
  ll N, K, MOD = 998244353;
  cin >> N >> K;
  rep(i, K) {
    ll L, R;
    cin >> L >> R;
    jump.push_back({L, R});
  }
  pos[200001] = 1;
  sum[200001] = 1;
  for (ll i = 2; i <= N; i++) {
    rep(j, K) {
      ll L = jump[j].first;
      ll R = jump[j].second;
      pos[200000 + i] += sum[200000 + i - L];
      pos[200000 + i] -= sum[200000 + i - R - 1];
      pos[200000 + i] += MOD;
      pos[200000 + i] %= MOD;
    }
    sum[200000 + i] = sum[200000 + i - 1];
    sum[200000 + i] += pos[200000 + i];
    sum[200000 + i] %= MOD;
    //    cout << "i:" << i << " pos:" << pos[200000 + i]
    //      << " sum:" << sum[200000 + i] << endl;
  }
  cout << pos[200000 + N] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(c) begin(c), end(c)
#define PI acos(-1)
#define oo LLONG_MAX
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
/*
N^2は無理

*/
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  vector<ll> pos(400010, 0);
  vector<ll> jump;
  ll N, K, MOD = 998244353;
  cin >> N >> K;
  rep(i, K) {
    ll L, R;
    cin >> L >> R;
    while (L <= R) {
      jump.push_back(L);
      L++;
    }
  }
  pos[200001] = 1;
  ll sz = jump.size();
  for (ll i = 2; i <= N; i++) {
    rep(j, sz) { (pos[200000 + i] += pos[200000 + i - jump[j]]) %= MOD; }
  }
  cout << pos[200000 + N] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define PI 3.141592653589793238462
using namespace std;
typedef long long ll;
typedef long double db;
ll d[200005], x[200005], y[200005], mod = 998244353;
int main() {
  ll n, k;
  cin >> n >> k;
  for (ll i = 1; i <= k; i++) {
    cin >> x[i] >> y[i];
  }
  d[1] = 1;
  d[0] = 0;
  for (ll i = 2; i <= n; i++) {
    for (ll j = 1; j <= k; j++) {
      if (i < x[j])
        continue;
      ll l = i - x[j], r = max((ll)0, i - y[j] - 1);
      d[i] += d[l] - d[r];
      d[i] = (d[i] % mod + mod) % mod;
    }
    d[i] += d[i - 1];
    d[i] %= mod;
  }
  cout << ((d[n] - d[n - 1]) % mod + mod) % mod << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define PI 3.141592653589793238462
using namespace std;
typedef long long ll;
typedef long double db;
set<ll> s;
ll d[200005], mod = 998244353;
int main() {
  ll n, k;
  cin >> n >> k;
  for (ll i = 1; i <= k; i++) {
    ll x, y;
    cin >> x >> y;
    for (int j = x; j <= y; j++) {
      s.insert(j);
    }
  }
  vector<ll> a;
  for (auto i : s) {
    a.push_back(i);
  }
  d[1] = 1;
  for (ll i = 2; i <= n; i++) {
    for (auto j : a) {
      if (i > j)
        d[i] += d[i - j], d[i] %= mod;
      else
        break;
    }
  }
  cout << d[n] % mod << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int mod = 998244353;
int n, k, lft[N], rgt[N];
int dp[N];

int add(int x, int y) {
  if (x + y >= mod) {
    return x + y - mod;
  }
  return x + y;
}

int sub(int x, int y) {
  if (x - y < 0) {
    return x - y + mod;
  }
  return x - y;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> k;

  for (int i = 0; i < k; i++) {
    cin >> lft[i] >> rgt[i];
  }

  dp[1] = 1;
  for (int i = 1; i <= n; i++) {
    if (i > 2) {
      dp[i] = add(dp[i], dp[i - 1]);
    }

    for (int j = 0; j < k; j++) {
      int l = i + lft[j];
      int r = i + rgt[j] + 1;

      if (l < N) {
        dp[l] = add(dp[l], dp[i]);
      }
      if (r < N) {
        dp[r] = sub(dp[r], dp[i]);
      }
    }
  }

  cout << dp[n] << '\\n';

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int mod = 998244353;
int n, k, lft[N], rgt[N];
int dp[N];

int add(int x, int y) {
  if (x + y >= mod) {
    return x + y - mod;
  }
  return x + y;
}

int sub(int x, int y) {
  if (x - y < 0) {
    return x - y + mod;
  }
  return x - y;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> k;

  for (int i = 0; i < k; i++) {
    cin >> lft[i] >> rgt[i];
  }

  dp[1] = 1;
  for (int i = 1; i <= n; i++) {
    if (i > 2) {
      dp[i] = add(dp[i], dp[i - 1]);
    }

    for (int j = 0; j < k; j++) {
      int l = i + lft[j];
      int r = i + rgt[j] + 1;

      dp[l] = add(dp[l], dp[i]);
      dp[r] = sub(dp[r], dp[i]);
    }
  }

  cout << dp[n] << '\\n';

  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
;
#define mod 998244353ll

int main(void) {
  int N, K;
  cin >> N >> K;
  int l[K], r[K];
  for (int i = 0; i < K; i++)
    cin >> l[i] >> r[i];

  vector<long long> dp(N + 1, 0);
  dp[0] = 0;
  dp[1] = 1;

  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < K; j++) {
      if (i - l[j] <= 0)
        continue;
      dp[i] = (dp[i] + dp[i - l[j]] - dp[max(0, i - r[j] - 1)]) % mod;
    }
    dp[i] = (dp[i] + dp[i - 1]) % mod;
  }

  long long ans = (dp[N] - dp[N - 1] + mod) % mod;

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
;
#define mod 998244353LL

int main(void) {
  int N, K;
  cin >> N >> K;
  int a[K], b[K];
  vector<bool> S(N + 1, 0);
  for (int i = 0; i < K; i++) {
    cin >> a[i] >> b[i];
    for (int j = a[i]; j < b[i] + 1; j++)
      S[j] = 1;
  }
  vector<long long> dp(N + 1, 0);
  dp[1] = 1;

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j < i; j++) {
      if (S[j]) {
        dp[i] = (dp[i] + dp[i - j]) % mod;
      }
    }
  }

  cout << dp[N] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#ifndef BZ
#pragma GCC optimize "-O3"
#endif
#include <bits/stdc++.h>
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define rep(i, l, r) for (ll i = (l); i < (r); ++i)
typedef long long ll;
typedef long double ld;
typedef unsigned int ull;
#define vi vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define int ll
#define pll pair<ll, ll>
#define pdd pair<ld, ld>
#define pb push_back
#define reset(x, v) memset(x, v, sizeof(x))
#define mod 998244353
using namespace std;

ll power(ll x, ll y) {
  if (x == 1 || y == 0)
    return 1;
  ll res = 1;
  x = x % mod;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return res;
}
int dp[200005];
int solve(int st, int n, pii p[], int k) {
  if (st == n)
    return 1;
  if (dp[st] != -1)
    return dp[st];
  int so = 0;
  rep(i, 0, k) {}
  return dp[st] = so;
}

signed main() {
  FASTIO
  int n, k;
  cin >> n >> k;
  pii p[k];
  rep(i, 0, k) { cin >> p[i].first >> p[i].second; }
  sort(p, p + k);
  int suff[n + 2];
  suff[n + 1] = 0;
  dp[n] = 1;
  suff[n] = 1;
  for (int i = n - 1; i > 0; i--) {
    dp[i] = 0;
    int L = n + 1, R = 0;
    rep(j, 0, k) {
      int l = p[j].first + i, r = min(p[j].second, n - i) + i;
      if (l > n || l > r)
        break;
      dp[i] = (dp[i] + (suff[l] - suff[r + 1] + mod) % mod) % mod;
      if (l == n)
        break;
    }
    // cout << i << " " << dp[i] << endl;
    suff[i] = (suff[i + 1] + dp[i]) % mod;
  }
  cout << dp[1];
  return 0;
}


Error: TLE

Fixed submission:

#ifndef BZ
#pragma GCC optimize "-O3"
#endif
#include <bits/stdc++.h>
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define rep(i, l, r) for (ll i = (l); i < (r); ++i)
typedef long long ll;
typedef long double ld;
typedef unsigned int ull;
#define vi vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define int ll
#define pll pair<ll, ll>
#define pdd pair<ld, ld>
#define pb push_back
#define reset(x, v) memset(x, v, sizeof(x))
#define mod 998244353
using namespace std;

ll power(ll x, ll y) {
  if (x == 1 || y == 0)
    return 1;
  ll res = 1;
  x = x % mod;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return res;
}
int dp[200005];
int solve(int st, int n, pii p[], int k) {
  if (st == n)
    return 1;
  if (dp[st] != -1)
    return dp[st];
  int so = 0;
  rep(i, 0, k) {
    for (int j = p[i].first; j <= p[i].second; j++) {
      if (j + st > n)
        break;
      so = (so + solve(st + j, n, p, k)) % mod;
    }
  }
  return dp[st] = so;
}

signed main() {
  FASTIO
  int n, k;
  cin >> n >> k;
  pii p[k];
  rep(i, 0, n + 1) dp[i] = -1;
  rep(i, 0, k) { cin >> p[i].first >> p[i].second; }
  cout << solve(1, n, p, k);
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
static const ll mod = 998244353;
ll N, K;
ll dp[200005];
ll sum[200005];
int main() {
  cin >> N >> K;
  vector<P> A(K);
  for (ll i = 0; i < K; i++) {
    ll l, r;
    cin >> l >> r;
    P p(l, r);
    A[i] = p;
  }
  dp[0] = 1;
  sum[0] = 1;
  for (ll i = 1; i < N; i++) {
    for (ll j = 0; j < K; j++) {
      ll l = A[j].first;
      ll r = A[j].second;
      if (i - l < 0) {
      } else if (0 <= i - l && 0 <= i - r - 1) {
        dp[i] = (dp[i] + sum[i - l] - sum[i - r - 1] + 2 * mod) % mod;
      } else
        dp[i] = (dp[i] + sum[i - l]) % mod;
    }
    sum[i] = (sum[i - 1] + dp[i]) % mod;
  }
  cout << dp[N - 1] << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
static const ll mod = 998244353;
ll N, K;
ll dp[100005];
ll sum[100005];
int main() {
  cin >> N >> K;
  vector<P> A(K);
  for (ll i = 0; i < K; i++) {
    ll l, r;
    cin >> l >> r;
    P p(l, r);
    A[i] = p;
  }
  dp[0] = 1;
  sum[0] = 1;
  for (ll i = 1; i < N; i++) {
    for (ll j = 0; j < K; j++) {
      ll l = A[j].first;
      ll r = A[j].second;
      if (i - l < 0) {
      } else if (0 <= i - l && 0 <= i - r - 1) {
        dp[i] = (dp[i] + sum[i - l] - sum[i - r - 1] + 2 * mod) % mod;
      } else
        dp[i] = (dp[i] + sum[i - l]) % mod;
    }
    sum[i] = (sum[i - 1] + dp[i]) % mod;
  }
  cout << dp[N - 1] << endl;

  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define MOD 998244353

int main() {
  int n, k, sz, l, r, ans = 0;
  cin >> n >> k;
  vector<int> dp(n + 10, 0);
  vector<pair<int, int>> vpp;
  while (k--) {
    cin >> l >> r;
    r++;
    vpp.push_back({l, r});
  }
  dp[0] = 1;
  dp[1] = -1;
  for (int i = 0; i < n; i++) {
    if (i > 0)
      dp[i] = (dp[i] + dp[i - 1]) % MOD;
    for (auto lol : vpp) {
      tie(l, r) = lol;
      if (i + l < n)
        dp[i + l] = (dp[i + l] + dp[i]) % MOD;
      if (i + r < n)
        dp[i + r] = (dp[i + r] - dp[i]) % MOD;
    }
  }
  // for(int i=0;i<n;i++) cout << i << " = " << dp[i] << endl;
  cout << (MOD + dp[n - 1]) % MOD << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

#define MOD 998244353

int main() {
  int n, k, sz, l, r, ans = 0;
  cin >> n >> k;
  vector<int> coins, dp(n + 5, 0);
  while (k--) {
    cin >> l >> r;
    for (int i = l; i <= r; i++)
      coins.push_back(i);
  }
  dp[0] = 1;
  for (int i = 0; i <= n; i++) {
    // cout << dp[i] << endl;
    for (auto lol : coins) {
      if (i + lol <= n + 1) {
        dp[i + lol] = (dp[i + lol] + dp[i]) % MOD;
        // cout << i+lol << " " << dp[i+lol] << endl;
      }
    }
    // for(auto lol : dp) cout << lol << " ";
    //	cout << endl ;
  }
  cout << dp[n - 1] << endl;
  return 0;
}