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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MOD = pow(10, 9) + 7;
// const int MOD = 998244353;
// const int MOD = ;
int mod(int A, int M) { return (A % M + M) % M; }
const int INF = 1LL << 60;
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int divCeil(int A, int B) { return (A + (B - 1)) / B; }
int myctoi(char C) { return C - 48; }
char myitoc(int N) { return '0' + N; }

signed main() {
  int N;
  cin >> N;
  vector<pair<int, int>> a(N);
  for (int i = 0; i < N; i++) {
    int a0;
    cin >> a0;
    a.at(i) = make_pair(a0, i + 1);
  }

  sort(a.begin(), a.end());

  int ans = 0;
  set<int> b;
  b.emplace(0);
  b.emplace(N + 1);
  for (int i = 0; i < N; i++) {
    int x = a.at(i).first;
    int y = a.at(i).second;

    auto itr = lower_bound(b.begin(), b.end(), y);
    itr--;
    int y0 = *itr;
    itr++;
    int y1 = *itr;
    b.emplace(y);

    ans += x * (y1 - y) * (y - y0);
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MOD = pow(10, 9) + 7;
// const int MOD = 998244353;
// const int MOD = ;
int mod(int A, int M) { return (A % M + M) % M; }
const int INF = 1LL << 60;
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int divCeil(int A, int B) { return (A + (B - 1)) / B; }
int myctoi(char C) { return C - 48; }
char myitoc(int N) { return '0' + N; }

signed main() {
  int N;
  cin >> N;
  vector<pair<int, int>> a(N);
  for (int i = 0; i < N; i++) {
    int a0;
    cin >> a0;
    a.at(i) = make_pair(a0, i + 1);
  }

  sort(a.begin(), a.end());

  int ans = 0;
  set<int> b;
  b.emplace(0);
  b.emplace(N + 1);
  for (int i = 0; i < N; i++) {
    int x = a.at(i).first;
    int y = a.at(i).second;

    auto itr = b.lower_bound(y);
    itr--;
    int y0 = *itr;
    itr++;
    int y1 = *itr;
    b.emplace(y);

    ans += x * (y1 - y) * (y - y0);
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef tuple<int, int, int> t3;

const int MX = 1 << 17;
const int MM = 1000000007;

struct UF {
  int t[MX], c[MX];
  void clear() {
    for (int i = 0; i < MX; i++)
      t[i] = i, c[i] = 1;
  }
  int find(int a) { return t[a] == a ? a : t[a] = find(t[a]); }
  int merge(int a, int b) {
    a = find(a), b = find(b);
    t[a] = b, c[b] += c[a];
  }
} uf;

int D[MX], E[MX];
int N;

int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++)
    scanf("%d", D + i), E[i] = i;
  sort(E + 1, E + N + 1, [](int l, int r) { return D[l] > D[r]; });
  uf.clear();
  ll ans = 0;
  for (int i = 1; i <= N; i++) {
    int c = E[i];
    int a = 1, b = 1;
    if (c >= 2 && D[c - 1] > D[c])
      a += uf.c[uf.find(c - 1)], uf.merge(c, c - 1);
    if (c < N && D[c + 1] > D[c])
      b += uf.c[uf.find(c + 1)], uf.merge(c, c + 1);
    ans += (ll)a * b * D[c];
  }
  printf("%lld\n", ans);
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef tuple<int, int, int> t3;

const int MX = 200005;
const int MM = 1000000007;

struct UF {
  int t[MX], c[MX];
  void clear() {
    for (int i = 0; i < MX; i++)
      t[i] = i, c[i] = 1;
  }
  int find(int a) { return t[a] == a ? a : t[a] = find(t[a]); }
  int merge(int a, int b) {
    a = find(a), b = find(b);
    t[a] = b, c[b] += c[a];
  }
} uf;

int D[MX], E[MX];
int N;

int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++)
    scanf("%d", D + i), E[i] = i;
  sort(E + 1, E + N + 1, [](int l, int r) { return D[l] > D[r]; });
  uf.clear();
  ll ans = 0;
  for (int i = 1; i <= N; i++) {
    int c = E[i];
    int a = 1, b = 1;
    if (c >= 2 && D[c - 1] > D[c])
      a += uf.c[uf.find(c - 1)], uf.merge(c, c - 1);
    if (c < N && D[c + 1] > D[c])
      b += uf.c[uf.find(c + 1)], uf.merge(c, c + 1);
    ans += (ll)a * b * D[c];
  }
  printf("%lld\n", ans);
}
Problem p03987 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int n, a[100009], s[100009];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cin >> n;
  int c = 1;
  long long cur = 0, sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    while (a[i] <= a[s[c - 1]]) {
      c--;
      cur -= 1LL * a[s[c]] * (s[c] - s[c - 1]);
    }
    cur += 1LL * a[i] * (i - s[c - 1]);
    s[c++] = i;
    sum += cur;
  }
  cout << sum << "\n";
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int n, a[200009], s[200009];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cin >> n;
  int c = 1;
  long long cur = 0, sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    while (a[i] <= a[s[c - 1]]) {
      c--;
      cur -= 1LL * a[s[c]] * (s[c] - s[c - 1]);
    }
    cur += 1LL * a[i] * (i - s[c - 1]);
    s[c++] = i;
    sum += cur;
  }
  cout << sum << "\n";
  return 0;
}
Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

int N;
int A[200000];
int B[200000];
long long C[200000];

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    B[A[i] - 1] = i;
  }
  set<int> g;
  for (int i = 0; i < N; i++) {
    long long s = 0;
    auto it = upper_bound(g.begin(), g.end(), B[i]);
    if (it != g.end()) {
      int x = *it;
      s = (long long)(i + 1) * (x - B[i]) + C[x];
    } else {
      s = (long long)(i + 1) * (N - B[i]);
    }
    C[B[i]] = s;
    g.insert(B[i]);
  }
  long long s = 0;
  for (int i = 0; i < N; i++)
    s += C[i];
  cout << s << "\n";
  return 0;
}


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

