prithivMLmods/Coder-Stat huggingface dataset:
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;
using ll = long long;

ll mod = 998244353;

int main() {
  int n, k;
  cin >> n >> k;

  vector<pair<ll, ll>> lr(k);
  for (auto &v : lr) {
    ll l, r;
    cin >> l >> r;
    v = make_pair(l, r);
  }

  vector<ll> vec1(3e5, 0), vec2(3e5, 0), vec3(3e5, 0);
  vec1[1] = 1;
  vec2[1] = 1;
  vec3[1] = 1;

  for (ll i = 1; i < n; ++i) {
    for (auto &v : lr) {
      ll l = v.first;
      ll r = v.second;
      vec1[i + l] = (vec1[i + l] + vec3[i]) % mod;
      vec2[i + r] = (vec2[i + r] + vec3[i]) % mod;
    }
    vec3[i + 1] = (vec3[i] - vec2[i]) % mod;
    if (vec3[i + 1] < 0)
      vec3[i + 1] += mod;
    vec3[i + 1] = (vec3[i + 1] + vec1[i + 1]) % mod;
  }

  cout << vec3[n] << endl;
}


Fixed submission:
#include <iostream>
#include <vector>

using namespace std;
using ll = long long;

ll mod = 998244353;

int main() {
  int n, k;
  cin >> n >> k;

  vector<pair<ll, ll>> lr(k);
  for (auto &v : lr) {
    ll l, r;
    cin >> l >> r;
    v = make_pair(l, r);
  }

  vector<ll> vec1(5e5, 0), vec2(5e5, 0), vec3(5e5, 0);
  vec1[1] = 1;
  vec2[1] = 1;
  vec3[1] = 1;

  for (ll i = 1; i < n; ++i) {
    for (auto &v : lr) {
      ll l = v.first;
      ll r = v.second;
      vec1[i + l] = (vec1[i + l] + vec3[i]) % mod;
      vec2[i + r] = (vec2[i + r] + vec3[i]) % mod;
    }
    vec3[i + 1] = (vec3[i] - vec2[i]) % mod;
    if (vec3[i + 1] < 0)
      vec3[i + 1] += mod;
    vec3[i + 1] = (vec3[i + 1] + vec1[i + 1]) % mod;
  }

  cout << vec3[n] << endl;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

int main(void) {
  ll N, K;
  cin >> N >> K;
  vector<pair<ll, ll>> ranges(K);
  vector<ll> diffs(N + 1, 0);
  vector<ll> paths(N + 1, 0);
  paths[1] = 1;
  ll cur = 0;
  for (ll i = 0; i < K; i++) {
    int l, r;
    cin >> l >> r;
    ranges[i] = make_pair(l, r);
  }
  for (ll i = 1; i <= N; i++) {
    cur += diffs[i];
    paths[i] += cur;
    paths[i] = paths[i] % 998244353;
    if (paths[i] > 0) {
      for (ll j = 0; j < K; j++) {
        if (i + ranges[j].first <= N) {
          diffs[i + ranges[j].first] += paths[i];
          diffs[min(N + 1, i + ranges[j].second + 1)] -= paths[i];
        }
      }
    }
  }
  cout << paths[N] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

int main(void) {
  ll N, K;
  cin >> N >> K;
  vector<pair<ll, ll>> ranges(K);
  vector<ll> diffs(N + 2, 0);
  vector<ll> paths(N + 2, 0);
  paths[1] = 1;
  ll cur = 0;
  for (ll i = 0; i < K; i++) {
    int l, r;
    cin >> l >> r;
    ranges[i] = make_pair(l, r);
  }
  for (ll i = 1; i <= N; i++) {
    cur += diffs[i];
    paths[i] += cur;
    paths[i] = paths[i] % 998244353;
    if (paths[i] > 0) {
      for (ll j = 0; j < K; j++) {
        if (i + ranges[j].first <= N) {
          diffs[i + ranges[j].first] += paths[i];
          diffs[min(N + 1, i + ranges[j].second + 1)] -= paths[i];
        }
      }
    }
  }
  cout << paths[N] << endl;
  return 0;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
#define int long long
#define mod 998244353
int L[11], R[11];
int dp[100005];
int sum[100005];

signed main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  for (int i = 1; i <= K; i++) {
    cin >> L[i] >> R[i];
  }
  sum[1] = dp[1] = 1;
  for (int i = 2; i <= N; i++) {
    for (int j = 1; j <= K; j++) {
      if (i - L[j] < 1)
        continue;
      else if (i - R[j] < 1) {
        dp[i] = (dp[i] + sum[i - L[j]]) % mod;
      } else {
        dp[i] = (dp[i] + sum[i - L[j]]) % mod;
        dp[i] = (dp[i] - sum[i - R[j] - 1] + mod) % mod;
      }
    }
    sum[i] = (sum[i - 1] + dp[i]) % mod;
  }
  cout << dp[N] << endl;
}

Fixed submission:
#include <iostream>
using namespace std;
#define int long long
#define mod 998244353
int L[11], R[11];
int dp[200005];
int sum[200005];

signed main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  for (int i = 1; i <= K; i++) {
    cin >> L[i] >> R[i];
  }
  sum[1] = dp[1] = 1;
  for (int i = 2; i <= N; i++) {
    for (int j = 1; j <= K; j++) {
      if (i - L[j] < 1)
        continue;
      else if (i - R[j] < 1) {
        dp[i] = (dp[i] + sum[i - L[j]]) % mod;
      } else {
        dp[i] = (dp[i] + sum[i - L[j]]) % mod;
        dp[i] = (dp[i] - sum[i - R[j] - 1] + mod) % mod;
      }
    }
    sum[i] = (sum[i - 1] + dp[i]) % mod;
  }
  cout << dp[N] << endl;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
// $%D%$.$%M%$.$%Y%$
// $%h%$:$%m%$:$%s%$

#pragma GCC optimize("O3")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0)
#define ll long long
#define ld long double
#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)
#define per(i, a, n) for (int i = (int)(n - 1); i >= (int)(a); i--)
#define mp(a, b) make_pair(a, b)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pil pair<int, ll>
#define pli pair<ll, int>
#define fi first
#define se second

const int MOD = 998244353; // = (119<<23)+1
const int MX = 2e5 + 5;
const ll INF = 1e18;
const ld PI = 4 * atan((ld)1);
const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1};

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> dp(n + 1);
  dp[0] = 1;
  vector<int> pref(n + 1);
  pref[1] = 1;
  vector<pii> da(k);

  for (int i = 0; i < k; ++i) {
    cin >> da[i].first >> da[i].second;
  }
  for (int i = 1; i <= n; ++i) {
    for (auto x : da) {
      int l = i - x.second;
      int r = i - x.first + 1;
      dp[i] = (dp[i] + (pref[max(0, r)] - pref[max(0, l)] + MOD) % MOD) % MOD;
    }
    pref[i + 1] = (pref[i] + dp[i]) % MOD;
  }
  cout << dp[n - 1] << '\n';
}

