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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < (n); i++)
#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#define ll long long
using namespace std;
typedef pair<int, int> P;

const double INF = 100000.0;

double dp[30][1 << 9];
int n, m, p, a, b;
int z[30][30];
int t[8];

int main() {
  int x, y, c;
  while (1) {
    cin >> n >> m >> p >> a >> b;
    if (n + m + p + a + b == 0)
      break;
    a--;
    b--;
    REP(i, n) cin >> t[i];
    REP(i, m) REP(j, m) z[i][j] = -1;
    REP(i, p) {
      cin >> x >> y >> c;
      x--;
      y--;
      z[x][y] = z[y][x] = c;
    }
    REP(i, m) REP(j, 1 << 9) dp[i][j] = INF;
    dp[a][0] = 0;
    queue<P> q;
    q.push(MP(a, 0));
    while (q.size()) {
      P p = q.front();
      q.pop();
      int now = p.first, bit = p.second;
      // cout<<now<<" "<<bit<<endl;
      REP(i, m) {
        if (z[now][i] == -1)
          continue;
        REP(j, n) {
          if (bit >> j & 1)
            continue;
          if (dp[i][bit | 1 << j] >
              dp[now][bit] + (double)z[now][i] / (double)t[j]) {
            dp[i][bit | 1 << j] =
                dp[now][bit] + (double)z[now][i] / (double)t[j];
            q.push(MP(i, bit | 1 << j));
          }
        }
      }
    }
    double ans = INF;
    REP(i, 1 << 9) ans = min(ans, dp[b][i]);
    if (ans == INF)
      cout << "Impossible" << endl;
    else
      cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < (n); i++)
#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#define ll long long
using namespace std;
typedef pair<int, int> P;

const double INF = 10000.0;

double dp[30][1 << 9];
int n, m, p, a, b;
int z[30][30];
int t[8];

double rec(int cur, int bit, ll state) {
  if (dp[cur][bit] < 10000.0)
    return dp[cur][bit];
  if (cur == b)
    return dp[cur][bit] = 0;
  double res = INF;
  REP(i, m) {
    if (z[cur][i] == -1)
      continue;
    if (state >> i & 1)
      continue;
    REP(j, n) {
      if (bit >> j & 1)
        continue;
      res = min(res, rec(i, bit | 1 << j, state | 1 << cur) +
                         (double)z[cur][i] / (double)t[j]);
    }
  }
  return dp[cur][bit] = res;
}

int main() {
  while (1) {
    cin >> n >> m >> p >> a >> b;
    if (n + m + p + a + b == 0)
      break;
    a--;
    b--;
    REP(i, n) cin >> t[i];
    REP(i, m) REP(j, m) z[i][j] = -1;
    REP(i, m) REP(j, 1 << 9) dp[i][j] = INF;
    REP(i, p) {
      int x, y, c;
      cin >> x >> y >> c;
      x--;
      y--;
      z[y][x] = z[x][y] = c;
    }
    double ans = rec(a, 0, 0);
    if (ans == INF) {
      cout << "Impossible" << endl;
    } else {
      cout << ans << endl;
    }
  }
}
Problem p00719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>
using namespace std;
typedef pair<int, int> P;
int main() {
  int n, m, p, a, b, x, y, z, i, j, k, l;
  double dp[30][1 << 8], t[8];
  vector<P> e[30];
  while (scanf("%d%d%d%d%d", &n, &m, &p, &a, &b), n) {
    --a, --b;
    for (i = 0; i < 30; ++i)
      e[i].clear();
    for (i = 0; i < n; ++i)
      scanf("%lf", t + i);
    while (p--) {
      scanf("%d%d%d", &x, &y, &z);
      --x, --y;
      e[x].push_back(P(y, z));
      e[y].push_back(P(x, z));
    }
    fill(dp[0], dp[30], 1e9);
    dp[a][(1 << n) - 1] = 0;
    for (i = (1 << n) - 1; i > 0; --i) {
      for (j = 0; j < m; ++j) {
        if (dp[j][i] == 1e9)
          continue;
        for (k = 0; k < e[j].size(); ++k) {
          for (l = 0; l < n; ++l) {
            if (!(i & (1 << l)))
              continue;
            double &r = dp[e[j][k].first][i ^ (1 << l)];
            r = min(r, dp[j][i] + e[j][k].second / t[l]);
          }
        }
      }
    }
    double r = *min_element(dp[b], dp[b + 1]);
    if (r == 1e9)
      puts("Impossible");
    else
      printf("%f\\n", r);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <numeric>
#include <vector>
using namespace std;
typedef vector<double> V;
int n, m, p, a, b, _ = 1e9, d[31][31], f[31];
double w;
V t;
void F(V &v, int i, int c) {
  if (i == b) {
    V u(v);
    sort(u.rbegin(), u.rend());
    w = min(w, inner_product(u.begin(), u.end(), t.begin(), 0.));
  } else if (c < n) {
    for (int j = 1; j <= m; ++j) {
      int x = d[i][j];
      if (!f[j] && _ - x) {
        f[j] = 1;
        v.push_back(x);
        F(v, j, c + 1);
        v.pop_back();
        f[j] = 0;
      }
    }
  }
}
int main() {
  int i, j, x, y, z;
  V v;
  while (scanf("%d%d%d%d%d", &n, &m, &p, &a, &b), n) {
    t.resize(n);
    fill(d[0], d[0] + 31 * 31, _);
    for (i = 0; i < n; ++i)
      scanf("%d", &x), t[i] = 1. / x;
    while (p--)
      scanf("%d%d%d", &x, &y, &z), d[x][y] = d[y][x] = z;
    w = 1e9;
    sort(t.begin(), t.end());
    F(v, a, 0);
    if (w == 1e9)
      puts("Impossible");
    else
      printf("%f\\n", w);
  }
  return 0;
}
Problem p00719 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, m, p, s, g;
  int t[8], edge[30][30];
  double dp[1 << 8][30];
  const double INF = 1e8;

  while (cin >> n >> m >> p >> s >> g, n) {
    fill_n(*edge, 30 * 30, -1);
    fill_n(*dp, (1 << 8) * 30, INF);
    s--, g--;

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

    for (int i = 0; i < p; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      a--, b--;
      edge[a][b] = c;
      edge[b][a] = c;
    }

    for (int i = 0; i < 1 << n; i++) {
      dp[i][s] = 0.0;
    }

    double ret = INF;
    for (int i = (1 << n) - 1; i >= 0; i--) { // 今のチケット
      ret = min(ret, dp[i][g]);
      for (int j = 0; j < m; j++) { // 今の街
        if (dp[i][j] != INF) {
          for (int k = 0; k < n; k++) { // 使うチケット
            if (i >> k & 1) {
              for (int l = 0; l < m; l++) { // 次の街
                if (edge[j][l] != -1) {
                  dp[i & ~(1 << k)][l] =
                      min(dp[i & ~(1 << k)][l],
                          dp[i][j] + (double)edge[j][l] / t[k]);
                }
              }
            }
          }
        }
      }
    }
    if (ret == INF)
      puts("Impossible");
    else
      cout << fixed << setprecision(8) << ret << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

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

struct edge {
  int to, cost;
  edge(int to, int cost) : to(to), cost(cost){};
};
vector<edge> info[30];
int ticket[8];
int n, m, p, a, b;
double used[1 << 8][30];
typedef pair<int, int> Pi;
typedef pair<double, Pi> Pii;
const double INF = 1e9;
#define fr first
#define sc second
double Dijkstra() {
  priority_queue<Pii, vector<Pii>, greater<Pii>> que;
  que.push(Pii(0.0, Pi(a, 0)));
  fill_n(*used, 30 * (1 << 8), INF);
  used[0][a] = 0.0;
  while (!que.empty()) {
    Pii p = que.top();
    que.pop();
    if (used[p.sc.sc][p.sc.fr] < p.fr)
      continue;
    if (p.sc.fr == b)
      return p.fr;
    for (int i = 0; i < info[p.sc.fr].size(); i++) {
      edge e = info[p.sc.fr][i];
      for (int j = 0; j < n; j++) { // チケット
        double next = p.fr + (double)e.cost / ticket[j];
        if (p.sc.sc >> j & 1)
          continue;
        if (used[p.sc.sc | 1 << j][e.to] < next)
          continue;
        que.push(Pii(next, Pi(e.to, p.sc.sc | 1 << j)));
        used[p.sc.sc | 1 << j][e.to] = next;
      }
    }
  }
  return -1;
}

int main() {

  while (cin >> n >> m >> p >> a >> b, n) {
    a--, b--;
    for (int i = 0; i < n; i++) {
      cin >> ticket[i];
    }
    for (int i = 0; i < p; i++) {
      int x, y, z;
      cin >> x >> y >> z;
      x--, y--;
      info[x].push_back(edge(y, z));
      info[y].push_back(edge(x, z));
    }
    double ret = Dijkstra();
    if (ret == -1)
      cout << "Impossible" << endl;
    else
      cout << fixed << setprecision(5) << ret << endl;

    for (int i = 0; i < m; i++)
      info[i].clear();
  }
  return (0);
}
Problem p00719 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <bits/stdc++.h>
using namespace std;
#define P pair<int, double>
#define to first
#define cost second
#define PB push_back

void f(int n, int m, int p, int a, int b) {
  vector<double> t(n);
  vector<vector<P>> es(m + 1);

  for (int i = 0; i < n; i++) {
    cin >> t[i];
  }
  for (int i = 0; i < p; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    es[x].PB(P(y, z));
    es[y].PB(P(x, z));
  }

  vector<vector<double>> dp(1 << n, vector<double>(m + 1, DBL_MAX));

  dp[0][a] = 0;

  for (int i = 0; i < (1 << n); i++) {
    for (int j = 1; j <= m; j++) {
      if (dp[i][j] == DBL_MAX)
        continue;
      for (int k = 0; k < n; k++) {
        if (i & (1 << k))
          continue;
        for (auto e : es[j]) {
          if (dp[i | (1 << k)][e.to] > dp[i][j] + e.cost / t[k]) {
            dp[i | (1 << k)][e.to] = dp[i][j] + e.cost / t[k];
          }
        }
      }
    }
  }

  double ans = DBL_MAX;
  for (int i = 0; i < (1 << n); i++)
    ans = min(ans, dp[i][b]);

  if (ans == DBL_MAX) {
    cout << "Impossible" << endl;
  } else {
    printf("%.10lf\\n", (ans == DBL_MAX ? -1 : ans));
  }
}

int main() {
  int n, m, p, a, b;
  while (true) {
    cin >> n >> m >> p >> a >> b;
    if (n == 0)
      break;
    f(n, m, p, a, b);
  }
}


Error: TLE

Fixed submission:


#include <bits/stdc++.h>
using namespace std;
#define P pair<int, double>

double ans;
int n, a, b;
vector<vector<P>> es;
vector<vector<double>> v;

void dfs(int x, int y, int m, vector<double> w) {
  if (m > n)
    return;
  if (x == b) {
    v.push_back(w);
    return;
  }
  for (int i = 0; i < es[x].size(); i++) {
    if (es[x][i].first == y)
      continue;
    w.push_back(es[x][i].second);
    dfs(es[x][i].first, x, m + 1, w);
    w.pop_back();
  }
}

void f(int m, int p) {
  vector<double> t;
  for (int i = 0; i < n; i++) {
    double c;
    cin >> c;
    t.push_back(c);
  }
  sort(t.begin(), t.end());
  reverse(t.begin(), t.end());
  es.resize(m + 1);
  for (int i = 0; i < p; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    es[x].push_back(P(y, z));
    es[y].push_back(P(x, z));
  }

  dfs(a, -1, 0, vector<double>());
  //  cout<<v.size()<<endl;
  if (v.empty()) {
    cout << "Impossible" << endl;
  } else {
    for (auto e : v) {
      double res = 0;
      sort(e.begin(), e.end());
      reverse(e.begin(), e.end());
      for (int i = 0; i < e.size(); i++) {
        //        cout<<e[i]<<' '<<t[i]<<endl;
        res += e[i] / t[i];
      }
      //      cout<<res<<endl;
      ans = min(ans, res);
    }
    printf("%.10lf\\n", ans);
  }
}

int main() {
  int m, p;
  while (true) {
    cin >> n >> m >> p >> a >> b;
    ans = DBL_MAX;
    if (n == 0)
      break;
    f(m, p);
  }
}
Problem p00722 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
int main() {
  int a, d, n, i, j;
  while (1) {
    int count = 0;
    long long int b = 0, c = 0;
    int k = 0;
    cin >> a >> d >> n;
    if (a == 0 && d == 0 && n == 0)
      break;
    while (k != n) {
      bool yakusu = false;
      if (count == 0) {
        b = a;
        if (b == 1) {
          count++;
          continue;
        } else if (b >= 2) {
          for (j = 2; j * j < b + 1; j++) {
            if (b % j == 0) {
              yakusu = true;
              break;
            }
          }
        }
        count++;
        if (!yakusu) {
          c = b;
          k++;
          // cout << c <<  " " << "k = " << k << endl;
        }
      } else if (count != 0) {
        b = a + d * count;
        // cout  << "b = " << b << " d = " << d << " count = " << count << "
        // d*count = " << d*count << endl;
        for (j = 2; j * j < b + 1; j++) {
          if (b % j == 0) {
            yakusu = true;
            break;
          }
        }
        count++;
        if (!yakusu) {
          c = b;
          k++;
          // cout << c  << " " << "k = " << k << endl;
        }
      }
    }
    cout << c << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
int main() {
  int a, d, n, i, j;
  while (1) {
    int count = 0;
    long long int b = 0, c = 0;
    int k = 0;
    cin >> a >> d >> n;
    if (a == 0 && d == 0 && n == 0)
      break;
    while (k != n) {
      bool yakusu = false;
      if (count == 0) {
        b = a;
        if (b == 1) {
          count++;
          continue;
        } else if (b >= 2) {
          for (j = 2; j < b; j++) {
            if (b % j == 0) {
              yakusu = true;
              break;
            }
          }
        }
        count++;
        if (!yakusu) {
          c = b;
          k++;
          // cout << c <<  " " << "k = " << k << endl;
        }
      } else if (count != 0) {
        b = a + d * count;
        // cout  << "b = " << b << " d = " << d << " count = " << count << "
        // d*count = " << d*count << endl;
        for (j = 2; j < b; j++) {
          if (b % j == 0) {
            yakusu = true;
            break;
          }
        }
        count++;
        if (!yakusu) {
          c = b;
          k++;
          // cout << c  << " " << "k = " << k << endl;
        }
      }
    }
    cout << c << endl;
  }
  return 0;
}
Problem p00722 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <string.h>
#define MAXNUM 1000000

using namespace std;

int main(void) {
  int flag[MAXNUM + 1], a, d, n, i, j, count;
  fill(flag, flag + MAXNUM, 1);
  flag[0] = flag[1] = 0;
  for (i = 2; i <= sqrt((double)MAXNUM); i++) {
    if (flag[i]) {
      for (j = i * 2; j <= MAXNUM; j += i)
        flag[j] = 0;
    }
  }
  while (1) {
    scanf("%d%d%d", &a, &d, &n);
    if (a == 0 && d == 0 && n == 0)
      break;
    i = count = 0;
    while (1) {
      if (flag[a + d * i]) {
        count++;
        if (count == n)
          break;
      }
      i++;
    }
    printf("%d\\n", a + d * i);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <string.h>
#define MAXNUM 1000000

using namespace std;

int main(void) {
  int flag[MAXNUM + 1], a, d, n, i, j, count;
  fill(flag, flag + MAXNUM, 1);
  flag[0] = flag[1] = 0;
  for (i = 2; i <= sqrt((double)MAXNUM); i++) {
    if (flag[i]) {
      for (j = i * 2; j <= MAXNUM; j += i)
        flag[j] = 0;
    }
  }
  while (1) {
    scanf("%d%d%d", &a, &d, &n);
    i = count = 0;
    while (1) {
      if (flag[a + d * i]) {
        count++;
        if (count == n)
          break;
      }
      i++;
    }
    printf("%d\\n", a + d * i);
  }
  return 0;
}
Problem p00722 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int a = 0, d = 0, n = 0;
  int sum = 0;

  while (1) {
    int count = 0;
    cin >> a >> d >> n;
    if (a == 0 && d == 0 && n == 0)
      break;
    sum = a;
    while (1) {
      if (sum == 2) {
        count++;
      } else if (sum > 2) {
        bool flag = true;
        if (sum % 2 == 0)
          flag = false;
        for (int i = 3; i <= sqrt(sum); i += 2) {
          if (sum % i == 0) {
            flag = false;
            break;
          }
        }
        if (flag)
          count++;
      }

      if (count == n)
        break;
      sum += d;
    }
    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int a = 0, d = 0, n = 0;
  int sum = 0;

  while (1) {
    int count = 0;
    cin >> a >> d >> n;
    if (a == 0 && d == 0 && n == 0)
      break;
    sum = a;
    while (1) {
      if (sum == 2) {
        count++;
      } else if (sum > 2) {
        bool flag = true;
        for (int i = 3; i < sum; i += 2) {
          if (sum % i == 0) {
            flag = false;
            break;
          }
        }
        if (flag)
          count++;
      }

      if (count == n)
        break;
      sum += d;
    }
    cout << sum << endl;
  }

  return 0;
}
Problem p00722 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::endl;

bool is_prime(int n) {
  if (n < 2) {
    return false;
  }
  if (n == 2 || n == 3) {
    return true;
  }
  for (int i = 3; i < n; i = i + 2) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}

int main(void) {
  int N = 1100000;
  int a, d, n;
  cin >> a;
  cin >> d;
  cin >> n;

  std::vector<bool> prime(N);
  for (int i = 0; i < N + 1; i++) {
    prime[i] = true;
  }
  prime[0] = false;
  prime[1] = false;
  for (int i = 2; i <= sqrt(N); i++) {
    if (is_prime(i)) {
      for (int j = 2; i * j < N + 1; j++) {
        prime[i * j] = false;
      }
    }
  }

  while (a != 0 || d != 0 || n != 0) {
    bool find = false;
    int count = 0;
    int b;
    for (int i = 0; !find; i++) {
      b = a + (d * i);
      if (prime[b]) {
        count++;
      }
      if (count == n) {
        cout << b << endl;
        find = true;
      }
    }

    cin >> a;
    cin >> d;
    cin >> n;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::endl;

bool is_prime(int n) {
  if (n < 2) {
    return false;
  }
  if (n == 2 || n == 3) {
    return true;
  }
  for (int i = 3; i < n; i = i + 2) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}

int main(void) {
  int N = 1000000;
  int a, d, n;
  cin >> a;
  cin >> d;
  cin >> n;

  std::vector<bool> prime(N);
  for (int i = 0; i < N + 1; i++) {
    prime[i] = true;
  }
  prime[0] = false;
  prime[1] = false;
  for (int i = 2; i <= sqrt(N); i++) {
    if (is_prime(i)) {
      for (int j = 2; i * j < N + 1; j++) {
        prime[i * j] = false;
      }
    }
  }

  while (a != 0 || d != 0 || n != 0) {
    bool find = false;
    int count = 0;
    int b;
    for (int i = 0; !find; i++) {
      b = a + (d * i);
      if (prime[b]) {
        count++;
      }
      if (count == n) {
        cout << b << endl;
        find = true;
      }
    }

    cin >> a;
    cin >> d;
    cin >> n;
  }

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

#include <cmath>
#include <iostream>
#include <vector>

#define N 1000000
using namespace std;

vector<int> p(1000000);

int main() {
  int a, d, n;
  for (int i = 0; i < N; i++)
    p[i] = 0;
  for (int i = 3; i < N; i += 2)
    p[i] = 1;
  p[2] = 1;
  for (int i = 3; i < (int)sqrt(N); i += 2) {
    if (p[i] == 0)
      continue;
    for (int j = i + i; j <= N; j = j + i)
      p[j] = 0;
  }

  while (cin >> a >> d >> n, a != 0 && d != 0 && n != 0) {
    int pr;
    for (int i = 0, j = 0; j < n;) {
      if (p[a + i * d]) {
        pr = a + (i * d);
        j++;
      }
      i++;
    }
    cout << pr << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int eratosthenes(int);

int main() {
  int a, d, n;

  while (cin >> a >> d >> n, a != 0 && d != 0 && n != 0) {
    int pr = a;
    for (int i = 0, j = 0; j < n;) {
      if (eratosthenes(a + (i * d))) {
        pr = a + (i * d);
        j++;
      }
      i++;
    }
    cout << pr << endl;
  }
  return 0;
}

int eratosthenes(int n) {
  bool p[n];
  for (int i = 1; i <= n; i++)
    p[i] = 0;
  for (int i = 3; i <= n; i += 2)
    p[i] = 1;
  p[2] = 1;
  for (int i = 3; i <= (int)sqrt(n); i += 2) {
    if (p[i] == 0)
      continue;
    for (int j = i + i; j <= n; j = j + i)
      p[j] = 0;
  }
  return p[n];
}
Problem p00722 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

bool is_prime(int n) {
  if (n == 2 || n == 3) {
    return true;
  }

  if (n < 2 || n % 2 == 0) {
    return false;
  }

  for (int i = 3; i < n; i = i + 2) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}

int main(void) {
  int N = 1100000;
  vector<bool> prime(N);
  for (int i = 0; i < N + 1; ++i) {
    prime[i] = true;
  }
  prime[0] = false;
  prime[1] = false;
  for (int i = 2; i <= sqrt(N); ++i) {
    if (is_prime(i)) {
      for (int j = 2; i * j < N + 1; ++j) {
        prime[i * j] = false;
      }
    }
  }
  int a, d, n;
  while (cin >> a >> d >> n, n) {
    int count = 0;
    bool find = false;
    for (int i = a; !find && i < N; i = i + d) {
      if (prime[i]) {
        ++count;
        if (count == n) {
          cout << i << endl;
          find = true;
        }
      }
    }
  }
  return 0;
}

Error: WA

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

bool is_prime(int n) {
  if (n == 2 || n == 3) {
    return true;
  }

  if (n < 2 || n % 2 == 0) {
    return false;
  }

  for (int i = 3; i < n; i = i + 2) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}

int main(void) {
  int N = 1000000;
  vector<bool> prime(N);
  for (int i = 0; i < N + 1; ++i) {
    prime[i] = true;
  }
  prime[0] = true;
  prime[1] = true;
  for (int i = 2; i <= sqrt(N); ++i) {
    if (is_prime(i)) {
      for (int j = 2; i * j < N + 1; ++j) {
        prime[i * j] = false;
      }
    }
  }
  int a, d, n;
  while (cin >> a >> d >> n, (a || d || n)) {
    int count = 0;
    bool find = false;
    for (int i = a; !find && i < N; i = i + d) {
      if (prime[i]) {
        ++count;
        if (count == n) {
          cout << i << endl;
          find = true;
        }
      }
    }
  }
  return 0;
}
Problem p00722 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

const int MAX = 1000000;
int s[MAX];

void eratos();

int main(int argc, char const *argv[]) {
  int a, d, n, tmp;
  int cnt = 0;
  vector<int> results;

  eratos();

  while (1) {
    cin >> a >> d >> n;
    if (a == 0 && d == 0 && n == 0)
      break;
    tmp = a;
    cnt = 0;
    while (1) {
      if (s[tmp])
        cnt++;
      if (cnt == n)
        break;
      tmp += d;
    }
    results.push_back(tmp);
  }

  for (auto it = results.begin(); it != results.end(); it++) {
    cout << *it << endl;
  }

  return 0;
}

void eratos() {
  for (int i = 2; i < MAX; i++) {
    s[i] = 1;
  }

  int j = 0;
  int k = 2;
  do {
    if (s[k] != 0) {
      j++;
      for (int i = k * k; i <= MAX; i += k) {
        s[i] = 0;
      }
    }
    k++;
  } while (k * k <= MAX);
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

bool check(int tmp);

int main(int argc, char const *argv[]) {
  int a, d, n, tmp;
  int cnt = 0;
  vector<int> results;

  while (1) {
    cin >> a >> d >> n;
    if (a == 0 && d == 0 && n == 0)
      break;
    tmp = a;
    cnt = 0;
    while (1) {
      if ((tmp % 2 == 1) && check(tmp))
        cnt++;
      if (cnt == n)
        break;
      tmp += d;
    }
    results.push_back(tmp);
  }

  for (auto it = results.begin(); it != results.end(); it++) {
    cout << *it << endl;
  }

  return 0;
}

bool check(int tmp) {
  if (tmp == 2)
    return true;

  if (tmp < 2)
    return false;

  for (int i = 3; i < tmp / 2; i += 2) {
    if ((tmp % i) == 0)
      return false;
  }

  return true;
}