int N;
int A[200000];
int B[200000];
long long C[200000];

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    B[A[i] - 1] = i;
  }
  set<int> g;
  for (int i = 0; i < N; i++) {
    long long s = 0;
    auto it = g.upper_bound(B[i]);
    if (it != g.end()) {
      int x = *it;
      s = (long long)(i + 1) * (x - B[i]) + C[x];
    } else {
      s = (long long)(i + 1) * (N - B[i]);
    }
    C[B[i]] = s;
    g.insert(B[i]);
  }
  long long s = 0;
  for (int i = 0; i < N; i++)
    s += C[i];
  cout << s << "\n";
  return 0;
}
Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ALL(x) (x).begin(), (x).end()
#define dump(x) cout << (x) << endl
#define LMAX 9223372036854775807LL
#define LMIN -9223372036854775807LL
using ll = long long;
template <typename T> using vec = vector<T>;
using P = pair<ll, ll>;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}

const ll INF = 1e16;
const ll MOD = 1e9 + 7;
// int dx[4] = {1,0,-1,0};
// int dy[4] = {0,1,0,-1};
// ll pas[51][52];

signed main() {
  IOS;
  ll n;
  cin >> n;
  set<ll> s;
  vec<ll> a(n + 1);
  REP(i, n) {
    int x;
    cin >> x;
    a[x] = i;
  }
  ll ans = 0;
  s.insert(-1);
  s.insert(n);
  for (ll i = 1; i <= n; i++) {
    auto it = upper_bound(ALL(s), a[i]);
    ll r = *it, l = *(--it);
    ans += i * (r - a[i]) * (a[i] - l);
    s.insert(a[i]);
  }
  dump(ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ALL(x) (x).begin(), (x).end()
#define dump(x) cout << (x) << endl
#define LMAX 9223372036854775807LL
#define LMIN -9223372036854775807LL
using ll = long long;
template <typename T> using vec = vector<T>;
using P = pair<ll, ll>;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}

const ll INF = 1e16;
const ll MOD = 1e9 + 7;
// int dx[4] = {1,0,-1,0};
// int dy[4] = {0,1,0,-1};
// ll pas[51][52];

signed main() {
  IOS;
  ll n;
  cin >> n;
  set<ll> s;
  vec<ll> a(n + 1);
  REP(i, n) {
    int x;
    cin >> x;
    a[x] = i;
  }
  ll ans = 0;
  s.insert(-1);
  s.insert(n);
  for (ll i = 1; i <= n; i++) {
    auto it = s.upper_bound(a[i]);
    ll r = *it, l = *(--it);
    ans += i * (r - a[i]) * (a[i] - l);
    s.insert(a[i]);
  }
  dump(ans);
}
Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
map<int, int> a;
set<int> s;

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a[x] = i;
  }
  ll ans = 0;
  s.insert(-1);
  s.insert(n);
  for (int i = 1; i <= n; i++) {
    int pos = a[i];
    auto it = lower_bound(s.begin(), s.end(), pos);
    int r = *it;
    it--;
    int l = *it;
    s.insert(pos);
    ans += (ll)i * (r - pos) * (pos - l);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
map<int, int> a;
set<int> s;

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a[x] = i;
  }
  ll ans = 0;
  s.insert(-1);
  s.insert(n);
  for (int i = 1; i <= n; i++) {
    int pos = a[i];
    auto it = s.lower_bound(pos);
    int r = *it;
    it--;
    int l = *it;
    s.insert(pos);
    ans += (ll)i * (r - pos) * (pos - l);
  }
  cout << ans << endl;
  return 0;
}
Problem p03987 (C++)

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <typename T> void vprint(T &v) {
  REP(i, v.size()) { cout << v[i] << " "; }
  cout << endl;
}

int main() {
  ll N;
  cin >> N;
  vector<ll> a(N);
  REP(i, N) cin >> a[i];

  set<ll> st;
  st.insert(N);
  st.insert(-1);
  ll res = 0;
  vector<ll> place(N + 1);
  REP(i, N) place[a[i]] = i;

  for (int i = 1; i <= N; i++) {
    auto itr = lower_bound(st.begin(), st.end(), place[i]);
    ll migi = *itr;
    itr--;
    ll hidari = *itr;
    res += i * (migi - place[i]) * (place[i] - hidari);
    st.insert(place[i]);
    /*
    dump(i)
    dump(migi)
    dump(hidari)
    dump(place[i])
    cout << endl;
    */
  }
  cout << res << endl;
  return 0;
}

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <typename T> void vprint(T &v) {
  REP(i, v.size()) { cout << v[i] << " "; }
  cout << endl;
}

int main() {
  ll N;
  cin >> N;
  vector<ll> a(N);
  REP(i, N) cin >> a[i];

  set<ll> st;
  st.insert(N);
  st.insert(-1);
  ll res = 0;
  vector<ll> place(N + 1);
  REP(i, N) place[a[i]] = i;

  for (int i = 1; i <= N; i++) {
    auto itr = st.lower_bound(place[i]);
    ll migi = *itr;
    itr--;
    ll hidari = *itr;
    res += i * (migi - place[i]) * (place[i] - hidari);
    st.insert(place[i]);
    /*
    dump(i)
    dump(migi)
    dump(hidari)
    dump(place[i])
    cout << endl;
    */
  }
  cout << res << endl;
  return 0;
}
Problem p03987 (C++)

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

using namespace std;
typedef long long ll;
const int MAX_N = 100100;
int n;
ll h[MAX_N];

ll L[MAX_N], R[MAX_N];
ll st[MAX_N];

void solve() {

  int t = 0;
  for (int i = 0; i < n; i++) {
    while (t > 0 && h[st[t - 1]] >= h[i]) {
      t--;
    }
    L[i] = t == 0 ? 0 : (st[t - 1] + 1);
    st[t++] = i;
  }

  t = 0;
  for (int i = n - 1; i >= 0; i--) {
    while (t > 0 && h[st[t - 1]] >= h[i]) {
      t--;
    }
    R[i] = t == 0 ? n : st[t - 1];
    st[t++] = i;
  }

  ll ans = 0;
  for (int i = 0; i < n; i++) { // ここを改変する

    ll res = (i - L[i] + 1) * (R[i] - i);
    ans += res * h[i];
  }

  cout << ans << endl;
}

int main() {

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }

  solve();

  return 0;
}

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

using namespace std;
typedef long long ll;
const int MAX_N = 200100;
int n;
ll h[MAX_N];

ll L[MAX_N], R[MAX_N];
ll st[MAX_N];