Fixed submission:
// $%D%$.$%M%$.$%Y%$
// $%h%$:$%m%$:$%s%$

#pragma GCC optimize("O3")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0)
#define ll long long
#define ld long double
#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)
#define per(i, a, n) for (int i = (int)(n - 1); i >= (int)(a); i--)
#define mp(a, b) make_pair(a, b)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pil pair<int, ll>
#define pli pair<ll, int>
#define fi first
#define se second

const int MOD = 998244353; // = (119<<23)+1
const int MX = 2e5 + 5;
const ll INF = 1e18;
const ld PI = 4 * atan((ld)1);
const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1};

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> dp(n + 1);
  dp[0] = 1;
  vector<int> pref(n + 2);
  pref[1] = 1;
  vector<pii> da(k);

  for (int i = 0; i < k; ++i) {
    cin >> da[i].first >> da[i].second;
  }
  for (int i = 1; i <= n; ++i) {
    for (auto x : da) {
      int l = i - x.second;
      int r = i - x.first + 1;
      dp[i] = (dp[i] + (pref[max(0, r)] - pref[max(0, l)] + MOD) % MOD) % MOD;
    }
    pref[i + 1] = (pref[i] + dp[i]) % MOD;
  }
  cout << dp[n - 1] << '\n';
}
Problem p02549 (Python)

Buggy submission (status: Runtime Error):
p = 998244353
n, k = map(int, input().split())
LRs = [tuple(map(int, input().split())) for _ in range(k)]
A = [0] * (2 * n)
for lj, rj in LRs:
    A[lj] += 1
    A[rj + 1] -= 1
s = 0
for i in range(1, n):
    s = (s + A[i]) % p
    for lj, rj in LRs:
        A[i + lj] += s
        A[i + rj + 1] -= s
print(s)


Fixed submission:
p = 998244353
n, k = map(int, input().split())
LRs = [tuple(map(int, input().split())) for _ in range(k)]
A = [0] * (2 * n + 1)
for lj, rj in LRs:
    A[lj] += 1
    A[rj + 1] -= 1
s = 0
for i in range(1, n):
    s = (s + A[i]) % p
    for lj, rj in LRs:
        A[i + lj] += s
        A[i + rj + 1] -= s
print(s)
Problem p02549 (Python)

Buggy submission (status: Runtime Error):
p = 998244353
n, k = map(int, input().split())
LRs = []
for j in range(k):
    lj, rj = map(int, input().split())
    LRs.append((lj, rj))
A = [0] * n
i0 = min(lj for lj, rj in LRs)
A[0] = 1
A[i0] = 1
for i in range(i0 + 1, n):
    A[i] = A[i - 1]
    for j in range(k):
        lj, rj = LRs[j]
        if i - lj >= 0:
            A[i] += A[i - lj]
        if i - 1 - rj >= 0:
            A[i] -= A[i - 1 - rj]
        A[i] %= p
print(A[n - 1])


Fixed submission:
p = 998244353
n, k = map(int, input().split())
LRs = []
for j in range(k):
    lj, rj = map(int, input().split())
    LRs.append((lj, rj))
A = [0] * (n + 1)
i0 = min(lj for lj, rj in LRs)
A[0] = 1
A[i0] = 1
for i in range(i0 + 1, n):
    A[i] = A[i - 1]
    for j in range(k):
        lj, rj = LRs[j]
        if i - lj >= 0:
            A[i] += A[i - lj]
        if i - 1 - rj >= 0:
            A[i] -= A[i - 1 - rj]
        A[i] %= p
print(A[n - 1])
Problem p02549 (Python)

Buggy submission (status: Time Limit Exceeded):
def main():
    n, k = map(int, input().split())
    mod = 998244353
    dp = [0] * (n + 1)
    dp[1] = 1
    delta = []
    for _ in range(k):
        l, r = map(int, input().split())
        for i in range(l, r + 1):
            delta.append(i)
    delta.sort()
    for i in range(n):
        for d in delta:
            if i + d > n:
                break
            dp[i + d] += dp[i]
            dp[i + d] %= mod
    print(dp[n])


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    n, k = map(int, input().split())
    mod = 998244353
    dp = [0] * (n + 1)
    dp[1] = 1
    dp[2] = -1
    left = [0] * k
    right = [0] * k
    for i in range(k):
        left[i], right[i] = map(int, input().split())
        right[i] += 1
    for i in range(1, n + 1):
        dp[i] += dp[i - 1]
        for j in range(k):
            if i + left[j] <= n:
                dp[i + left[j]] += dp[i]
            if i + right[j] <= n:
                dp[i + right[j]] -= dp[i]
        dp[i] %= mod
    print(dp[n])


