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

#include <algorithm>
#include <iostream>
#include <vector>
#define INF 1000000000;
using namespace std;
int n, m, a[11];
vector<int> coin;
int used[11];
void mk_coin(int sum, int k) {
  for (int i = k; i < n; i++) {
    if (used[i] == 1)
      continue;
    used[i] = 1;
    coin.push_back(sum + a[i]);
    mk_coin(sum + a[i], i + 1);
    used[i] = 0;
  }
}

int main() {
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    coin.clear();
    for (int i = 0; i < n; i++)
      cin >> a[i];
    mk_coin(0, 0);

    int dp[100010] = {};
    for (int i = 1; i <= m; i++)
      dp[i] = INF;
    for (int i = 0; i < coin.size(); i++)
      for (int j = 0; j <= m - coin[i]; j++)
        dp[j + coin[i]] = min(dp[j] + 1, dp[j + coin[i]]);
    cout << dp[m] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define INF 1000000000;
using namespace std;
int n, m, a[11];
vector<int> coin;
int used[11];
void mk_coin(int sum, int k) {
  for (int i = k; i < n; i++) {
    if (used[i] == 1)
      continue;
    used[i] = 1;
    coin.push_back(sum + a[i]);
    mk_coin(sum + a[i], i + 1);
    used[i] = 0;
  }
}

int main() {
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    coin.clear();
    for (int i = 0; i < n; i++)
      cin >> a[i];
    mk_coin(0, 0);

    int dp[100010] = {};
    for (int i = 1; i <= m; i++)
      dp[i] = INF;
    for (int i = 0; i < coin.size(); i++)
      for (int j = 0; j <= m; j++)
        dp[j + coin[i]] = min(dp[j] + 1, dp[j + coin[i]]);
    cout << dp[m] << endl;
  }
  return 0;
}
Problem p01257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
#define INF 1000000000;
using namespace std;
int n, m, a[11];
vector<int> coin;
int used[11];
void saiki(int sum, int k) {
  for (int i = k; i < n; i++) {
    if (used[i] == 1)
      continue;
    used[i] = 1;
    coin.push_back(sum + a[i]);
    saiki(sum + a[i], i + 1);
    used[i] = 0;
  }
}