void solve() {

  int t = 0;
  for (int i = 0; i < n; i++) {
    while (t > 0 && h[st[t - 1]] >= h[i]) {
      t--;
    }
    L[i] = t == 0 ? 0 : (st[t - 1] + 1);
    st[t++] = i;
  }

  t = 0;
  for (int i = n - 1; i >= 0; i--) {
    while (t > 0 && h[st[t - 1]] >= h[i]) {
      t--;
    }
    R[i] = t == 0 ? n : st[t - 1];
    st[t++] = i;
  }

  ll ans = 0;
  for (int i = 0; i < n; i++) { // ここを改変する

    ll res = (i - L[i] + 1) * (R[i] - i);
    ans += res * h[i];
  }

  cout << ans << endl;
}

int main() {

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }

  solve();

  return 0;
}
Problem p03987 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main() {
  int N;
  scanf("%d", &N);
  vector<ll> h(N);
  for (int i = 0; i < N; i++) {
    int a;
    scanf("%lld", &a);
    a--;
    h[a] = i;
  }

  set<ll> s;

  s.insert(-1);
  s.insert(N);
  ll ans = 0;
  for (int a = 0; a < N; a++) {
    s.insert(h[a]);
    auto itr = lower_bound(s.begin(), s.end(), h[a]);

    auto r1 = itr;
    auto r2 = itr;
    r1--;
    r2++;

    ans += (a + 1) * (*r2 - *itr) * (*itr - *r1);
  }

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

  return 0;
}

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

using namespace std;
typedef long long ll;

int main() {
  int N;
  scanf("%d", &N);
  vector<ll> h(N);
  for (int i = 0; i < N; i++) {
    int a;
    scanf("%lld", &a);
    a--;
    h[a] = i;
  }

  set<ll> s;

  s.insert(-1);
  s.insert(N);
  ll ans = 0;
  for (int a = 0; a < N; a++) {
    s.insert(h[a]);
    auto itr = s.lower_bound(h[a]);

    auto r1 = itr;
    auto r2 = itr;
    r1--;
    r2++;

    ans += (a + 1) * (*r2 - *itr) * (*itr - *r1);
  }

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

  return 0;
}
Problem p03987 (C++)

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

const int maxn = 100005;

int n, l, r;
int a[maxn], pos[maxn];
long long ans;
set<int> border;
set<int>::iterator j;

signed main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    pos[a[i]] = i;
  }
  for (int i = 1; i <= n; ++i) {
    l = 0, r = n + 1;
    j = border.upper_bound(pos[i]);
    if (j != border.end())
      r = *j;
    if (j != border.begin())
      l = *prev(j);
    ans += ((long long)i) * ((long long)pos[i] - l) * ((long long)r - pos[i]);
    border.insert(pos[i]);
  }
  printf("%lld\n", ans);
}


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

const int maxn = 200005;

int n, l, r;
int a[maxn], pos[maxn];
long long ans;
set<int> border;
set<int>::iterator j;

signed main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    pos[a[i]] = i;
  }
  for (int i = 1; i <= n; ++i) {
    l = 0, r = n + 1;
    j = border.upper_bound(pos[i]);
    if (j != border.end())
      r = *j;
    if (j != border.begin())
      l = *prev(j);
    ans += ((long long)i) * ((long long)pos[i] - l) * ((long long)r - pos[i]);
    border.insert(pos[i]);
  }
  printf("%lld\n", ans);
}
Problem p03987 (C++)

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

using namespace std;

int main() {

  long long n, a[20000], pehele[20000], baadme[20000], ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  stack<int> low;
  stack<int> up;
  a[0] = 0;
  a[n + 1] = 0;
  low.push(0);
  up.push(n + 1);
  for (int i = 1; i <= n; i++) {
    while (true) {
      if (a[low.top()] < a[i]) {
        pehele[i] = low.top();
        break;
      } else
        low.pop();
    }
    low.push(i);
  }
  for (int i = n; i > 0; i--) {

    while (true) {

      if (a[up.top()] < a[i]) {
        baadme[i] = up.top();
        break;
      } else
        up.pop();
    }
    up.push(i);
  }
  for (int i = 1; i <= n; i++) {
    ans += a[i] * (baadme[i] - i) * (i - pehele[i]);
  }
  cout << ans;
  return 0;
}


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

using namespace std;

int main() {

  long long n, a[200005], pehele[200005], baadme[200005], ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  stack<int> low;
  stack<int> up;
  a[0] = 0;
  a[n + 1] = 0;
  low.push(0);
  up.push(n + 1);
  for (int i = 1; i <= n; i++) {
    while (true) {
      if (a[low.top()] < a[i]) {
        pehele[i] = low.top();
        break;
      } else
        low.pop();
    }
    low.push(i);
  }
  for (int i = n; i > 0; i--) {

    while (true) {

      if (a[up.top()] < a[i]) {
        baadme[i] = up.top();
        break;
      } else
        up.pop();
    }
    up.push(i);
  }
  for (int i = 1; i <= n; i++) {
    ans += a[i] * (baadme[i] - i) * (i - pehele[i]);
  }
  cout << ans;
  return 0;
}
Problem p03988 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 1000000000
#define LLINF 1000000000000000ll
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define repn(i, a, n) for (int(i) = (a); (i) < (int)(n); (i)++)
#define EQ(a, b) (abs((a) - (b)) < eps)
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}
int N;
int a[105];
int cnt[105];
int r = 0;
void fail() {
  cout << "Impossible" << endl;
  exit(1);
  return;
}
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    r = max(r, a[i]);
  }
  if (r & 1) {
    for (int i = 0; i < N; i++) {
      a[i] = a[i] * 2 - r;
      if (a[i] < 0)
        fail();
      cnt[a[i]]++;
    }
    if (cnt[1] != 2)
      fail();
    for (int i = 3; i <= r; i += 2) {
      if (cnt[i] < 2)
        fail();
    }
    cout << "Possible" << endl;
  } else {
    r /= 2;
    for (int i = 0; i < N; i++) {
      a[i] -= r;
      if (a[i] < 0)
        fail();
      cnt[a[i]]++;
    }
    if (cnt[0] != 1)
      fail();
    for (int i = 1; i <= r; i++) {
      if (cnt[i] < 2)
        fail();
    }
    cout << "Possible" << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 1000000000
#define LLINF 1000000000000000ll
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define repn(i, a, n) for (int(i) = (a); (i) < (int)(n); (i)++)
#define EQ(a, b) (abs((a) - (b)) < eps)
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}
int N;
int a[105];
int cnt[105];
int r = 0;
void fail() {
  cout << "Impossible" << endl;
  exit(0);
  return;
}
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    r = max(r, a[i]);
  }
  if (r & 1) {
    for (int i = 0; i < N; i++) {
      a[i] = a[i] * 2 - r;
      if (a[i] < 0)
        fail();
      cnt[a[i]]++;
    }
    if (cnt[1] != 2)
      fail();
    for (int i = 3; i <= r; i += 2) {
      if (cnt[i] < 2)
        fail();
    }
    cout << "Possible" << endl;
  } else {
    r /= 2;
    for (int i = 0; i < N; i++) {
      a[i] -= r;
      if (a[i] < 0)
        fail();
      cnt[a[i]]++;
    }
    if (cnt[0] != 1)
      fail();
    for (int i = 1; i <= r; i++) {
      if (cnt[i] < 2)
        fail();
    }
    cout << "Possible" << endl;
  }
  return 0;
}
Problem p03988 (C++)

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