if __name__ == "__main__":
    main()
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int32_t main() {

  ios_base::sync_with_stdio(false);

  cin.tie(0);

  long long n;

  cin >> n;

  long long k;

  cin >> k;

  long long diff[200005];

  memset(diff, 0, sizeof diff);

  vector<pair<long long, long long>> v;

  for (long long i = 0; i < k; i++) {

    long long x;

    cin >> x;

    long long y;

    cin >> y;

    v.push_back({x, y});
  }

  diff[0] = 0;

  diff[1] = 1;

  long long last = 0;

  for (long long i = 1; i <= n + 5; i++) {

    if (i > 2) {

      diff[i] += diff[i - 1];
    }

    long long cur = diff[i] % 998244353;

    for (long long j = 0; j < v.size(); j++) {

      diff[i + v[j].first] += cur;

      diff[i + v[j].second + 1] -= cur;
    }
  }

  cout << diff[n] % 998244353 << '\n';

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int32_t main() {

  ios_base::sync_with_stdio(false);

  cin.tie(0);

  long long n;

  cin >> n;

  long long k;

  cin >> k;

  long long diff[4 * 200005];

  memset(diff, 0, sizeof diff);

  vector<pair<long long, long long>> v;

  for (long long i = 0; i < k; i++) {

    long long x;

    cin >> x;

    long long y;

    cin >> y;

    v.push_back({x, y});
  }

  diff[0] = 0;

  diff[1] = 1;

  long long last = 0;

  for (long long i = 1; i <= n + 5; i++) {

    if (i > 2) {

      diff[i] += diff[i - 1];
    }

    long long cur = diff[i] % 998244353;

    for (long long j = 0; j < v.size(); j++) {

      diff[i + v[j].first] += cur;

      diff[i + v[j].second + 1] -= cur;
    }
  }

  cout << diff[n] % 998244353 << '\n';

  return 0;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

int N, K;
int L[15], R[15];
long long imos[202020];
long long now = 1;

int main() {
  cin >> N >> K;
  for (int i = 0; i < K; i++) {
    cin >> L[i] >> R[i];
  }

  for (int i = 1; i <= N; i++) {
    now += imos[i];
    now %= 998244353LL;
    for (int j = 0; j < K; j++) {
      if (i + L[j] < 202020) {
        imos[i + L[j]] += now;
      }
      if (i + R[j] < 202020) {
        imos[i + R[j] + 1] -= now;
      }
    }
    if (i == N) {
      while (imos[N] < 0LL) {
        imos[N] += 998244353LL;
      }
      cout << imos[N] % 998244353LL << endl;
    }
  }
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

int N, K;
int L[15], R[15];
long long imos[202020];
long long now = 1;

int main() {
  cin >> N >> K;
  for (int i = 0; i < K; i++) {
    cin >> L[i] >> R[i];
  }

  for (int i = 1; i <= N; i++) {
    now += imos[i];
    now %= 998244353LL;
    for (int j = 0; j < K; j++) {
      if (i + L[j] < 202020) {
        imos[i + L[j]] += now;
      }
      if (i + R[j] + 1 < 202020) {
        imos[i + R[j] + 1] -= now;
      }
    }
    if (i == N) {
      while (imos[N] < 0LL) {
        imos[N] += 998244353LL;
      }
      cout << imos[N] % 998244353LL << endl;
    }
  }
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 998244353;
const ll INF = (1ll << 60);

int main(void) {
  int n, k;
  cin >> n >> k;

  vector<ll> l(k), r(k), dp(n, 0), s(n + 1, 0);

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

  dp[0] = 1;
  s[1] = 1;

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < k; j++) {
      ll x, y;

      if (i - l[j] < 0) {
        continue;
      }

      x = i - l[j] + 1; // big
      y = i - r[j];     // small

      dp[i] += s[x] - s[y] + MOD;
      dp[i] %= MOD;
    }

    s[i + 1] = s[i] + dp[i] + MOD;
    s[i + 1] %= MOD;
  }

  cout << dp[n - 1] << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 998244353;
const ll INF = (1ll << 60);

int main(void) {
  int n, k;
  cin >> n >> k;

  vector<ll> l(k), r(k), dp(n, 0), s(n + 1, 0);

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

  dp[0] = 1;
  s[1] = 1;

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < k; j++) {
      ll x, y;

      if (i - l[j] < 0) {
        continue;
      }

      x = max(i - l[j], 0ll) + 1; // big
      y = max(i - r[j], 0ll);     // small

      dp[i] += s[x] - s[y] + MOD;
      dp[i] %= MOD;
    }

    s[i + 1] = s[i] + dp[i] + MOD;
    s[i + 1] %= MOD;
  }

  cout << dp[n - 1] << endl;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
// {{{ Boilerplate Code <--------------------------------------------------
// vim:filetype=cpp:foldmethod=marker:foldmarker={{{,}}}

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define ALL(A) (A).begin(), (A).end()

#define MOD 998244353
using namespace std;

// }}}

int main() {
  int N, K;

  cin >> N >> K;
  vector<pair<int, int>> seg;

  FOR(i, 0, K) {
    int L, R;
    cin >> L >> R;
    seg.push_back(pair<int, int>(L, R));
  }

  long long cur = 0;

  long long a[300000];

  FOR(i, 0, N + 1) a[i] = 0;
  a[0] = 1;
  a[1] = -1;

  FOR(i, 0, N) {
    cur = (cur + a[i]) % MOD;
    FOR(j, 0, seg.size()) {
      a[i + seg[j].first] = (a[i + seg[j].first] + cur) % MOD;
      a[i + seg[j].second + 1] = (a[i + seg[j].second + 1] - cur) % MOD;
    }
  }

  cout << (cur + MOD) % MOD << endl;
}


Fixed submission:
// {{{ Boilerplate Code <--------------------------------------------------
// vim:filetype=cpp:foldmethod=marker:foldmarker={{{,}}}

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define ALL(A) (A).begin(), (A).end()

#define MOD 998244353
using namespace std;

// }}}

int main() {
  int N, K;

  cin >> N >> K;
  vector<pair<int, int>> seg;

  FOR(i, 0, K) {
    int L, R;
    cin >> L >> R;
    seg.push_back(pair<int, int>(L, R));
  }

  long long cur = 0;

  long long a[500000];

  FOR(i, 0, N + 1) a[i] = 0;
  a[0] = 1;
  a[1] = -1;

  FOR(i, 0, N) {
    cur = (cur + a[i]) % MOD;
    FOR(j, 0, seg.size()) {
      a[i + seg[j].first] = (a[i + seg[j].first] + cur) % MOD;
      a[i + seg[j].second + 1] = (a[i + seg[j].second + 1] - cur) % MOD;
    }
  }

  cout << (cur + MOD) % MOD << endl;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 998244353;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<pair<ll, ll>> sections;
  for (int i = 0; i < k; ++i) {
    ll l, r;
    cin >> l >> r;
    sections.push_back({l, r});
  }
  vector<ll> f(2 * n + 100, 0), a(2 * n + 100, 0);
  f[1] = 1;
  for (int i = 1; i < 2 * n; ++i) {
    for (int j = 0; j < k; ++j) {
      ll l = sections[j].first;
      ll r = sections[j].second;
      a[i + l] += f[i];
      a[i + r + 1] += (mod - f[i]);
      a[i + l] %= mod;
      a[i + r + 1] %= mod;
    }
    if (i == 1)
      f[i + 1] = a[i + 1];
    else
      f[i + 1] = f[i] + a[i + 1];
    f[i + 1] %= mod;
  }
  cout << f[n] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 998244353;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<pair<ll, ll>> sections;
  for (int i = 0; i < k; ++i) {
    ll l, r;
    cin >> l >> r;
    sections.push_back({l, r});
  }
  vector<ll> f(2 * n + 100, 0), a(2 * n + 100, 0);
  f[1] = 1;
  for (int i = 1; i < n; ++i) {
    for (int j = 0; j < k; ++j) {
      ll l = sections[j].first;
      ll r = sections[j].second;
      a[i + l] += f[i];
      a[i + r + 1] += (mod - f[i]);
      a[i + l] %= mod;
      a[i + r + 1] %= mod;
    }
    if (i == 1)
      f[i + 1] = a[i + 1];
    else
      f[i + 1] = f[i] + a[i + 1];
    f[i + 1] %= mod;
  }
  cout << f[n] << endl;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define ll long long
#define MOD 998244353
using namespace std;
;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> L(K), R(K);
  rep(i, 0, K) cin >> L[i] >> R[i];
  vector<ll> dp(N + 1, 0);
  dp[0] = 1;
  dp[1] = -1;
  rep(i, 0, N) {
    dp[i] = ((dp[i] % MOD) + MOD) % MOD;
    dp[i + 1] = (dp[i + 1] + dp[i]) % MOD;
    rep(j, 0, K) {
      dp[i + L[j]] += dp[i];
      dp[i + R[j] + 1] -= dp[i];
    }
  }
  cout << dp[N - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define ll long long
#define MOD 998244353
using namespace std;
;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> L(K), R(K);
  rep(i, 0, K) cin >> L[i] >> R[i];
  vector<ll> dp(N + 1, 0);
  dp[0] = 1;
  dp[1] = -1;
  rep(i, 0, N) {
    dp[i] = ((dp[i] % MOD) + MOD) % MOD;
    dp[i + 1] = (dp[i + 1] + dp[i]) % MOD;
    rep(j, 0, K) {
      if (i + L[j] < N)
        dp[i + L[j]] += dp[i];
      if (i + R[j] + 1 < N)
        dp[i + R[j] + 1] -= dp[i];
    }
  }
  cout << dp[N - 1] << endl;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
#define _GLIBCXX_DEBUG
ll mod = 998244353;

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(200010);
  vector<ll> dpsum(20010);
  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] += mod;
      dp[i] %= mod;
    }
    dpsum[i] += dpsum[i - 1] + dp[i];
    dpsum[i] %= mod;
    // cout << dp[i] << " " << dpsum[i] << endl;
  }
  cout << dp[n] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
#define _GLIBCXX_DEBUG
ll mod = 998244353;

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);
  vector<ll> dpsum(n + 1);
  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] += mod;
      dp[i] %= mod;
    }
    dpsum[i] += dpsum[i - 1] + dp[i];
    dpsum[i] %= mod;
    // cout << dp[i] << " " << dpsum[i] << endl;
  }
  cout << dp[n] << endl;
}
Problem p02549 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstring>
#include <iostream>
#include <stdio.h>
#define int long long
using namespace std;
const int N = 5e5, M = 15, MOD = 998244353;
int a[N];
int f[N];
int x[M], y[M];
int n, m;
int qzh[N];
inline int modbez(int a, int b) {
  register int res = a % b;
  if (res < 0)
    return res + b;
  else
    return res;
}
void DP() {
  f[n] = 1;
  for (int i = n; i >= 1; i--) {
    for (int j = 1; j <= m; j++) {
      f[i] = modbez(f[i] + qzh[i + x[j]] - qzh[i + y[j] + 1], MOD);
    }
    qzh[i] = modbez(qzh[i + 1] + f[i], MOD);
  }
  cout << f[1] % MOD << endl;
}
void common_DP() {
  f[n] = 1;
  for (int i = n; i >= 1; i--)
    for (int j = 1; j <= m; j++)
      for (int k = x[j]; k <= y[j]; k++) {
        if (i + k <= n) {
          f[i] = (f[i] + f[i + k]) % MOD;
        }
      }
  cout << f[1] << endl;
}
signed main() {
  memset(f, 0, sizeof(f));
  memset(a, 0, sizeof(a));
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> x[i] >> y[i];
  }
  if (n > 1e4)
    common_DP();
  else
    DP();
}


