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

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

int main() {
  long long n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    long long ans = 0;
    long long a = 5;
    while (a <= n) {
      ans += n / a;
      a *= 5;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  long long n;
  while (1) {
    cin >> n;
    if (n = 0) {
      break;
    }
    long long ans = 0;
    long long a = 5;
    while (a <= n) {
      ans += n / a;
      a *= 5;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00052 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for (int i = s; i < (int)n; i++)
#define per(i, n) for (int i = n; i >= 0; i--)
#define ROF(i, s, n) for (int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\\n"
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n)                                                             \\
  for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define CLR(a) memset((a), 0, sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\\n";
#define debug(x)                                                               \\
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << "\\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue<int, vector<int>, greater<int>> minpq;
static const double EPS = 1e-10;
static const double PI = acos(-1.0);
static const int mod = 10007;
static const int INF = 1 << 25;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = {-1, 0, 1, 0, 1, -1, 1, -1};
static const int dy[] = {0, -1, 0, 1, 1, 1, -1, -1};
int ans;
int n;

int main() {
  while (~scanf("%d", &n) && n) {
    ans = 0;
    while (n != 0) {
      n /= 5;
      ans += n;
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int main() {
  unsigned long long int a = 1, count = 0;
  int n, i;
  while (scanf("%d", &n) != 0) {
    if (n == 0)
      break;
    for (i = 1; i <= n; i++)
      a *= i;
    while (a % 10 == 0) {
      if (a % 10 != 0)
        break;
      count++;
      a /= 10;
    }
    printf("%lld\\n", count);
    count = 0;
  }
  return 0;
}
Problem p00052 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int main() {
  int n;
  while (1) {
    int ct = 0;
    cin >> n;
    if (!n)
      break;
    while (n /= 5)
      ct += n;
    cout << ct << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n;
  while (1) {
    int ct = 0;
    cin >> n;
    if (!n)
      break;
    while (n / 5)
      ct += n;
    cout << ct << endl;
  }
  return 0;
}
Problem p00052 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

using namespace std;

int main(void) {
  long long int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    long long int ans = 0;
    bool ac = true;
    // long long int b=5;
    while (n >= 5) {
      ans += n / 5;
      n = n / 5;
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

using namespace std;

int main(void) {
  int n;
  while (~scanf("%d", &n)) {
    int ans = 0;
    bool ac = true;
    while (ac) {
      if (n % 5 != 0)
        ac = false;
      ans += n / 5;
      n = n / 5;
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00053 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int main() {
  bool f[105000];
  int n;
  long long int sum[105000];
  memset(f, false, sizeof(f));
  sum[0] = 0;
  for (int i = 2; i * i <= 105000; i++) {
    if (f[i])
      continue;
    for (int j = i * i; j < 105000; j += i)
      f[j] = true;
  }
  int k = 1;
  for (int j = 2; j <= 105000; j++) {
    if (!f[j]) {
      sum[k] = sum[k - 1] + j;
      k++;
    }
  }
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    cout << sum[n] << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int main() {
  bool f[105000];
  int n;
  long long int sum[105000];
  memset(f, false, sizeof(f));
  sum[0] = 0;
  for (int i = 2; i <= 105000; i++) {
    if (f[i])
      continue;
    for (int j = i * i; j < 105000; j += i)
      f[j] = true;
  }
  int k = 1;
  for (int j = 2; j <= 105000; j++) {
    if (!f[j]) {
      sum[k] = sum[k - 1] + j;
      k++;
    }
  }
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    cout << sum[n] << endl;
  }
  return 0;
}
Problem p00053 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int n, m, sum, w, i;

  long img[10000];
  img[0] = 2;
  m = 1;
  for (long t = 3; m <= 10000; t += 2) {
    w = 0;
    for (i = 0; (img[i] <= sqrt(t)) && (i <= m); i++) {
      if (t % img[i] == 0) {
        w++;
      }
    }
    if (w == 0) {
      img[m] = t;
      m++;
    }
  }
  while (1) {
    cin >> n;
    if (n == 0) {
      return 0;
    }
    sum = 0;
    for (i = 0; i < n; i++) {
      sum += img[i];
    }
    cout << sum << "\\n";
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int n, m, sum, w;
  while (1) {
    cin >> n;
    if (n == 0) {
      return 0;
    }
    m = 0;
    sum = 0;
    for (int t = 2; m < n; t++) {
      w = 0;
      for (int i = 2; i <= sqrt(t); i++) {
        if (t % i == 0) {
          w++;
        }
      }
      if (w == 0) {
        sum += t;
        m++;
      }
    }
    cout << sum << "\\n";
  }
}
Problem p00053 (C++) — buggy submission, verdict: Runtime Error

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

#define MAX 1000000

vector<int> prime;
bool is_prime[MAX + 1];

void Sieve() {

  for (int i = 0; i <= MAX; i++) {
    is_prime[i] = true;
  }

  is_prime[0] = is_prime[1] = false;

  for (int i = 2; i <= MAX; i++) {
    if (is_prime[i]) {
      prime.push_back(i);
      for (int j = 2 * i; j <= MAX; j += i)
        is_prime[j] = false;
    }
  }
}

int main() {

  Sieve();

  int n;
  while (cin >> n && n) {
    int sum = 0;
    for (int i = 0; i < n; i++) {
      sum += prime[i];
    }
    cout << sum << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

#define MAX 100000

vector<int> prime;
bool is_prime[MAX];

void Sieve() {

  for (int i = 0; i <= MAX; i++) {
    is_prime[i] = true;
  }

  is_prime[0] = is_prime[1] = false;

  for (int i = 2; i <= MAX; i++) {
    if (is_prime[i]) {
      prime.push_back(i);
      for (int j = 2 * i; j <= MAX; j += i)
        is_prime[j] = false;
    }
  }
}

int main() {

  Sieve();

  int n;
  while (cin >> n && n) {
    int sum = 0;
    for (int i = 0; i < n; i++) {
      sum += prime[i];
    }
    cout << sum << endl;
  }

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

#include <cstdio>
using namespace std;

int main() {
  bool prime[1000000];
  int n, p, res;

  while (true) {
    scanf("%d", &n);
    if (n == 0)
      break;

    p = 2;
    res = 0;
    for (int i = 0; i < 1000000; i++)
      prime[i] = true;
    for (int i = 0; i < n; i++) {
      while (!prime[p])
        p++;
      res += p;
      for (int j = p; j < 1000000; j += p) {
        prime[j] = false;
      }
      p++;
    }

    printf("%d\\n", res);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
using namespace std;

int main() {
  bool prime[20000];
  int n, p, res;

  while (true) {
    scanf("%d", &n);
    if (n == 0)
      break;

    p = 2;
    res = 0;
    for (int i = 0; i < 20000; i++)
      prime[i] = true;
    for (int i = 0; i < n; i++) {
      while (!prime[p])
        p++;
      res += p;
      for (int j = p; j < 20000; j += p) {
        prime[j] = false;
      }
      p++;
    }

    printf("%d\\n", res);
  }

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

#include <iostream>
using namespace std;
#define MAX 1000000
void sosu();

int prime[1000000] = {0};

int main() {
  int n, cnter, cnt, ans;
  sosu();
  while (1) {
    cnt = 0;
    cnter = 2;
    ans = 0;
    cin >> n;
    if (n == 0)
      break;
    while (1) {
      if (cnt == n)
        break;
      if (prime[cnter] == 0) {
        ans += cnter;
        cnt++;
      }
      cnter++;
    }
    cout << ans << endl;
  }
}

void sosu() {
  for (int i = 2; i < MAX / 2; i++) {
    for (int j = 2; i * j <= MAX; j++) {
      prime[i * j] = 1;
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define MAX 10000000
void sosu();

int prime[10000000] = {0};

int main() {
  int n, cnter, cnt, ans;
  sosu();
  while (1) {
    cnt = 0;
    cnter = 2;
    ans = 0;
    cin >> n;
    if (n == 0)
      break;
    while (1) {
      if (cnt == n)
        break;
      if (prime[cnter] == 0) {
        ans += cnter;
        cnt++;
      }
      cnter++;
    }
    cout << ans << endl;
  }
}

void sosu() {
  for (int i = 2; i < MAX / 2; i++) {
    for (int j = 2; i * j <= MAX; j++) {
      prime[i * j] = 1;
    }
  }
}
Problem p00053 (C++) — buggy submission, verdict: Runtime Error


#include <cmath>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define MAX 105000
using namespace std;

int main() {
  /*for(int k = 1; k<MAX;k++)
  {
          if(PrimeCheck(v[k+1]))
          {
                  //cout << v[k+1] << " ";
                  v2[d] = v[k+1];
                  d++;
          }
  }
  //rep(i,d) cout << v2[i] << " ";
  //cout << d << endl;*/
  int number;
  while (cin >> number, number) {

    int counter = 0;
    vector<bool> v(MAX + 1, 0);
    vector<int> v2(MAX, 0);
    rep(i, MAX) v[i + 1] = true;
    long long answer = 0;
    int lim = (int)sqrt(number);
    int d = 0;
    //		cout << lim <<" ";
    for (int i = 2; i < MAX; i++) {
      if (v[i]) {
        for (int j = 2; j < MAX / i; j++) {
          v[j * i] = false;
          // cout << " " << i << "i*j=  " << i*j;
        }
        v2[d] = i;
        d++;
      }
    }
    // rep(i,number) cout << v[i+1] << " ";
    for (int i = 2; i < MAX + 1; i++) {
      if (v[i]) {
        answer += i;
        counter++;
        // cout << " " << i;
        if (counter == number)
          break;
      }
    }
    cout /*<< " answer is "*/ << answer << endl;
  }
  return 0;
}

Error: -6
malloc(): corrupted top size


Fixed submission:


#include <cmath>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define MAX 105000
using namespace std;

int main() {
  /*for(int k = 1; k<MAX;k++)
  {
          if(PrimeCheck(v[k+1]))
          {
                  //cout << v[k+1] << " ";
                  v2[d] = v[k+1];
                  d++;
          }
  }
  //rep(i,d) cout << v2[i] << " ";
  //cout << d << endl;*/
  int number;
  while (cin >> number, number) {

    int counter = 0;
    vector<bool> v(MAX + 1, 0);
    vector<int> v2(10008, 0);
    rep(i, MAX) v[i + 1] = true;
    long long answer = 0;
    int lim = (int)sqrt(number);
    int d = 0;
    //		cout << lim <<" ";
    for (int i = 2; i < MAX; i++) {
      if (v[i]) {
        for (int j = 2; j < MAX / i; j++) {
          v[j * i] = false;
          // cout << " " << i << "i*j=  " << i*j;
        }
        v2[d] = i;
        d++;
      }
    }
    // rep(i,number) cout << v[i+1] << " ";
    for (int i = 2; i < MAX + 1; i++) {
      if (v[i]) {
        answer += i;
        counter++;
        // cout << " " << i;
        if (counter == number)
          break;
      }
    }
    cout /*<< " answer is "*/ << answer << endl;
  }
  return 0;
}
Problem p00053 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  int n;
  bool pr[105000] = {0};
  for (int i = 2; i < 400; i++)
    for (int j = i; j * i < 105000; j++)
      pr[j * i] = 1;
  while (cin >> n, n) {
    int ans = 0, cu = 2;
    while (n--) {
      while (pr[cu])
        cu++;
      ans += cu++;
    }
    cout << ans << endl;
  }
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

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

int main() {
  int n;
  vector<int> vec;
  for (int i = 2; i < 66000; i++) {
    for (int j = i; j * i < 130000; j++) {
      vec.push_back(i * j);
    }
  }
  sort(vec.begin(), vec.end());
  while (cin >> n, n) {
    int ans = 0, cu = 2;
    while (n--) {
      while (binary_search(vec.begin(), vec.end(), cu))
        cu++;
      ans += cu++;
    }
    cout << ans << endl;
  }
}
Problem p00053 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <math.h>

using namespace std;

int main() {

  int prime[1000000];

  for (int i = 0; i < 1000000; i++)
    prime[i] = 1;

  prime[0] = 0;
  prime[1] = 0;

  for (int i = 2; i < sqrt(1000000); i++) {
    if (prime[i] == 1)
      for (int j = i * 2; j < 1000000; j += i)
        prime[j] = 0;
  }

  while (1) {

    int in;

    cin >> in;
    if (in == 0)
      break;
    int out = 0, sum = 0;
    for (int i = 0; out < in; i++) {
      if (prime[i] == 1) {
        out++;
        sum += i;
      }
    }

    cout << sum << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <math.h>

using namespace std;

int main() {

  int prime[10001];

  for (int i = 0; i < 10001; i++)
    prime[i] = 1;

  prime[0] = 0;
  prime[1] = 0;

  for (int i = 2; i < sqrt(10001); i++) {
    if (prime[i] == 1) {
      for (int j = i * 2; j < 10001; j += i)
        prime[j] = 0;
    }
  }

  while (1) {

    int in;

    cin >> in;
    if (in == 0)
      break;
    int out = 0, sum = 0;
    for (int i = 0; out < in; i++) {
      if (prime[i] == 1) {
        out++;
        sum += i;
      }
    }

    cout << sum << endl;
  }

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

#include <iostream>
#define MAX 999999

using namespace std;

bool is_prime[MAX];

void isprime(void) {

  for (int i = 0; i < MAX; i++)
    is_prime[i] = true;
  is_prime[0] = is_prime[1] = false;

  for (int i = 2; i * i < MAX; i++) {
    if (is_prime[i]) {
      for (int j = i + i; j < MAX; j += i) {
        is_prime[j] = false;
      }
    }
  }
}

int main(void) {

  isprime();

  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    int sum = 0;
    for (int i = 2;; i++) {
      if (is_prime[i]) {
        sum += i;
        n--;
      }
      if (n == 0)
        break;
    }
    cout << sum << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#define MAX 100000
using namespace std;

bool is_prime[MAX];

void isprime(void) {

  for (int i = 0; i < MAX; i++)
    is_prime[i] = true;
  is_prime[0] = is_prime[1] = false;

  for (int i = 2; i * i < MAX; i++) {
    if (is_prime[i]) {
      for (int j = i + i; j < MAX; j += i) {
        is_prime[j] = false;
      }
    }
  }
}

int main(void) {

  isprime();

  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    int temp = n;
    int sum = 0;
    for (int i = 2;; i++) {
      if (is_prime[i]) {
        sum += i;
        temp--;
      }
      if (temp == 0)
        break;
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p00053 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    int v[1000000];
    for (int i = 0; i < 1000000; i++)
      v[i] = 1;
    v[0] = v[1] = 0;
    for (int i = 2; i * i < 1000000; i++) {
      if (v[i]) {
        for (int j = i * 2; j < 1000000; j += i) {
          v[j] = 0;
        }
      }
    }
    long s = 0, i = 0;
    while (n--) {
      while (!v[i])
        i++;
      s += i++;
    }
    cout << s << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    int count = 0;
    int ans = 0;
    vector<int> sosu(0);
    for (int i = 2; count != n; i++) {
      if (i == 2) {
        ans += 2;
        sosu.push_back(2);
        count++;
      } else {
        for (int j = 0; j < sosu.size(); j++) {
          if (i % sosu[j] == 0)
            break;
          if (j == sosu.size() - 1) {
            sosu.push_back(i);
            ans += i;
            count++;
          }
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00053 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool isPrime(int n) {
  if (n == 2)
    return true;
  if (n % 2 == 0)
    return false;

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

int main() {

  int input;
  while (cin >> input, input) {
    int count = 0;
    int sum = 0;
    for (int i = 2;; ++i) {
      if (isPrime(i)) {
        count++;
        sum += i;
        if (count == input) {
          cout << sum << endl;
          break;
        }
      }
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

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

bool isPrime(int n) {
  if (n == 2)
    return true;
  if (n % 2 == 0)
    return false;

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

int main() {

  int input;
  while (cin >> input, input) {
    int count = 0;
    int sum = 0;
    for (int i = 2;; ++i) {
      if (isPrime(i)) {
        count++;
        sum += i;
        if (count == input) {
          cout << sum << endl;
          break;
        }
      }
    }
  }

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

#include <iostream>
using namespace std;

bool is_prime[1000001];

void sieve() {
  for (int i = 0; i < 1000001; i++) {
    is_prime[i] = true;
  }

  is_prime[0] = is_prime[1] = false;

  for (int i = 2; i < 1000001; i++) {
    if (is_prime[i]) {
      for (int j = 2 * i; j < 1000001; j += i) {
        is_prime[j] = false;
      }
    }
  }
}

int solve(int n) {
  int times = 0;
  int sum = 0;
  for (int i = 2; times < n; i++) {
    if (is_prime[i]) {
      times++;
      sum += i;
    }
  }
  return sum;
}

int main() {
  int n;
  sieve();
  while (cin >> n) {
    if (n == 0)
      break;
    cout << solve(n) << endl;
  }
}

Error: 0

Fixed submission:


#include <iostream>
using namespace std;

bool is_prime[10001];

void sieve() {
  for (int i = 0; i < 10001; i++) {
    is_prime[i] = true;
  }

  is_prime[0] = is_prime[1] = false;

  for (int i = 2; i < 10001; i++) {
    if (is_prime[i]) {
      for (int j = 2 * i; j < 10001; j += i) {
        is_prime[j] = false;
      }
    }
  }
}

int solve(int n) {
  int times = 0;
  int sum = 0;
  for (int i = 2; times < n; i++) {
    if (is_prime[i]) {
      times++;
      sum += i;
    }
  }
  return sum;
}

int main() {
  int n;
  sieve();
  while (cin >> n) {
    if (n == 0)
      break;
    cout << solve(n) << endl;
  }
}
Problem p00053 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <math.h>
using namespace std;

int main() {
  bool prime[500000] = {false};
  for (int i = 2; i <= sqrt(500000); i++) {
    if (!prime[i]) {
      for (int j = 2; j * i <= 500000; j++) {
        prime[j * i] = true;
      }
    }
  }
  int n;
  unsigned int sum;
  while (cin >> n) {
    if (n == 0)
      break;
    sum = 0;
    int count = 0;
    for (int i = 2;; i++) {
      if (!prime[i]) {
        sum += i;
        count++;
      }
      if (count == n)
        break;
    }
    cout << sum << endl;
  }
}


Error: 0

Fixed submission:

#include <iostream>
#include <math.h>
using namespace std;

int main() {
  bool prime[50000] = {false};
  for (int i = 2; i <= sqrt(50000); i++) {
    if (!prime[i]) {
      for (int j = 2; j * i <= 50000; j++) {
        prime[j * i] = true;
      }
    }
  }
  int n;
  unsigned int sum;
  while (cin >> n) {
    if (n == 0)
      break;
    sum = 0;
    int count = 0;
    for (int i = 2;; i++) {
      if (!prime[i]) {
        sum += i;
        count++;
      }
      if (count == n)
        break;
    }
    cout << sum << endl;
  }
}
Problem p00053 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

bool p[1000000];

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

  for (int i = 2; i < 1000000; i++) {
    p[i] = true;
  }
  for (int i = 2; i * i < 1000000; i++) {
    if (p[i]) {
      for (int j = 2 * i; j < 1000000; j += i) {
        p[j] = false;
      }
    }
  }

  int n;

  while (cin >> n, n) {
    int sum = 0;
    int f = 0;

    for (int i = 0; f != n; i++) {
      if (p[i]) {
        sum += i;
        f++;
      }
    }
    cout << sum << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

bool p[10000];
int prime[10000];

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

  for (int i = 2; i < 10000; i++) {
    p[i] = true;
  }
  for (int i = 2; i * i < 10000; i++) {
    if (p[i]) {
      for (int j = 2 * i; j < 10000; j += i) {
        p[j] = false;
      }
    }
  }

  int n;

  while (cin >> n, n) {
    int sum = 0;
    int f = 0;

    for (int i = 0; f != n; i++) {
      if (p[i]) {
        sum += i;
        f++;
      }
    }
    cout << sum << endl;
  }
}
Problem p00053 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
using namespace std;
int dp[10010];

int prime(int n) {
  int i, j, d;
  if (n == 1) {
    return dp[1] = 2;
  };
  d = dp[n - 1];
  for (i = d + 1;; i++) {
    for (j = 2;; j++) {
      if (i % j == 0)
        break;
      if (j == (int)sqrt(i) + 1) {
        return dp[n] = i;
      }
    }
  }
}

int main() {
  int n, i;
  long long sum = 0;
  while (scanf("%d", &n) && n) {
    if (n == 0)
      break;
    for (i = 1; i <= n; i++)
      sum += prime(i);
    printf("%d\\n", sum);
    sum = 0;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
using namespace std;
int dp[10010];

int prime(int n) {
  int i, j, d;
  if (n == 1) {
    return dp[1] = 2;
  };
  d = dp[n - 1];
  for (i = d + 1;; i++) {
    for (j = 2;; j++) {
      if (i % j == 0)
        break;
      if (j == i - 1) {
        return dp[n] = i;
      }
    }
  }
}

int main() {
  int n, i;
  long long sum = 0;
  while (scanf("%d", &n) && n) {
    if (n == 0)
      break;
    for (i = 1; i <= n; i++)
      sum += prime(i);
    printf("%ld\\n", sum);
    sum = 0;
  }
  return 0;
}