using namespace std;

int n, cnt[1 << 20], d;

int main() {
  int i, x;

  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &x);
    ++cnt[x];
    d = max(d, x);
  }
  if (d == 1 && n != 2) {
    printf("Impossible\n");
    return 0;
  }
  if (d & 1) {
    assert(0);
    for (i = d; i >= ((d + 1) >> 1); i--) {
      if (cnt[i] < 2) {
        printf("Impossible\n");
        return 0;
      }
    }
    for (i = 1; i < ((d + 1) >> 1); i++) {
      if (cnt[i] > 0) {
        printf("Impossible\n");
        return 0;
      }
    }
    printf("Possible\n");
  } else {
    if (cnt[d >> 1] != 1) {
      printf("Impossible\n");
      return 0;
    }
    for (i = d; i > (d >> 1); i--) {
      if (cnt[i] < 2) {
        printf("Impossible\n");
        return 0;
      }
    }
    for (i = 1; i < (d >> 1); i++) {
      if (cnt[i] > 0) {
        printf("Impossible\n");
        return 0;
      }
    }
    printf("Possible\n");
  }

  return 0;
}


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

using namespace std;

int n, cnt[1 << 20], d;

int main() {
  int i, x;

  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &x);
    ++cnt[x];
    d = max(d, x);
  }
  if (d == 1 && n != 2) {
    printf("Impossible\n");
    return 0;
  }
  if (d & 1) {
    if (cnt[((d + 1) >> 1)] != 2) {
      printf("Impossible\n");
      return 0;
    }
    for (i = d; i > ((d + 1) >> 1); i--) {
      if (cnt[i] < 2) {
        printf("Impossible\n");
        return 0;
      }
    }
    for (i = 1; i < ((d + 1) >> 1); i++) {
      if (cnt[i] > 0) {
        printf("Impossible\n");
        return 0;
      }
    }
    printf("Possible\n");
  } else {
    if (cnt[d >> 1] != 1) {
      printf("Impossible\n");
      return 0;
    }
    for (i = d; i > (d >> 1); i--) {
      if (cnt[i] < 2) {
        printf("Impossible\n");
        return 0;
      }
    }
    for (i = 1; i < (d >> 1); i++) {
      if (cnt[i] > 0) {
        printf("Impossible\n");
        return 0;
      }
    }
    printf("Possible\n");
  }

  return 0;
}
Problem p03989 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
const int mu = 924844033;
int n, k, f[2][2005][2], cnt, p[2005], a[2005], now, ans;
int main() {
  scanf("%d%d", &n, &k);
  p[0] = 1;
  for (int i = 1; i <= n; i++)
    p[i] = p[i - 1] * 1ll * i % mu;
  for (int i = 1; i <= k; i++) {
    a[cnt] = -1;
    for (int j = i; j <= n; j += k)
      cnt++;
    a[cnt] = -1;
    for (int j = i; j <= n; j += k)
      cnt++;
  }
  now = 1, f[now][0][0] = 1;
  for (int i = 1; i < cnt; i++) {
    now = now ^ 1;
    memset(f[now], 0, sizeof(f[now]));
    for (int j = 0; j <= i; j++) {
      f[now][j][0] = (f[now][j][0] + f[now ^ 1][j][1] + f[now ^ 1][j][0]) % mu;
      if (j && !a[i])
        f[now][j][1] = (f[now][j][1] + f[now ^ 1][j - 1][0]) % mu;
    }
  }
  ans = p[n];
  for (int i = 1; i <= n; i++) {
    ans = (ans +
           (f[now][i][0] + f[now][i][1]) * (i & 1 ? -1 : 1) * 1ll * p[n - i] %
               mu +
           mu) %
          mu;
  }
  printf("%d\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
const int mu = 924844033;
int n, k, f[2][4005][2], cnt, p[2005], a[4005], now, ans;
int main() {
  scanf("%d%d", &n, &k);
  p[0] = 1;
  for (int i = 1; i <= n; i++)
    p[i] = p[i - 1] * 1ll * i % mu;
  for (int i = 1; i <= k; i++) {
    a[cnt] = -1;
    for (int j = i; j <= n; j += k)
      cnt++;
    a[cnt] = -1;
    for (int j = i; j <= n; j += k)
      cnt++;
  }
  now = 1, f[now][0][0] = 1;
  for (int i = 1; i < cnt; i++) {
    now = now ^ 1;
    memset(f[now], 0, sizeof(f[now]));
    for (int j = 0; j <= i; j++) {
      f[now][j][0] = (f[now][j][0] + f[now ^ 1][j][1] + f[now ^ 1][j][0]) % mu;
      if (j && !a[i])
        f[now][j][1] = (f[now][j][1] + f[now ^ 1][j - 1][0]) % mu;
    }
  }
  ans = p[n];
  for (int i = 1; i <= n; i++) {
    ans = (ans +
           (f[now][i][0] + f[now][i][1]) * (i & 1 ? -1 : 1) * 1ll * p[n - i] %
               mu +
           mu) %
          mu;
  }
  printf("%d\n", ans);
}
Problem p03989 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define Mod 924844033
#define MN 2005
int n, k, id[MN << 1], tot;
LL fac[MN], f[MN][MN][2];
int main() {
  scanf("%d%d", &n, &k);
  fac[0] = 1;
  for (int i = 1; i <= n; ++i)
    fac[i] = (LL)fac[i - 1] * i % Mod;
  for (int i = 1; i <= k; ++i) {
    for (int j = i; j <= n; j += k)
      id[++tot] = j;
    for (int j = i; j <= n; j += k)
      id[++tot] = j;
  }
  f[1][0][0] = 1;
  for (int i = 2; i <= (n << 1); ++i)
    for (int j = 0; j <= i / 2; ++j) {
      f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1]) % Mod;
      if (j && id[i] - id[i - 1] == k)
        f[i][j][1] = f[i - 1][j - 1][0];
    }
  LL ans = fac[n];
  for (int i = 1; i <= n; ++i)
    ans = (ans +
           ((i & 1) ? -1ll : 1ll) * (LL)(f[n << 1][i][0] + f[n << 1][i][1]) *
               (LL)fac[n - i] % Mod +
           Mod) %
          Mod;
  printf("%lld", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define Mod 924844033
#define MN 2005
int n, k, id[MN << 1], tot;
LL fac[MN], f[MN << 1][MN][2];
int main() {
  scanf("%d%d", &n, &k);
  fac[0] = 1;
  for (int i = 1; i <= n; ++i)
    fac[i] = (LL)fac[i - 1] * i % Mod;
  for (int i = 1; i <= k; ++i) {
    for (int j = i; j <= n; j += k)
      id[++tot] = j;
    for (int j = i; j <= n; j += k)
      id[++tot] = j;
  }
  f[1][0][0] = 1;
  for (int i = 2; i <= (n << 1); ++i)
    for (int j = 0; j <= i / 2; ++j) {
      f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1]) % Mod;
      if (j && id[i] - id[i - 1] == k)
        f[i][j][1] = f[i - 1][j - 1][0];
    }
  LL ans = fac[n];
  for (int i = 1; i <= n; ++i)
    ans = (ans +
           ((i & 1) ? -1ll : 1ll) * (LL)(f[n << 1][i][0] + f[n << 1][i][1]) *
               (LL)fac[n - i] % Mod +
           Mod) %
          Mod;
  printf("%lld", ans);
  return 0;
}
Problem p03989 (C++)

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