Fixed submission:
#include <cstring>
#include <iostream>
#include <stdio.h>
#define int long long
using namespace std;
const int N = 5e5, M = 15, MOD = 998244353;
int a[N];
int f[N];
int x[M], y[M];
int n, m;
int qzh[N];
inline int modbez(int a, int b) {
  register int res = a % b;
  if (res < 0)
    return res + b;
  else
    return res;
}
void DP() {
  f[n] = 1;
  for (int i = n; i >= 1; i--) {
    for (int j = 1; j <= m; j++) {
      f[i] = modbez(f[i] + qzh[i + x[j]] - qzh[i + y[j] + 1], MOD);
    }
    qzh[i] = modbez(qzh[i + 1] + f[i], MOD);
  }
  cout << f[1] % MOD << endl;
}
void common_DP() {
  f[n] = 1;
  for (int i = n; i >= 1; i--)
    for (int j = 1; j <= m; j++)
      for (int k = x[j]; k <= y[j]; k++) {
        if (i + k <= n) {
          f[i] = (f[i] + f[i + k]) % MOD;
        }
      }
  cout << f[1] << endl;
}
signed main() {
  memset(f, 0, sizeof(f));
  memset(a, 0, sizeof(a));
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> x[i] >> y[i];
  }
  if (n < 1e4)
    common_DP();
  else
    DP();
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define N int(1e5 + 10)
#define mod 998244353
using namespace std;
ll dp[N], pre[N];
int l[N], r[N], n, k;
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout.precision(10);
  cin >> n >> k;
  for (int i = 1; i <= k; i++)
    cin >> l[i] >> r[i];
  pre[0] = dp[0] = 0ll;
  pre[1] = dp[1] = 1ll;
  for (int i = 2; i <= n; i++) {
    dp[i] = 0ll;
    for (int j = 1, u, v; j <= k; j++) {
      if (l[j] >= i)
        continue;
      u = l[j];
      v = min(i - 1, r[j]);
      dp[i] = ((dp[i] + pre[i - u]) % mod - pre[i - v - 1]) % mod;
      if (dp[i] < 0ll)
        dp[i] += mod;
    }
    pre[i] = (pre[i - 1] + dp[i]) % mod;
  }
  cout << dp[n] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define N int(2e5 + 10)