int main() {
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    coin.clear();
    for (int i = 0; i < n; i++)
      cin >> a[i];
    saiki(0, 0);

    int dp[100010] = {};
    for (int i = 1; i <= m; i++)
      dp[i] = INF;
    for (int i = 0; i < coin.size(); i++)
      for (int j = 0; j <= m; j++)
        for (int k = 1; j + coin[i] * k <= m && dp[j + coin[i] * k] > dp[j] + k;
             k++)
          dp[j + coin[i] * k] = dp[j] + k;
    cout << dp[m] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define INF 1000000000;
using namespace std;

int main() {
  while (1) {
    int n, m, a[11];
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    vector<int> coin;
    for (int i = 0; i < n; i++)
      for (int j = i, sum = 0; j < n; j++)
        sum += a[j], coin.push_back(sum);

    int dp[100010] = {};
    for (int i = 1; i <= m; i++)
      dp[i] = INF;

    for (int i = 0; i < coin.size(); i++)
      for (int j = coin[i]; j <= m; j++)
        for (int k = 0; k <= j / coin[i]; k++)
          dp[j] = min(dp[j], dp[j - coin[i] * k] + k);

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

#include <algorithm>
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int n, m, cost[100001], op[1023];
void dfs(int sum) {
  rep(i,
      (1 << n) - 1) if (sum + op[i] <= m && cost[sum + op[i]] > cost[sum] + 1)
      cost[sum + op[i]] = cost[sum] + 1,
                 dfs(sum + op[i]);
}
int main() {
  while (cin >> n >> m, n) {
    int coin[10], s = 0;
    rep(i, n) cin >> coin[i];
    rep(i, 1 << n) if (i > 0) {
      s = 0;
      rep(j, n) if (i & 1 << j) s += coin[j];
      op[i - 1] = s;
    }
    sort(op, op + (1 << n) - 1, greater<int>());
    rep(i, m + 1) cost[i] = 1 << 20;
    cost[0] = 0;
    dfs(0);
    cout << cost[m] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int n, m, cost[100001], op[1023];
void dfs(int sum) {
  rep(i, (1 << n) - 1) if (cost[sum + op[i]] > cost[sum] + 1)
      cost[sum + op[i]] = cost[sum] + 1,
                 dfs(sum + op[i]);
}
int main() {
  while (cin >> n >> m, n) {
    int coin[10], s = 0;
    rep(i, n) cin >> coin[i];
    rep(i, 1 << n) if (i > 0) {
      s = 0;
      rep(j, n) if (i & 1 << j) s += coin[j];
      op[i - 1] = s;
    }
    sort(op, op + (1 << n) - 1, greater<int>());
    rep(i, m + 1) cost[i] = 1 << 20;
    cost[0] = 0;
    dfs(0);
    cout << cost[m] << endl;
  }
  return 0;
}
Problem p01257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int n, m, a[10];
int dp[100001];

int main() {
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> a[i];

    int L = 1, R = m, M;
    while (L < R) {
      M = (L + R) / 2;
      memset(dp, -1, sizeof(dp));
      dp[0] = 0;
      for (int i = 0; i < n; i++) {
        for (int j = 1; j <= m; j++) {
          if (dp[j] == -1) {
            if (j - a[i] >= 0 && dp[j - a[i]] != -1 && dp[j - a[i]] < M)
              dp[j] = dp[j - a[i]] + 1;
          } else {
            dp[j] = 0;
          }
        }
      }
      if (dp[m] == -1)
        L = M + 1;
      else
        R = M;
    }
    cout << L << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, m, a;
int dp[11][100001];

int main() {
  while (1) {
    memset(dp, 31, sizeof(dp));

    dp[0][0] = 0;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 1; i <= n; i++) {
      cin >> a;
      for (int j = 0; j <= m; j++) {
        for (int k = 0; j - k * a >= 0; k++) {
          dp[i][j] = min(dp[i][j], max(k, dp[i - 1][j - k * a]));
          if (dp[i][j] < k)
            break;
        }
      }
    }
    /*
    for(int i=1;i<=n;i++){
      for(int j=0;j<=10;j++){
        cout<<dp[i][j]<<' ';
      }
      cout<<endl;
    }
    */
    cout << dp[n][m] << endl;
  }
  return 0;
}
Problem p01257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int a[15], n, w;
bool dp[100020];
bool knapsack(vector<int> x) {
  for (int i = 1; i < 100020; i++)
    dp[i] = false;
  dp[0] = true;
  for (int i = 0; i < (int)x.size(); i++) {
    for (int j = w - x[i]; j >= 0; j--) {
      if (dp[j] == true)
        dp[j + x[i]] = true;
    }
  }
  return dp[w];
}
bool solve(int r) {
  vector<int> J;
  for (int i = 0; i < n; i++) {
    int R = r, t = 0;
    while (R >= (1 << t)) {
      R -= (1 << t);
      t++;
      J.push_back(a[i] * (1 << (t - 1)));
    }
    if (R >= 1)
      J.push_back(a[i] * R);
  }
  return knapsack(J);
}
int main() {
  while (true) {
    cin >> n >> w;
    if (n == 0 && w == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    int L = 1, R = w + 10, M;
    while (true) {
      M = (L + R) / 2;
      bool p1 = solve(M), p2 = solve(M - 1);
      if (p1 == true && p2 == false) {
        cout << M << endl;
        break;
      }
      if (p2 == true)
        R = M;
      if (p1 == false)
        L = M;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int a[15], n, w;
bool dp[100020];
bool knapsack(vector<int> x) {
  for (int i = 1; i < 100020; i++)
    dp[i] = false;
  dp[0] = true;
  for (int i = 0; i < (int)x.size(); i++) {
    for (int j = w - x[i]; j >= 0; j--) {
      if (dp[j] == true)
        dp[j + x[i]] = true;
    }
  }
  return dp[w];
}
bool solve(int r) {
  if (r == 1) {
    r += 0;
  }
  vector<int> J;
  for (int i = 0; i < n; i++) {
    int R = r, t = 0;
    while (R >= (1 << t)) {
      R -= (1 << t);
      t++;
      J.push_back(a[i] * (1 << (t - 1)));
    }
    if (R >= 1)
      J.push_back(a[i] * R);
  }
  return knapsack(J);
}
int main() {
  while (true) {
    cin >> n >> w;
    if (n == 0 && w == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    int L = 1, R = w, M;
    while (true) {
      M = (L + R) / 2;
      bool p1 = solve(M), p2 = solve(M - 1);
      if (p1 == true && p2 == false) {
        cout << M << endl;
        break;
      }
      if (p2 == true)
        R = M;
      if (p1 == false)
        L = M;
    }
  }
  return 0;
}
Problem p01261 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>

using namespace std;

using ll = long long;
constexpr int MAX = 100;

ll com[MAX][MAX];

void make_com() {
  memset(com, 0, sizeof(com));
  com[0][0] = 1;
  for (int i = 0; i < 65; i++) {
    for (int j = 0; j <= i; j++) {
      com[i + 1][j] += com[i][j];
      com[i + 1][j + 1] += com[i][j];
    }
  }
}

void solve(ll N, ll s, ll M, ll n) {
  while (s < M) {
    ll j = -1;
    for (ll i = n; i <= N; i++) {
      if (s + com[i][n] >= M) {
        s += com[i - 1][n];
        j = i;
        break;
      }
    }

    for (int i = 0; i < N - j; i++) {
      cout << '0';
    }

    if (n > 0) {
      cout << '1';
    }

    n--;
    N = j - 1;
  }

  cout << endl;
}

bool run() {
  ll N, M;
  cin >> N >> M;
  if (N == 0 && M == 0) {
    return false;
  }

  if (M == 1) {
    cout << std::string(N, '0') << endl;
    return true;
  }

  make_com();

  ll sum = 1;
  for (ll i = 1; i <= N; i++) {
    sum += com[N][i];
    if (sum >= M) {
      solve(N, sum - com[N][i], M, i);
      break;
    }
  }
  return true;
}

int main() {
  while (run())
    ;
  return 0;
}

Error: WA

Fixed submission:

#include <cstring>
#include <iostream>

using namespace std;

using ll = long long;
constexpr int MAX = 65;

ll com[MAX + 1][MAX + 1];

void make_com() {
  memset(com, 0, sizeof(com));
  com[0][0] = 1;
  for (int i = 0; i < MAX; i++) {
    for (int j = 0; j <= i; j++) {
      com[i + 1][j] += com[i][j];
      com[i + 1][j + 1] += com[i][j];
    }
  }
}

void solve(ll N, ll s, ll M, ll n) {
  while (s < M) {
    ll j = -1;
    for (ll i = n; i <= N; i++) {
      if (s + com[i][n] >= M) {
        s += com[i - 1][n];
        j = i;
        break;
      }
    }

    for (int i = 0; i < N - j; i++) {
      cout << '0';
    }

    if (n > 0) {
      cout << '1';
    }

    n--;
    N = j - 1;
  }

  cout << endl;
}

bool run() {
  ll N, M;
  cin >> N >> M;
  if (N == 0 && M == 0) {
    return false;
  }

  if (M == 1) {
    cout << std::string(N, '0') << endl;
    return true;
  }

  make_com();

  ll sum = 1;
  for (ll i = 1; i <= N; i++) {
    sum += com[N][i];
    if (sum >= M) {
      solve(N, sum - com[N][i], M, i);
      break;
    }
  }
  return true;
}

int main() {
  while (run())
    ;
  return 0;
}
Problem p01261 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

long long comb[61][61];

int main() {
  for (int i = 0; i <= 60; i++) {
    for (int j = 0; j <= 60; j++) {
      comb[i][j] = (j > i)              ? 0
                   : (j == i || j == 0) ? 1
                                        : comb[i - 1][j - 1] + comb[i - 1][j];
    }
  }
  for (long long N, M; cin >> N >> M, N;) {
    long long a[61] = {1};
    for (int i = 1; i <= N; i++) {
      a[i] = a[i - 1] + comb[N][i];
    }
    int nb = lower_bound(a, a + N + 1, M) - a;
    auto cm = nb ? a[nb - 1] : 0;
    for (int i = 0; i < N; i++) {
      if (nb && cm + comb[N - i - 1][nb] < M) {
        cout << 1;
        cm += comb[N - i - 1][nb];
        nb--;
      } else {
        cout << 0;
      }
    }
    cout << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

long long comb[61][61];

int main() {
  for (int i = 0; i <= 60; i++) {
    for (int j = 0; j <= 60; j++) {
      comb[i][j] = (j > i)              ? 0
                   : (j == i || j == 0) ? 1
                                        : comb[i - 1][j - 1] + comb[i - 1][j];
    }
  }
  for (int N, M; cin >> N >> M, N;) {
    long long a[61] = {1};
    for (int i = 1; i < N; i++) {
      a[i] = a[i - 1] + comb[N][i + 1];
    }
    int nb = lower_bound(a, a + N + 1, M) - a;
    auto cm = nb ? a[nb - 1] : 0;
    for (int i = 0; i < N; i++) {
      if (nb && cm + comb[N - i - 1][nb] < M) {
        cout << 1;
        cm += comb[N - i - 1][nb];
        nb--;
      } else {
        cout << 0;
      }
    }
    cout << endl;
  }
}
Problem p01261 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

using namespace std;
typedef long long ll;

ll pascal[61][61] = {{0ll}};

int main() {
  for (int i = 0; i <= 60; ++i) {
    pascal[i][0] = 1;
    for (int j = 1; j <= i; ++j) {
      pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j];
    }
  }
  while (true) {
    int n, x;
    ll m;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    --m;
    if (m == 0) {
      cout << string(n, '0') << endl;
      continue;
    }
    for (x = 0; x < n; ++x) {
      if (m >= pascal[n][x]) {
        m -= pascal[n][x];
      } else {
        break;
      }
    }
    string answer(n, '0');
    for (int y = x - 1, z = n; y > 0; --y) {
      for (int w = y; w < z; ++w) {
        if (m >= pascal[w][y]) {
          m -= pascal[w][y];
        } else {
          answer[w] = '1';
          z = w;
          break;
        }
      }
    }
    answer[m] = '1';
    reverse(answer.begin(), answer.end());
    cout << answer << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;
typedef long long ll;

ll pascal[61][61] = {{0ll}};

int main() {
  for (int i = 0; i <= 60; ++i) {
    pascal[i][0] = 1;
    for (int j = 1; j <= i; ++j) {
      pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j];
    }
  }
  while (true) {
    int n, x;
    ll m;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    --m;
    if (m == 0) {
      cout << string(n, '0') << endl;
      continue;
    }
    for (x = 0; x < n; ++x) {
      if (m >= pascal[n][x]) {
        m -= pascal[n][x];
      } else {
        break;
      }
    }
    string answer(n, '0');
    for (int y = x - 1, z = n; y > 0; --y) {
      for (int w = y; w < z; ++w) {
        if (m >= pascal[w - 1][y - 1]) {
          m -= pascal[w - 1][y - 1];
        } else {
          answer[w] = '1';
          z = w;
          break;
        }
      }
    }
    answer[m] = '1';
    reverse(answer.begin(), answer.end());
    cout << answer << endl;
  }
  return 0;
}
Problem p01267 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main(void) {
  for (;;) {
    int N, A, B, C;
    long long int X;
    cin >> N >> A >> B >> C >> X;
    if (N == 0) {
      return 0;
    }
    int Y[N];
    for (int i = 0; i < N; i++) {
      cin >> Y[i];
    }
    int i = 0;
    bool finish = false;
    int ans = -1;
    for (int step = 0; step <= 10000 && finish == false; step++) {
      if (X == Y[i]) {
        if (i == N - 1) {
          finish = true;
          ans = step;
        } else {
          i++;
        }
      }
      X = (A * X + B) % C;
    }
    cout << ((finish == true) ? (ans) : (-1)) << endl;
  }
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main(void) {
  for (;;) {
    int N, A, B, C;
    long long int X;
    cin >> N >> A >> B >> C >> X;
    if (N == 0) {
      return 0;
    }
    int Y[N];
    for (int i = 0; i < N; i++) {
      cin >> Y[i];
    }
    int i = 0;
    int step = 0;
    bool finish = false;
    while (step < 10000 || finish) {
      if (X == Y[i]) {
        if (i == N - 1) {
          finish = true;
        } else {
          i++;
        }
      }
      X = (A * C + B) % C;
      step++;
    }
    cout << ((finish == true) ? (step) : (-1)) << endl;
  }
}
Problem p01267 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

int main() {
  int Y[10000];
  for (int N, A, B, C, X; cin >> N >> A >> B >> C >> X && N;) {
    for (int i = 0; i < N; i++) {
      cin >> Y[i];
    }
    int cnt, i = 0;
    for (cnt = 0; cnt <= 10000; cnt++) {
      if (Y[i] == X) {
        i++;
        if (i == N)
          break;
      }
      X = (A * X + B) % C;
    }
    cout << (cnt <= 10000 ? cnt : -1) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

int main() {
  for (int N, A, B, C, X; scanf("%d%d%d%d%d", &N, &A, &B, &C, &X) && N;) {
    int cnt = -1;
    for (int i = 0; i < N; i++) {
      int Y;
      scanf("%d", &Y);
      while (true) {
        if (Y == X) {
          X = (A * X + B) % C;
          cnt++;
          break;
        }
        X = (A * X + B) % C;
        cnt++;
        if (cnt > 10000) {
          cnt = -1;
          break;
        }
      }
      if (cnt > 10000) {
        cnt = -1;
        break;
      }
    }
    printf("%d\\n", cnt);
  }
  return 0;
}
Problem p01267 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, a, b, c, x;
  while (cin >> n >> a >> b >> c >> x, n + a + b + c + x) {
    int count = 0;
    vector<int> ans(n);
    for (int i = 0; i < n; i++)
      cin >> ans[i];
    for (int count = 0;; count++) {
      if (10000 < count) {
        cout << -1 << endl;
        break;
      }
      if (x == ans[0]) {
        vector<int> t;
        for (int i = 1; i < ans.size(); i++)
          t.push_back(ans[i]);
        ans = t;
      }
      if (ans.size() == 0) {
        cout << count << endl;
        break;
      }
      x = (a * x + b) % c;
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, a, b, c, x;
  while (cin >> n >> a >> b >> c >> x, n + a + b + c + x) {
    int count = 0;
    vector<int> r;
    vector<int> ans(n);
    for (int i = 0; i < n; i++)
      cin >> ans[i];
    for (int count = 0;; count++) {
      if (10000 < count) {
        cout << -1 << endl;
        break;
      }
      r.push_back(x);
      if (n <= r.size()) {
        bool is = false;
        for (int j = 0; j < ans.size();) {
          for (int i = 0; i < r.size(); i++) {
            if (r[i] == ans[j])
              j++;
            if (j == ans.size()) {
              is = true;
              j = 1000000;
              break;
            }
            if (i == r.size() - 1) {
              j = 1000000;
            }
          }
        }
        if (is) {
          cout << count << endl;
          break;
        }
      }
      x = (a * x + b) % c;
    }
  }
}
Problem p01267 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
using namespace std;

int n, a, b, c, x, y[110];

int main() {
  while (cin >> n >> a >> b >> c >> x, n) {
    rep(i, n) cin >> y[i];
    int cnt = 0, index = 0;
    while (cnt < 10001) {
      if (x == y[index])
        index++;
      if (index == n) {
        cout << cnt << endl;
        goto end;
      }
      x = (a * x + b) % c;
      cnt++;
    }
    cout << -1 << endl;
  end:;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
using namespace std;
const int INF = sizeof(int) == sizeof(long long) ? 1e18 : 1e9;
const int MOD = 1000000007;
const long double EPS = 1e-8;

int n, a, b, c, x, y;

int main() {
  while (cin >> n >> a >> b >> c >> x, n) {
    vector<int> t;
    t.push_back(x);
    while (t.front() != (x = (a * x + b) % c)) {
      t.push_back(x);
    }
    int prevTimestamp = 0, ans = 0;
    bool flag = false;
    rep(i, n) {
      cin >> y;
      auto itr = find(t.begin(), t.end(), y);
      flag |= (itr == t.end());
      int dif = itr - t.begin();
      ans += (prevTimestamp < dif ? dif - prevTimestamp
                                  : t.size() - prevTimestamp + dif);
      if (i == 0 && prevTimestamp == dif)
        ans = 0;
      prevTimestamp = dif;
    }
    cout << (flag || ans > 10000 ? -1 : ans) << endl;
  }
  return 0;
}
Problem p01267 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define lp(i, n) for (int i = 0; i < n; i++)
#define lpin(a, i, n)                                                          \\
  for (int i = 0; i < n; i++) {                                                \\
    cin >> a[i];                                                               \\
  }

int main() {
  while (1) {
    int n, a, b, c, x;
    cin >> n >> a >> b >> c >> x;
    if (n == 0 && a == 0 && b == 0 && c == 0 && x == 0)
      break;
    int ans = 0;
    int j, i;
    for (i = 0; i < n; i++) {
      int rand;
      cin >> rand;
      for (j = 0; j < 10001; j++) {
        if (x == rand) {
          rand = -1;
          break;
        }
        x = (a * x + b) % c;
        ans++;
        if (ans > 10000) {
          ans = -1;
          break;
        }
      }
      if (ans == -1) {
        ans = -1;
        break;
      }
      if (rand != -1) {
        ans = -1;
        break;
      }
      x = (a * x + b) % c;
      ans++;
    }
    if (i == n - 1)
      1;
    else
      lp(k, n - 1 - i) {
        int rand;
        cin >> rand;
      }
    if (ans == -1)
      cout << "-1" << endl;
    else
      cout << ans - 1 << endl;
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define lp(i, n) for (int i = 0; i < n; i++)
#define lpin(a, i, n)                                                          \\
  for (int i = 0; i < n; i++) {                                                \\
    cin >> a[i];                                                               \\
  }

int main() {
  while (1) {
    int n, a, b, c, x;
    cin >> n >> a >> b >> c >> x;
    if (n == 0 && a == 0 && b == 0 && c == 0 && x == 0)
      break;
    int ans = 0;
    lp(i, n) {
      int rand;
      cin >> rand;
      lp(j, 10001) {
        if (x == rand) {
          rand = -1;
          break;
        }
        x = (a * x + b) % c;
        ans++;
        if (ans > 10000) {
          ans = -1;
          break;
        }
      }
      if (ans == -1) {
        ans = -1;
        break;
      }
      if (rand != -1) {
        ans = -1;
        break;
      }
      x = (a * x + b) % c;
      ans++;
    }
    if (ans == -1)
      cout << "-1" << endl;
    else
      cout << ans - 1 << endl;
  }
  return 0;
}