typedef long long ll;

int N, K;
ll mod = 924844033;
ll dp[4010][4010][2];
ll F[100010] = {1};
bool mem[4010];
ll ans;

int main() {
  scanf("%d%d", &N, &K);

  int now = 0;
  for (int i = 0; i < N % K * 2; i++) {
    now += N / K;
    mem[now] = true;
    now++;
  }
  for (int i = 0; i < (K - N % K) * 2; i++) {
    now += N / K - 1;
    mem[now] = true;
    now++;
  }

  for (int i = 1; i < 100010; i++)
    F[i] = F[i - 1] * i % mod;

  dp[0][0][0] = 1;
  for (int i = 0; i < N * 2; i++) {
    for (int j = 0; j <= N * 2; j++) {
      if (mem[i]) {
        dp[i + 1][j][0] += dp[i][j][1] + dp[i][j][0];
      } else {
        dp[i + 1][j][0] += dp[i][j][0] + dp[i][j][1];
        dp[i + 1][j + 1][1] += dp[i][j][0];
      }
      dp[i + 1][j][0] %= mod;
      dp[i + 1][j + 1][1] %= mod;
    }
  }

  for (int i = 0; i <= N * 2; i++) {
    ll tmp = dp[N * 2][i][0] + dp[N * 2][i][1];
    tmp = tmp * F[N - i] % mod;
    if (i % 2)
      ans -= tmp;
    else
      ans += tmp;
    ans = (ans + mod) % mod;
  }

  printf("%lld\n", ans);
  return 0;
}


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

typedef long long ll;

int N, K;
ll mod = 924844033;
ll dp[4010][4010][2];
ll F[100010] = {1};
bool mem[4010];
ll ans;

int main() {
  scanf("%d%d", &N, &K);

  int now = 0;
  for (int i = 0; i < N % K * 2; i++) {
    now += N / K;
    mem[now] = true;
    now++;
  }
  for (int i = 0; i < (K - N % K) * 2; i++) {
    now += N / K - 1;
    mem[now] = true;
    now++;
  }

  for (int i = 1; i < 100010; i++)
    F[i] = F[i - 1] * i % mod;

  dp[0][0][0] = 1;
  for (int i = 0; i < N * 2; i++) {
    for (int j = 0; j <= N * 2; j++) {
      if (mem[i]) {
        dp[i + 1][j][0] += dp[i][j][1] + dp[i][j][0];
      } else {
        dp[i + 1][j][0] += dp[i][j][0] + dp[i][j][1];
        dp[i + 1][j + 1][1] += dp[i][j][0];
      }
      dp[i + 1][j][0] %= mod;
      dp[i + 1][j + 1][1] %= mod;
    }
  }

  for (int i = 0; i <= N; i++) {
    ll tmp = dp[N * 2][i][0] + dp[N * 2][i][1];
    tmp = tmp * F[N - i] % mod;
    if (i % 2)
      ans -= tmp;
    else
      ans += tmp;
    ans = (ans + mod) % mod;
  }

  printf("%lld\n", ans);
  return 0;
}
Problem p03989 (C++)

Buggy submission (status: Runtime Error):
#include <fstream>
#include <iostream>
using namespace std;
const int nmax = 1005;
const long long mod = 924844033;
long long dp[nmax][nmax];
long long modes[nmax], pond[nmax], fact[nmax];
long long ans;
int n, k, i, j, rr, cate;
int main() {
  // freopen("data.in","r",stdin);
  ios_base::sync_with_stdio(false);
  cin >> n >> k;
  dp[0][0] = 1;
  for (i = 1; i <= n; i++) {
    for (j = 0; j <= i; j++) {
      if (i >= 2 && j)
        dp[i][j] = dp[i - 2][j - 1];
      dp[i][j] += dp[i - 1][j];
      if (dp[i][j] >= mod)
        dp[i][j] -= mod;
    }
  }
  modes[0] = 1;
  for (rr = 0; rr < k; rr++) {
    cate = (n - 1) / k;
    if (rr <= (n - 1) % k)
      cate++;
    for (i = 1; i <= cate; i++)
      pond[i] = (dp[cate][i]) % mod;
    for (int dd = 0; dd < 2; dd++)
      for (i = n; i >= 0; i--)
        for (j = 1; j <= min(n - i, cate); j++)
          modes[i + j] = (modes[i + j] + 1LL * modes[i] * pond[j]) % mod;
  }
  fact[0] = 1;
  for (long long I = 1; I <= n; I++)
    fact[I] = (1LL * fact[I - 1] * I) % mod;
  for (i = 1; i <= n; i++) {
    if ((i & 1))
      ans += (1LL * modes[i] * fact[n - i]) % mod;
    else
      ans -= (1LL * modes[i] * fact[n - i]) % mod;
    if (ans < 0)
      ans += mod;
    if (ans >= mod)
      ans -= mod;
  }
  ans = (fact[n] - ans + mod) % mod;
  cout << ans;
  return 0;
}