#define mod 998244353
using namespace std;
ll dp[N], pre[N];
int l[N], r[N], n, k;
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout.precision(10);
  cin >> n >> k;
  for (int i = 1; i <= k; i++)
    cin >> l[i] >> r[i];
  pre[0] = dp[0] = 0ll;
  pre[1] = dp[1] = 1ll;
  for (int i = 2; i <= n; i++) {
    dp[i] = 0ll;
    for (int j = 1, u, v; j <= k; j++) {
      if (l[j] >= i)
        continue;
      u = l[j];
      v = min(i - 1, r[j]);
      dp[i] = ((dp[i] + pre[i - u]) % mod - pre[i - v - 1]) % mod;
      if (dp[i] < 0ll)
        dp[i] += mod;
    }
    pre[i] = (pre[i - 1] + dp[i]) % mod;
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main() {

  int n, k;
  cin >> n >> k;
  int mod = 998244353;
  vector<pair<int, int>> steps;
  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    steps.push_back({x, y});
  }

  sort(steps.begin(), steps.end());
  vector<int> dp(n);
  dp[0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      int l = i - steps[j].second;
      int r = i - steps[j].first;
      if (r >= 0) {
        dp[i] = (dp[i] + dp[r]) % mod;
      }
      if (l > 0) {
        dp[i] = (dp[i] - dp[l - 1] + mod) % mod;
      }
    }

    if (i != n - 1) {
      dp[i] = (dp[i] + dp[i - 1]) % mod;
    }
  }

  cout << dp[n - 1];

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {

  int n, k;
  cin >> n >> k;
  int mod = 998244353;
  vector<pair<int, int>> steps;
  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    steps.push_back({x, y});
  }

  sort(steps.begin(), steps.end());
  vector<int> dp(n);
  dp[0] = 1;
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < k; j++) {
      int l = i - steps[j].second;
      int r = i - steps[j].first;
      if (r >= 0) {
        dp[i] = (dp[i] + dp[r]) % mod;
      }
      if (l > 0) {
        dp[i] = (dp[i] - dp[l - 1] + mod) % mod;
      }
    }

    if (i != n - 1) {
      dp[i] = (dp[i] + dp[i - 1]) % mod;
    }
  }

  cout << dp[n - 1];

  return 0;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// g++ -std=c++11

using namespace std;
typedef long long ll;

#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define DIV 998244353

long long dp[200005];

