Problem p01163 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
// #include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int primes[3000];
int siz = 0;
int num[40000] = {0};
bool isprime[200000] = {false};

int main() {
  for (int i = 2; i < 20000; i++) {
    if (isprime[i])
      continue;
    primes[siz++] = i;
    for (int j = i * 2; j < 20000; j += i) {
      isprime[j] = true;
    }
  }
  for (int a = 0; primes[a] <= 15000; a++) {
    for (int b = a; primes[b] <= 15000; b++) {
      for (int c = b; primes[c] <= 15000; c++) {
        if (primes[a] + primes[b] <= primes[c])
          continue;
        if (primes[a] + primes[b] + primes[c] > 30000)
          continue;
        num[primes[a] + primes[b] + primes[c]]++;
        // LOG("%d %d %d\\n",primes[a],primes[b],primes[c]);
      }
    }
  }
  // cout << "end works" << endl;

  int T;
  while (cin >> T, T) {
    cout << num[T] << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
// #include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int primes[3000];
int siz = 0;
int num[40000] = {0};
bool isprime[200000] = {false};

int main() {
  for (int i = 2; i < 20000; i++) {
    if (isprime[i])
      continue;
    primes[siz++] = i;
    for (int j = i * 2; j < 20000; j += i) {
      isprime[j] = true;
    }
  }
  for (int a = 0; primes[a] <= 15000; a++) {
    for (int b = a; primes[b] <= 15000; b++) {
      for (int c = b; primes[c] <= 15000; c++) {
        if (primes[a] + primes[b] <= primes[c])
          continue;
        num[primes[a] + primes[b] + primes[c]]++;
        // LOG("%d %d %d\\n",primes[a],primes[b],primes[c]);
      }
    }
  }
  // cout << "end works" << endl;

  int T;
  while (cin >> T, T) {
    cout << num[T] << endl;
  }
}