Fixed submission:
#include <fstream>
#include <iostream>
using namespace std;
const int nmax = 2005;
const long long mod = 924844033;
long long dp[nmax][nmax];
long long modes[nmax], pond[nmax], fact[nmax];
long long ans;
int n, k, i, j, rr, cate;
int main() {
  // freopen("data.in","r",stdin);
  ios_base::sync_with_stdio(false);
  cin >> n >> k;
  dp[0][0] = 1;
  for (i = 1; i <= n; i++) {
    for (j = 0; j <= i; j++) {
      if (i >= 2 && j)
        dp[i][j] = dp[i - 2][j - 1];
      dp[i][j] += dp[i - 1][j];
      if (dp[i][j] >= mod)
        dp[i][j] -= mod;
    }
  }
  modes[0] = 1;
  for (rr = 0; rr < k; rr++) {
    cate = (n - 1) / k;
    if (rr <= (n - 1) % k)
      cate++;
    for (i = 1; i <= cate; i++)
      pond[i] = (dp[cate][i]) % mod;
    for (int dd = 0; dd < 2; dd++)
      for (i = n; i >= 0; i--)
        for (j = 1; j <= min(n - i, cate); j++)
          modes[i + j] = (modes[i + j] + 1LL * modes[i] * pond[j]) % mod;
  }
  fact[0] = 1;
  for (long long I = 1; I <= n; I++)
    fact[I] = (1LL * fact[I - 1] * I) % mod;
  for (i = 1; i <= n; i++) {
    if ((i & 1))
      ans += (1LL * modes[i] * fact[n - i]) % mod;
    else
      ans -= (1LL * modes[i] * fact[n - i]) % mod;
    if (ans < 0)
      ans += mod;
    if (ans >= mod)
      ans -= mod;
  }
  ans = (fact[n] - ans + mod) % mod;
  cout << ans;
  return 0;
}
Problem p03989 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int mod = 924844033;
ll f[5010][5010][2], fac[2010];
int nxt[5010], e[5010], pre[5010];
int read() {
  int x = 0;
  char c = getchar(), flag = '+';
  while (!isdigit(c))
    flag = c, c = getchar();
  while (isdigit(c))
    x = x * 10 + c - '0', c = getchar();
  return flag == '-' ? -x : x;
}
int main() {
  int n = read(), k = read(), cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (i + k <= n)
      nxt[i] = i + k + n, pre[i + k + n] = i;
    if (i - k >= 1)
      nxt[i - k + n] = i, pre[i] = i - k + n;
  }
  for (int i = 1; i <= 2 * n; i++) {
    if (!pre[i]) {
      int x = i;
      cnt++;
      while (nxt[x])
        x = nxt[x], e[++cnt] = 1;
    }
  }
  //	cout<<cnt;
  // for(int i=1;i<=cnt;i++) cout<<e[i]<<' ';
  f[0][0][0] = 1;
  for (int i = 1; i <= cnt; i++) {
    for (int j = 0; j <= n; j++) {
      f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1]) % mod;
      if (e[i] && j)
        f[i][j][1] = f[i - 1][j - 1][0];
    }
  }
  ll ans = 1;
  for (int i = 1; i <= n; i++)
    ans = ans * i % mod;
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = fac[i - 1] * i % mod;
  for (int i = 1; i <= n; i++) {
    if (i & 1)
      ans =
          (ans - (f[cnt][i][0] + f[cnt][i][1]) * fac[n - i] % mod + mod) % mod;
    else
      ans = (ans + (f[cnt][i][0] + f[cnt][i][1]) * fac[n - i]) % mod;
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int mod = 924844033;
ll f[4010][2010][2], fac[2010];
int nxt[5010], e[5010], pre[5010];
int read() {
  int x = 0;
  char c = getchar(), flag = '+';
  while (!isdigit(c))
    flag = c, c = getchar();
  while (isdigit(c))
    x = x * 10 + c - '0', c = getchar();
  return flag == '-' ? -x : x;
}
int main() {
  int n = read(), k = read(), cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (i + k <= n)
      nxt[i] = i + k + n, pre[i + k + n] = i;
    if (i - k >= 1)
      nxt[i - k + n] = i, pre[i] = i - k + n;
  }
  for (int i = 1; i <= 2 * n; i++) {
    if (!pre[i]) {
      int x = i;
      cnt++;
      while (nxt[x])
        x = nxt[x], e[++cnt] = 1;
    }
  }
  //	cout<<cnt;
  // for(int i=1;i<=cnt;i++) cout<<e[i]<<' ';
  f[0][0][0] = 1;
  for (int i = 1; i <= cnt; i++) {
    for (int j = 0; j <= n; j++) {
      f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1]) % mod;
      if (e[i] && j)
        f[i][j][1] = f[i - 1][j - 1][0];
    }
  }
  ll ans = 1;
  for (int i = 1; i <= n; i++)
    ans = ans * i % mod;
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = fac[i - 1] * i % mod;
  for (int i = 1; i <= n; i++) {
    if (i & 1)
      ans =
          (ans - (f[cnt][i][0] + f[cnt][i][1]) * fac[n - i] % mod + mod) % mod;
    else
      ans = (ans + (f[cnt][i][0] + f[cnt][i][1]) * fac[n - i]) % mod;
  }
  cout << ans;
  return 0;
}
Problem p03989 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define mod 924844033
#define N 5004
#define ll long long

using namespace std;

int n, k, cnt;
ll f[N][N][2], ans, fac[N], a[N];

void init();