int main() {
  ll N, K;
  cin >> N >> K;
  vector<pair<ll, ll>> LR(K);

  rep(i, K) { cin >> LR[i].first >> LR[i].second; }

  ll cur = 1;
  dp[0] = -1;
  ll ans = -1;

  rep(i, N) {
    rep(j, K) {
      ll l = LR[j].first;
      ll r = LR[j].second;
      dp[i + l - 1] += cur;
      dp[i + l - 1] %= DIV;
      dp[i + r] -= cur;
      dp[i + r] += DIV;
      dp[i + r] %= DIV;
    }
    if (i == N - 1) {
      ans = cur;
    }

    cur += dp[i];
    cur %= DIV;
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
// g++ -std=c++11

using namespace std;
typedef long long ll;

#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define DIV 998244353

long long dp[200005];

int main() {
  ll N, K;
  cin >> N >> K;
  vector<pair<ll, ll>> LR(K);

  rep(i, K) { cin >> LR[i].first >> LR[i].second; }

  ll cur = 1;
  dp[0] = -1;
  ll ans = -1;

  rep(i, N) {
    rep(j, K) {
      ll l = LR[j].first;
      ll r = LR[j].second;
      if (i + l - 1 <= N) {
        dp[i + l - 1] += cur;
        dp[i + l - 1] %= DIV;
      }
      if (i + r <= N) {
        dp[i + r] -= cur;
        dp[i + r] += DIV;
        dp[i + r] %= DIV;
      }
    }
    if (i == N - 1) {
      ans = cur;
    }

    cur += dp[i];
    cur %= DIV;
  }

  cout << ans << endl;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define ll long long
#define pb push_back
#define pp pair<long long, long long>
#define ppp pair<long long, pair<long long, long long>>
#define f first
#define s second
#define pd pair<double, long long>
ll barr[200100], M = 998244353;
void solveTestCase() {
  ll n, i, j, k, l;
  cin >> n >> k;
  vector<pp> arr(k + 1);
  for (i = 1; i <= k; i++)
    cin >> arr[i].f >> arr[i].s;
  barr[1] = 1;
  barr[2] = -1;
  for (i = 1; i <= n; i++) {
    barr[i] += barr[i - 1];
    barr[i] %= M;
    if (!barr[i])
      continue;
    for (j = 1; j <= k; j++) {
      barr[i + arr[j].f] += barr[i];
      barr[i + arr[j].f] %= M;
      barr[i + arr[j].s + 1] += (M - barr[i]);
      barr[i + arr[j].s + 1] %= M;
    }
  }
  cout << barr[n] << '\n';
}
int main() {
  ll t, n, i, j, k, l, m;
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll p;
  t = 1;
  // cin>>t;
  for (ll T = 1; T <= t; T++) {
    solveTestCase();
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define ll long long
#define pb push_back
#define pp pair<long long, long long>
#define ppp pair<long long, pair<long long, long long>>
#define f first
#define s second
#define pd pair<double, long long>
ll barr[200100], M = 998244353;
void solveTestCase() {
  ll n, i, j, k, l;
  cin >> n >> k;
  vector<pp> arr(k + 1);
  for (i = 1; i <= k; i++)
    cin >> arr[i].f >> arr[i].s;
  barr[1] = 1;
  barr[2] = -1;
  for (i = 1; i <= n; i++) {
    barr[i] += barr[i - 1];
    barr[i] %= M;
    if (!barr[i])
      continue;
    for (j = 1; j <= k; j++) {
      if (i + arr[j].f <= n) {
        barr[i + arr[j].f] += barr[i];
        barr[i + arr[j].f] %= M;
      }
      if (i + arr[j].s + 1 <= n) {
        barr[i + arr[j].s + 1] += (M - barr[i]);
        barr[i + arr[j].s + 1] %= M;
      }
    }
  }
  cout << barr[n] << '\n';
}
int main() {
  ll t, n, i, j, k, l, m;
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll p;
  t = 1;
  // cin>>t;
  for (ll T = 1; T <= t; T++) {
    solveTestCase();
  }
  return 0;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long n, k, i, j, mod = 998244353;
  cin >> n >> k;

  vector<pair<long, long>> d(k);
  for (j = 0; j < k; j++) {
    cin >> d[j].first >> d[j].second;
  }

  vector<long long> ans(n + 1 + 100000, 0), cur(n + 1 + 100000, 0);
  long long l, r, res;

  res = 1;
  for (i = 1; i < n; i++) {
    for (j = 0; j < k; j++) {
      l = d[j].first;
      r = d[j].second + 1;
      cur[i + l] += res;
      cur[i + r] -= res;
    }
    ans[i + 1] = (ans[i] + cur[i + 1]) % mod;
    res = (ans[i] + cur[i + 1]) % mod;
  }

  ans[n] %= mod;
  if (ans[n] < 0) {
    ans[n] += mod;
  }
  cout << ans[n] << endl;
  return 0;
}

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

int main() {
  long long n, k, i, j, mod = 998244353;
  cin >> n >> k;

  vector<pair<long, long>> d(k);
  for (j = 0; j < k; j++) {
    cin >> d[j].first >> d[j].second;
  }

  vector<long long> ans(n + 1 + 100000, 0), cur(n + 1 + 100000, 0);
  long long l, r, res;

  res = 1;
  for (i = 1; i < n; i++) {
    for (j = 0; j < k; j++) {
      l = d[j].first;
      r = d[j].second + 1;
      if (i + l > n) {
        l = n + 1 - i;
      }
      if (i + r > n) {
        r = n + 1 - i;
      }
      cur[i + l] += res;
      cur[i + r] -= res;
    }
    ans[i + 1] = (ans[i] + cur[i + 1]) % mod;
    res = (ans[i] + cur[i + 1]) % mod;
  }

  ans[n] %= mod;
  if (ans[n] < 0) {
    ans[n] += mod;
  }
  cout << ans[n] << endl;
  return 0;
}
Problem p02549 (C++)

Buggy submission (status: 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 MOD = 998244353;
const int INF = 1001001001;
// const ll INF = 1000000000000000000LL;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<pair<ll, ll>> pr(k);
  rep(i, k) {
    ll a, b;
    cin >> a >> b;
    pr[i].first = a;
    pr[i].second = b;
  }
  sort(pr.begin(), pr.end());

  vector<ll> dp(n);
  vector<ll> dp_sum(n + 1);
  dp[0] = 1;
  dp_sum[0] = 0;
  dp_sum[1] = 1;
  for (int i = 1; i < n; i++) {
    rep(j, k) {
      ll cur_l = pr[j].first;
      ll cur_r = pr[j].second;
      if (i - cur_l < 0)
        break;
      if (i - cur_r >= 0) {
        // あとで
        dp[i] += dp_sum[i - cur_l + 1] - dp_sum[i - cur_r];
        dp[i] %= MOD;
      } else {
        for (int k = cur_l; k <= cur_r; k++) {
          if (i - k >= 0) {
            dp[i] += dp[i - k];
            dp[i] %= MOD;
          } else
            break;
        }
      }
    }
    dp_sum[i + 1] = dp[i] + dp_sum[i];
    dp_sum[i + 1] %= MOD;
  }
  if (dp[n - 1] < 0)
    dp[n - 1] += MOD;
  cout << dp[n - 1] << endl;
  return 0;
}

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 MOD = 998244353;
const int INF = 1001001001;
// const ll INF = 1000000000000000000LL;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<pair<ll, ll>> pr(k);
  rep(i, k) {
    ll a, b;
    cin >> a >> b;
    pr[i].first = a;
    pr[i].second = b;
  }
  sort(pr.begin(), pr.end());

  vector<ll> dp(n);
  vector<ll> dp_sum(n + 1);
  dp[0] = 1;
  dp_sum[0] = 0;
  dp_sum[1] = 1;
  for (int i = 1; i < n; i++) {
    rep(j, k) {
      ll cur_l = pr[j].first;
      ll cur_r = pr[j].second;
      if (i - cur_l < 0)
        break;
      if (i - cur_r < 0)
        cur_r = i;
      // if(i-cur_r>=0){
      // あとで
      dp[i] += dp_sum[i - cur_l + 1] - dp_sum[i - cur_r];
      dp[i] %= MOD;
      // }
      // else{
      //     for(int k=cur_l;k<=cur_r;k++){
      //         if(i-k>=0){
      //             dp[i] += dp[i-k];
      //             dp[i]%=MOD;
      //         }
      //         else
      //             break;
      //     }
      // }
    }
    dp_sum[i + 1] = dp[i] + dp_sum[i];
    dp_sum[i + 1] %= MOD;
  }
  if (dp[n - 1] < 0)
    dp[n - 1] += MOD;
  cout << dp[n - 1] << endl;
  return 0;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define ntest 0
#define l first
#define r second

using namespace std;
const int mod = 998244353;
int n, k;
pair<int, int> seg[11];
long long dp[100005], f[100005];
void solve() {
  cin >> n >> k;
  for (int i = 1; i <= k; ++i)
    cin >> seg[i].l >> seg[i].r;
  dp[1] = 1;
  f[1] = 1;
  for (int i = 2; i <= n; ++i) {
    for (int j = 1; j <= k; ++j) {
      int s = max(0, i - seg[j].l);
      int e = max(1, i - seg[j].r);
      dp[i] = (dp[i] + (f[s] - f[e - 1] + mod) % mod) % mod;
    }
    f[i] = (f[i - 1] + dp[i]) % mod;
  }
  cout << dp[n];
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  if (ntest) {
    int t;
    cin >> t;
    while (t--)
      solve();
  } else {
    solve();
  }
}


Fixed submission:
#include <bits/stdc++.h>

#define ntest 0
#define l first
#define r second

using namespace std;
const int mod = 998244353;
int n, k;
pair<int, int> seg[11];
long long dp[200005], f[200005];
void solve() {
  cin >> n >> k;
  for (int i = 1; i <= k; ++i)
    cin >> seg[i].l >> seg[i].r;
  dp[1] = 1;
  f[1] = 1;
  for (int i = 2; i <= n; ++i) {
    for (int j = 1; j <= k; ++j) {
      int s = max(0, i - seg[j].l);
      int e = max(1, i - seg[j].r);
      dp[i] = (dp[i] + (f[s] - f[e - 1] + mod) % mod) % mod;
    }
    f[i] = (f[i - 1] + dp[i]) % mod;
  }
  cout << dp[n];
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  if (ntest) {
    int t;
    cin >> t;
    while (t--)
      solve();
  } else {
    solve();
  }
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(a) (a).begin(), (a).end()
const ll MOD = 998244353;

int main() {
  int N, K, L[10], R[10];
  cin >> N >> K;

  REP(i, K) {
    cin >> L[i] >> R[i];
    R[i]++;
  }

  ll dp[200001] = {};
  dp[0] = 1;
  FOR(i, 1, N) {
    dp[i] += dp[i - 1];
    dp[i] %= MOD;
    REP(j, K) {
      dp[i + L[j]] += dp[i];
      dp[i + L[j]] %= MOD;
      dp[i + R[j]] -= dp[i];
    }
  }
  cout << dp[N] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(a) (a).begin(), (a).end()
const ll MOD = 998244353;

int main() {
  int N, K, L[10], R[10];
  cin >> N >> K;

  REP(i, K) {
    cin >> L[i] >> R[i];
    R[i]++;
  }

  ll dp[200001] = {};
  dp[0] = 1;
  FOR(i, 1, N) {
    dp[i] += dp[i - 1];
    dp[i] %= MOD;
    REP(j, K) {
      if (i + L[j] <= N) {
        dp[i + L[j]] += dp[i];
        dp[i + L[j]] %= MOD;
      }
      if (i + R[j] <= N) {
        dp[i + R[j]] -= dp[i] - MOD;
        dp[i + R[j]] %= MOD;
      }
    }
  }
  cout << dp[N] << endl;
  return 0;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
const int mod = 998244353;
const int N = 1e5 + 10;
int a[N];
int f[N];
struct node {
  int s[2];
} T[N];
bool cmp(node t1, node t2) { return t1.s[0] < t2.s[0]; }
int main() {
  f[1] = 1;
  f[2] = -1;
  a[1] = 1;
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= k; ++i)
    cin >> T[i].s[0] >> T[i].s[1];
  // for(int i=1;i<=k;++i){
  //     cout<<T[i].s[0]<<' '<<T[i].s[1]<<endl;
  // }
  sort(T + 1, T + k + 1, cmp);
  for (int i = 1; i <= n; ++i) {
    if (i > 2)
      f[i] = (f[i - 1] + a[i]) % mod;
    else if (i == 2)
      f[i] = a[i];
    // else if(i==2)f[2]=f[i-1]+f[i];
    for (int j = 1; j <= k; ++j) {
      if (i + T[j].s[0] <= n)
        (a[i + T[j].s[0]] += f[i]) %= mod;
      if (i + T[j].s[1] + 1 <= n)
        (((a[i + T[j].s[1] + 1] -= f[i]) %= mod) += mod) %= mod;
    }
  }
  // for(int i=1;i<=n;++i){
  //     cout<<f[i]<<' ';
  // }
  // cout<<endl;
  cout << f[n] << endl;
  // int sum=0;
  // for(int i=1;i<=n;i++)sum+=f[i];
  // cout<<sum<<endl;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
const int mod = 998244353;
const int N = 2e5 + 10;
int a[N];
int f[N];
struct node {
  int s[2];
} T[N];
bool cmp(node t1, node t2) { return t1.s[0] < t2.s[0]; }
int main() {
  f[1] = 1;
  f[2] = -1;
  a[1] = 1;
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= k; ++i)
    cin >> T[i].s[0] >> T[i].s[1];
  // for(int i=1;i<=k;++i){
  //     cout<<T[i].s[0]<<' '<<T[i].s[1]<<endl;
  // }
  sort(T + 1, T + k + 1, cmp);
  for (int i = 1; i <= n; ++i) {
    if (i > 2)
      f[i] = (f[i - 1] + a[i]) % mod;
    else if (i == 2)
      f[i] = a[i];
    // else if(i==2)f[2]=f[i-1]+f[i];
    for (int j = 1; j <= k; ++j) {
      if (i + T[j].s[0] <= n)
        (a[i + T[j].s[0]] += f[i]) %= mod;
      if (i + T[j].s[1] + 1 <= n)
        (((a[i + T[j].s[1] + 1] -= f[i]) %= mod) += mod) %= mod;
    }
  }
  // for(int i=1;i<=n;++i){
  //     cout<<f[i]<<' ';
  // }
  // cout<<endl;
  cout << f[n] << endl;
  // int sum=0;
  // for(int i=1;i<=n;i++)sum+=f[i];
  // cout<<sum<<endl;
}
Problem p02549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pii pair<int, int>
ll dp[(ll)(2e5 + 2)];
ll MOD = 998244353;
int main() {
  memset(dp, 0, sizeof dp);
  int n, k;
  cin >> n >> k;
  vector<pii> v(k);
  for (int i = 0; i < k; i++) {
    cin >> v[i].first >> v[i].second;
  }
  // int ans=0;
  dp[0] = 1;
  vector<ll> pref(n + 1);
  ll pref_sum = 0;
  for (int i = 0; i < n; i++) {
    dp[i] += pref_sum;
    for (int j = 0; j < k; j++) {
      int L = v[j].first;
      int R = v[j].second;
      if (i + L < n)
        pref[i + L] += dp[i];
      // pref[i+L] = pref[i+L]%MOD;
      if (pref[i + L] > MOD)
        pref[i + L] -= MOD;
      if (i + R + 1 < n) {
        pref[i + R + 1] -= dp[i];
        if (pref[i + R + 1] < 0)
          pref[i + R + 1] += MOD;
      }
    }
    pref_sum += pref[i + 1];
    if (pref_sum > MOD)
      pref_sum -= MOD;
    if (pref_sum < 0)
      pref_sum += MOD;
  }
  cout << dp[n - 1] % MOD << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pii pair<int, int>
ll dp[(ll)(2e5 + 2)];
ll MOD = 998244353;
int main() {
  memset(dp, 0, sizeof dp);
  int n, k;
  cin >> n >> k;
  vector<pii> v(k);
  for (int i = 0; i < k; i++) {
    cin >> v[i].first >> v[i].second;
  }
  // int ans=0;
  dp[0] = 1;
  vector<ll> pref(n + 1);
  ll pref_sum = 0;
  for (int i = 0; i < n; i++) {
    dp[i] += pref_sum;
    for (int j = 0; j < k; j++) {
      int L = v[j].first;
      int R = v[j].second;
      if (i + L < n)
        pref[i + L] += dp[i];
      // pref[i+L] = pref[i+L]%MOD;
      if (i + L < n && pref[i + L] > MOD)
        pref[i + L] -= MOD;
      if (i + R + 1 < n) {
        pref[i + R + 1] -= dp[i];
        if (pref[i + R + 1] < 0)
          pref[i + R + 1] += MOD;
      }
    }
    pref_sum += pref[i + 1];
    if (pref_sum > MOD)
      pref_sum -= MOD;
    if (pref_sum < 0)
      pref_sum += MOD;
  }
  cout << dp[n - 1] % MOD << endl;
}
Problem p02550 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  vector<bool> check(m + 1, false);
  vector<ll> a;
  ll rs;
  check[x] = true;
  a.push_back(x);

  while (1) {
    ll f = (x * x) % m;
    if (check[f] == true) {
      rep(i, a.size()) {
        if (a[i] == f)
          rs = i;
      }
      break;
    } else {
      check[f] = true;
    }
    x = f;
    a.push_back(x);
  }
  ll roop = a.size() - rs;
  ll ans = 0;
  vector<ll> asum(a.size());

  rep(i, a.size() + 1) {
    if (i == 0)
      asum[i] = a[i];
    else
      asum[i] = asum[i - 1] + a[i];
  }

  if (n < rs) {
    ans = asum[n];
  } else {
    ans = asum[rs] - a[rs];
    n = n - rs;
    ans += (n / roop) * (asum[asum.size() - 1] - (asum[rs] - a[rs])) +
           ((asum[n % roop + rs] - a[n % roop + rs]) - (asum[rs] - a[rs]));
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  ll n, x, m;
  cin >> n >> x >> m;
  vector<bool> check(m + 1, false);
  vector<ll> a;
  ll rs;
  check[x] = true;
  a.push_back(x);

  while (1) {
    ll f = (x * x) % m;
    if (check[f] == true) {
      rep(i, a.size()) {
        if (a[i] == f)
          rs = i;
      }
      break;
    } else {
      check[f] = true;
    }
    x = f;
    a.push_back(x);
  }
  ll roop = a.size() - rs;
  ll ans = 0;
  vector<ll> asum(a.size());

  rep(i, a.size()) {
    if (i == 0)
      asum[i] = a[i];
    else
      asum[i] = asum[i - 1] + a[i];
  }

  if (n < rs) {
    ans = asum[n];
  } else {
    ans = asum[rs] - a[rs];
    n = n - rs;
    ans += (n / roop) * (asum[asum.size() - 1] - (asum[rs] - a[rs])) +
           ((asum[n % roop + rs] - a[n % roop + rs]) - (asum[rs] - a[rs]));
  }

  cout << ans << endl;

  return 0;
}
Problem p02550 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
int INF = 1e18 + 7;
int mod = 998244353;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
signed main() {
  int N, X, M;
  cin >> N >> X >> M;
  vector<int> used(100005);
  int old = X, j = 1;
  used[X] = true;
  while (true) {
    old = old * old % M;
    if (used[old]) {
      break;
    }
    used[old] = true;
    j++;
  }
  int sum = X;
  int now = X;
  old = X;
  for (int i = 2; i <= min(N, j); i++) {
    sum += old * old % M;
    old = old * old % M;
    now = old;
  }
  vector<int> s(100005);
  int cnt = 1;
  while (true) {
    old = old * old % M;
    s[cnt] = s[cnt - 1] + old;
    if (old == now) {
      break;
    }
    cnt++;
  }
  cout << sum + (N - min(N, j)) / cnt * s[cnt] + s[(N - min(N, j)) % cnt]
       << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
int INF = 1e18 + 7;
int mod = 998244353;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
signed main() {
  int N, X, M;
  cin >> N >> X >> M;
  vector<int> used(100005);
  int old = X, j = 1;
  used[X] = true;
  while (true) {
    old = old * old % M;
    if (used[old]) {
      break;
    }
    used[old] = true;
    j++;
  }
  int sum = X;
  int now = X;
  old = X;
  for (int i = 2; i <= min(N, j); i++) {
    sum += old * old % M;
    old = old * old % M;
    now = old;
  }
  if (N <= j) {
    cout << sum << endl;
    return 0;
  }
  vector<int> s(100005);
  int cnt = 1;
  while (true) {
    old = old * old % M;
    s[cnt] = s[cnt - 1] + old;
    if (old == now) {
      break;
    }
    cnt++;
  }
  cout << sum + (N - min(N, j)) / cnt * s[cnt] + s[(N - min(N, j)) % cnt]
       << endl;
}
Problem p02550 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  int x, m;
  vector<int> check(m + 5, 0);
  vector<long long> store(m + 5, 0);

  cin >> n >> x >> m;
  check[x] = 1;
  store[1] = x;
  long long next = x;
  long long ans = x;
  for (long long i = 2; i <= n; i++) {
    next = (next * next) % m;
    if (check[next]) {
      long long sum = 0;
      for (long long j = check[next]; j < i; j++) {
        sum += store[j];
      }
      long long length = i - check[next];
      long long togo = n - i + 1;
      ans += (togo / length) * sum;
      for (long long j = check[next]; j < check[next] + togo % length; j++) {
        ans += store[j];
      }
      break;
    } else {
      ans += next;
      check[next] = i;
      store[i] = next;
    }
  }
  cout << ans << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  int x, m;
  vector<int> check(200001, 0);
  vector<long long> store(200001, 0);

  cin >> n >> x >> m;
  check[x] = 1;
  store[1] = x;
  long long next = x;
  long long ans = x;
  for (long long i = 2; i <= n; i++) {
    next = (next * next) % m;
    if (check[next]) {
      long long sum = 0;
      for (long long j = check[next]; j < i; j++) {
        sum += store[j];
      }
      long long length = i - check[next];
      long long togo = n - i + 1;
      ans += (togo / length) * sum;
      for (long long j = check[next]; j < check[next] + togo % length; j++) {
        ans += store[j];
      }
      break;
    } else {
      ans += next;
      check[next] = i;
      store[i] = next;
    }
  }
  cout << ans << '\n';
  return 0;
}