int main() {
  cin >> n >> k;
  init();
  for (int i = 1; i <= k; ++i) {
    for (int j = i; j <= n; j += k)
      a[++cnt] = j;
    for (int j = i; j <= n; j += k)
      a[++cnt] = j;
  }
  f[1][0][0] = 1;
  for (int i = 2; i <= (n << 1); ++i)
    for (int j = 0; j <= min(n, i / 2); ++j) {
      f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1]) % mod;
      if (j > 0 && a[i] - a[i - 1] == k)
        f[i][j][1] = f[i - 1][j - 1][0];
    }
  for (int i = 0; i <= n; ++i) {
    ll sum = (f[n << 1][i][0] + f[n << 1][i][1]) % mod;
    (ans += (sum * fac[n - i] % mod * ((i & 1) ? -1 : 1) + mod) % mod) %= mod;
  }
  cout << ans << endl;
  return 0;
}

void init() {
  fac[0] = 1;
  for (int i = 1; i <= 2000; ++i)
    fac[i] = fac[i - 1] * i % mod;
  return;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define mod 924844033
#define N 4004
#define ll long long

using namespace std;

int n, k, cnt;
ll f[N][N][2], ans, fac[N], a[N];

void init();

int main() {
  cin >> n >> k;
  init();
  for (int i = 1; i <= k; ++i) {
    for (int j = i; j <= n; j += k)
      a[++cnt] = j;
    for (int j = i; j <= n; j += k)
      a[++cnt] = j;
  }
  f[1][0][0] = 1;
  for (int i = 2; i <= (n << 1); ++i)
    for (int j = 0; j <= min(n, i / 2); ++j) {
      f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1]) % mod;
      if (j > 0 && a[i] - a[i - 1] == k)
        f[i][j][1] = f[i - 1][j - 1][0];
    }
  for (int i = 0; i <= n; ++i) {
    ll sum = (f[n << 1][i][0] + f[n << 1][i][1]) % mod;
    (ans += (sum * fac[n - i] % mod * ((i & 1) ? -1 : 1) + mod) % mod) %= mod;
  }
  cout << ans << endl;
  return 0;
}

void init() {
  fac[0] = 1;
  for (int i = 1; i <= 2000; ++i)
    fac[i] = fac[i - 1] * i % mod;
  return;
}
Problem p03989 (C++)

Buggy submission (status: Runtime Error):
// In the name of God
#include <bits/stdc++.h>
#define int long long

using namespace std;

const int N = 2010;
const int MOD = 924844033;

int n, k, f[N][N], dp[N][2 * N], a[N], cnt = 1, sz, fact[N];
vector<int> g[2 * N];
bool mark[2 * N];

void dfs(int v) {
  if (mark[v])
    return;
  mark[v] = 1;
  sz++;
  for (auto u : g[v])
    dfs(u);
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    if (i - k >= 1) {
      g[i].push_back(n + i - k);
      g[n + i - k].push_back(i);
    }
    if (i + k <= n) {
      g[i].push_back(n + i + k);
      g[n + i + k].push_back(i);
    }
  }
  for (int i = 1; i <= 2 * n; i++)
    if (!mark[i]) {
      sz = 0;
      dfs(i);
      a[cnt++] = sz - 1;
    }
  for (int i = 0; i < N; i++) {
    f[i][0] = 1;
    f[i][1] = i;
    for (int j = 2; j <= i; j++)
      f[i][j] = (1LL * f[i - 1][j] + 1LL * f[i - 2][j - 1]) % MOD;
  }
  int sum = 0;
  dp[0][0] = 1;
  for (int i = 1; i < cnt; i++) {
    for (int j = 0; j <= sum; j++)
      for (int x = 0; x <= a[i]; x++) {
        int now = 1LL * dp[i - 1][j] * f[a[i]][x] % MOD;
        dp[i][j + x] = (dp[i][j + x] + now) % MOD;
      }
    sum += a[i];
  }
  fact[0] = 1;
  for (int i = 1; i <= n; i++)
    fact[i] = 1LL * i * fact[i - 1] % MOD;
  int res = 0;
  for (int i = 0; i <= n; i++) {
    int now = 1LL * dp[cnt - 1][i] * fact[n - i] % MOD;
    if (i % 2)
      now = -now;
    res = (res + now + MOD) % MOD;
  }
  cout << res;
  return 0;
}

Fixed submission:
// In the name of God
#include <bits/stdc++.h>
#define int long long

using namespace std;

const int N = 2010;
const int MOD = 924844033;

int n, k, f[N][N], dp[N * 3][N * 3], a[N * 3], cnt = 1, sz, fact[N];
vector<int> g[N * 2];
bool mark[N * 2];

void dfs(int v) {
  if (mark[v])
    return;
  mark[v] = 1;
  sz++;
  for (auto u : g[v])
    dfs(u);
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    if (i - k >= 1) {
      g[i].push_back(n + i - k);
      g[n + i - k].push_back(i);
    }
    if (i + k <= n) {
      g[i].push_back(n + i + k);
      g[n + i + k].push_back(i);
    }
  }
  for (int i = 1; i <= 2 * n; i++)
    if (!mark[i]) {
      sz = 0;
      dfs(i);
      a[cnt++] = sz - 1;
    }
  for (int i = 0; i < N; i++) {
    f[i][0] = 1;
    f[i][1] = i;
    for (int j = 2; j <= i; j++)
      f[i][j] = (1LL * f[i - 1][j] + 1LL * f[i - 2][j - 1]) % MOD;
  }
  int sum = 0;
  dp[0][0] = 1;
  for (int i = 1; i < cnt; i++) {
    for (int j = 0; j <= sum; j++)
      for (int x = 0; x <= a[i]; x++) {
        int now = 1LL * dp[i - 1][j] * f[a[i]][x] % MOD;
        dp[i][j + x] = (dp[i][j + x] + now) % MOD;
      }
    sum += a[i];
  }
  fact[0] = 1;
  for (int i = 1; i <= n; i++)
    fact[i] = 1LL * i * fact[i - 1] % MOD;
  int res = 0;
  for (int i = 0; i <= n; i++) {
    int now = 1LL * dp[cnt - 1][i] * fact[n - i] % MOD;
    if (i % 2)
      now = -now;
    res = (res + now + MOD) % MOD;
  }
  cout << res;
  return 0;
}
Problem p03989 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define MOD 924844033
#define mkp make_pair
typedef long long ll;
using namespace std;

int N, K;

vector<int> line;
ll dp[2020][2020][2];

void add(ll &a, ll b) {
  a = (a + b) % MOD;
  return;
}

int main() {
  cin >> N >> K;

  for (int i = 0; i < 2 * K; i++) {
    if (i >= N)
      break;

    bool fir = true;
    if (i >= K) {
      line.push_back(1);
      fir = false;
    }

    for (int j = i; j + K < N; j += K) {
      if (j == i && fir)
        line.push_back(1);
      else
        line.push_back(0);
    }
  }

  dp[0][0][0] = 1;
  for (int i = 0; i < line.size(); i++) {
    for (int j = 0; j <= i; j++) {
      if (line[i] == 1) {
        add(dp[i + 1][j + 1][1], dp[i][j][0] + dp[i][j][1]);
      } else {
        add(dp[i + 1][j + 1][1], dp[i][j][0]);
      }

      add(dp[i + 1][j][0], dp[i][j][0] + dp[i][j][1]);
    }
  }

  vector<ll> fact(N + 1, 0);
  fact[0] = 1;
  for (int i = 1; i <= N; i++)
    fact[i] = fact[i - 1] * i % MOD;

  ll ans = 0;
  for (int i = 1; i <= N; i++) {
    ll res = dp[line.size()][i][0] + dp[line.size()][i][1];
    res %= MOD;
    res = res * fact[N - i] % MOD;

    if (i % 2 == 0)
      res = -res;
    add(ans, res);
  }

  ans = (fact[N] - ans + MOD) % MOD;
  add(ans, MOD);

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define MOD 924844033
#define mkp make_pair
typedef long long ll;
using namespace std;

int N, K;

vector<int> line;
ll dp[4040][4040][2];

void add(ll &a, ll b) {
  a = (a + b) % MOD;
  return;
}

int main() {
  cin >> N >> K;

  for (int i = 0; i < 2 * K; i++) {
    if (i >= N)
      break;

    bool fir = true;
    if (i >= K) {
      line.push_back(1);
      fir = false;
    }

    for (int j = i; j + K < N; j += K) {
      if (j == i && fir)
        line.push_back(1);
      else
        line.push_back(0);
    }
  }

  dp[0][0][0] = 1;
  for (int i = 0; i < line.size(); i++) {
    for (int j = 0; j <= i; j++) {
      if (line[i] == 1) {
        add(dp[i + 1][j + 1][1], dp[i][j][0] + dp[i][j][1]);
      } else {
        add(dp[i + 1][j + 1][1], dp[i][j][0]);
      }

      add(dp[i + 1][j][0], dp[i][j][0] + dp[i][j][1]);
    }
  }

  vector<ll> fact(N + 1, 0);
  fact[0] = 1;
  for (int i = 1; i <= N; i++)
    fact[i] = fact[i - 1] * i % MOD;

  ll ans = 0;
  for (int i = 1; i <= N; i++) {
    ll res = dp[line.size()][i][0] + dp[line.size()][i][1];
    res %= MOD;
    res = res * fact[N - i] % MOD;

    if (i % 2 == 0)
      res = -res;
    add(ans, res);
  }

  ans = (fact[N] - ans + MOD) % MOD;
  add(ans, MOD);

  cout << ans << endl;

  return 0;
}
Problem p03989 (C++)

Buggy submission (status: Runtime Error):
// ===================================
//   author: M_sea
//   website: http://m-sea-blog.com/
// ===================================
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#define re register
using namespace std;

inline int read() {
  int X = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    X = X * 10 + c - '0', c = getchar();
  return X * w;
}

const int N = 2000 + 10;
const int mod = 924844033;

int n, k;
int a[N], cnt = 0;
int fac[N], dp[N << 1][N][2];

int main() {
  n = read(), k = read();
  for (re int i = 1; i <= k; ++i) {
    for (re int j = i; j <= n; j += k)
      a[++cnt] = j == i;
    for (re int j = i; j <= n; j += k)
      a[++cnt] = j == i;
  }
  dp[0][0][0] = 1;
  for (re int i = 0; i < cnt; ++i)
    for (re int j = 0; j <= n; ++j) {
      dp[i + 1][j][0] = (dp[i][j][0] + dp[i][j][1]) % mod;
      if (!a[i + 1])
        dp[i + 1][j + 1][1] = dp[i][j][0];
    }
  fac[0] = 1;
  for (re int i = 1; i <= n; ++i)
    fac[i] = 1ll * fac[i - 1] * i % mod;
  int ans = 0;
  for (re int i = 0; i <= n; ++i) {
    int w = 1ll * (dp[cnt][i][0] + dp[cnt][i][1]) * fac[n - i] % mod;
    if (i & 1)
      ans = (ans - w + mod) % mod;
    else
      ans = (ans + w) % mod;
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
// ===================================
//   author: M_sea
//   website: http://m-sea-blog.com/
// ===================================
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#define re register
using namespace std;

inline int read() {
  int X = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    X = X * 10 + c - '0', c = getchar();
  return X * w;
}

const int N = 2000 + 10;
const int mod = 924844033;

int n, k;
int a[N << 1], cnt = 0;
int fac[N], dp[N << 1][N][2];

int main() {
  n = read(), k = read();
  for (re int i = 1; i <= k; ++i) {
    for (re int j = i; j <= n; j += k)
      a[++cnt] = j == i;
    for (re int j = i; j <= n; j += k)
      a[++cnt] = j == i;
  }
  dp[0][0][0] = 1;
  for (re int i = 0; i < cnt; ++i)
    for (re int j = 0; j <= n; ++j) {
      dp[i + 1][j][0] = (dp[i][j][0] + dp[i][j][1]) % mod;
      if (!a[i + 1])
        dp[i + 1][j + 1][1] = dp[i][j][0];
    }
  fac[0] = 1;
  for (re int i = 1; i <= n; ++i)
    fac[i] = 1ll * fac[i - 1] * i % mod;
  int ans = 0;
  for (re int i = 0; i <= n; ++i) {
    int w = 1ll * (dp[cnt][i][0] + dp[cnt][i][1]) * fac[n - i] % mod;
    if (i & 1)
      ans = (ans - w + mod) % mod;
    else
      ans = (ans + w) % mod;
  }
  printf("%d\n", ans);
  return 0;
}