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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

string s;
int memo[2000][2000];
bool memo2[2000][2000];
int memo3[2000][2000];

int rec(int l, int r) {
  if (l > r)
    return 0;
  if (memo2[l][r])
    return memo[l][r];
  memo2[l][r] = 1;
  if (s[l] == s[r]) {
    memo3[l][r] = 0;
    if (l == r)
      return memo[l][r] = rec(l + 1, r - 1) + 1;
    else
      return memo[l][r] = rec(l + 1, r - 1) + 2;
  } else {
    int tmp1 = rec(l + 1, r);
    int tmp2 = rec(l, r - 1);

    if (tmp1 < tmp2) {
      memo3[l][r] = 1;
      return memo[l][r] = tmp2;
    } else {
      memo3[l][r] = -1;
      return memo[l][r] = tmp1;
    }
  }
}

int main() {
  while (cin >> s) {
    memset(memo2, 0, sizeof(memo2));
    rec(0, s.size() - 1);

    string res;
    bool f = 0;
    for (int l = 0, r = s.size() - 1; l <= r;) {
      if (memo3[l][r] > 0)
        r--;
      else if (memo3[l][r] < 0)
        l++;
      else {
        if (l == r)
          f = 1;
        res += string(1, s[l]);
        r--;
        l++;
      }
    }
    string res2 = res;
    reverse(res2.begin(), res2.end());

    if (f) {
      res2.erase(res2.begin());
    }
    cout << res << res2 << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

string s;
string memo[2000][2001];

bool memo2[2000][2001];

string rec(int l, int r) {
  if (l >= r)
    return "";
  if (memo[l][r].size())
    return memo[l][r];

  string res;
  bool seen[26] = {};
  int pos[26];
  memset(pos, -1, sizeof(pos));
  for (int i = r - 1; i >= l; --i) {
    if (pos[s[i] - 'A'] < 0) {
      pos[s[i] - 'A'] = i;
    }
  }
  for (int i = l; i < r; ++i) {
    int c = s[i] - 'A';
    if (seen[c])
      continue;
    seen[c] = 1;
    if (pos[c] >= 0) {
      if (i > pos[c])
        continue;
      string tmp = rec(i + 1, pos[c]);
      if (res.size() < tmp.size() + 1) {
        tmp.push_back(s[i]);
        res = tmp;
      }
    }
  }
  return memo[l][r] = res;
}

int main() {
  while (cin >> s) {

    REP(j, s.size() + 1) REP(i, j) memo[i][j] = "";
    string res = rec(0, s.size());

    string res2 = res;
    reverse(res.begin(), res.end());

    int l = 0, r = s.size() - 1;
    bool f = 0;
    for (int cnt = 0; cnt < res.size(); ++cnt) {
      while (s[l] != res[cnt])
        l++;
      while (s[r] != res[cnt])
        r--;
      if (cnt == res.size() - 1) {
        if (l == r)
          f = 1;
      }
      l++;
      r--;
    }

    if (f) {
      res2.erase(res2.begin());
    }
    cout << res << res2 << endl;
  }
}
Problem p01160 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
int DP[2002][2002], back[2002][2002];

template <typename T> vector<T> LCS(const vector<T> &a, const vector<T> &b) {
  memset(DP, 0, sizeof(DP));
  memset(back, 1, sizeof(back));
  for (int i = 1; i <= (int)a.size(); i++) {
    for (int j = 1; j <= (int)b.size(); j++) {
      if (a[i - 1] == b[j - 1]) {
        if (DP[i][j] < DP[i - 1][j - 1] + 1) {
          DP[i][j] = DP[i - 1][j - 1] + 1;
          back[i][j] = 0;
        }
      }
      if (DP[i][j] < DP[i - 1][j]) {
        DP[i][j] = DP[i - 1][j];
        back[i][j] = 1;
      }
      if (DP[i][j] < DP[i][j - 1]) {
        DP[i][j] = DP[i][j - 1];
        back[i][j] = -1;
      }
    }
  }
  int x = a.size(), y = b.size();
  vector<T> res;
  while (x > 0 && y > 0) {
    if (back[x][y] == 0) {
      res.push_back(a[x - 1]);
      x--;
      y--;
    } else if (back[x][y] > 0)
      x--;
    else
      y--;
  }
  std::reverse(res.begin(), res.end());
  return res;
}
string LCS(const string &a, const string &b) {
  vector<char> a_, b_, ret;
  for (int i = 0; i < (int)a.size(); i++)
    a_.push_back(a[i]);
  for (int i = 0; i < (int)b.size(); i++)
    b_.push_back(b[i]);
  ret = LCS(a_, b_);
  string res;
  for (int i = 0; i < (ret.size()); i++)
    res += ret[i];
  return res;
}
int main() {
  string in;
  while (cin >> in) {
    string a = in;
    reverse(a.begin(), a.end());
    cout << LCS(a, in) << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
template <typename T> vector<T> LCS(const vector<T> &a, const vector<T> &b) {
  int DP[a.size() + 1][b.size() + 1], back[a.size() + 1][b.size() + 1];
  memset(DP, 0, sizeof(DP));
  memset(back, 1, sizeof(back));
  for (int i = 1; i <= (int)a.size(); i++) {
    for (int j = 1; j <= (int)b.size(); j++) {
      if (a[i - 1] == b[j - 1]) {
        if (DP[i][j] < DP[i - 1][j - 1] + 1) {
          DP[i][j] = DP[i - 1][j - 1] + 1;
          back[i][j] = 0;
        }
      }
      if (DP[i][j] < DP[i - 1][j]) {
        DP[i][j] = DP[i - 1][j];
        back[i][j] = 1;
      }
      if (DP[i][j] < DP[i][j - 1]) {
        DP[i][j] = DP[i][j - 1];
        back[i][j] = -1;
      }
    }
  }
  int x = a.size(), y = b.size();
  vector<T> res;
  while (x > 0 && y > 0) {
    if (back[x][y] == 0) {
      res.push_back(a[x - 1]);
      x--;
      y--;
    } else if (back[x][y] > 0)
      x--;
    else
      y--;
  }
  std::reverse(res.begin(), res.end());
  return res;
}
string LCS(const string &a, const string &b) {
  vector<char> a_, b_, ret;
  for (int i = 0; i < (int)a.size(); i++)
    a_.push_back(a[i]);
  for (int i = 0; i < (int)b.size(); i++)
    b_.push_back(b[i]);
  ret = LCS(a_, b_);
  string res;
  for (int i = 0; i < (ret.size()); i++)
    res += ret[i];
  return res;
}
int main() {
  string in;
  while (cin >> in) {
    string a = in;
    reverse(a.begin(), a.end());
    cout << LCS(a, in) << endl;
  }

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

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

int n;
string s;
short dp[2002][2002];

int main() {
  while (cin >> s) {
    n = s.length();
    memset(dp, 0, sizeof(dp));

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

    for (int i = 1; i < n; i++) {
      for (int j = 0; j + i < n; j++) {
        int k = i + j;

        if (s[j] == s[k] && dp[j][k] < dp[j + 1][k - 1] + 2) {
          dp[j][k] = dp[j + 1][k - 1] + 2;
        }

        if (dp[j][k] < dp[j + 1][k]) {
          dp[j][k] = dp[j + 1][k];
        }
        if (dp[j][k] < dp[j][k - 1]) {
          dp[j][k] = dp[j][k - 1];
        }
      }
    }

    string ans = "";
    int x = n - 1;
    int y = 0;

    while (y < x) {
      if (dp[y][x] == dp[y + 1][x]) {
        y = y + 1;
      } else if (dp[y][x] == dp[y][x - 1]) {
        x = x - 1;
      } else if (s[y] == s[x] && dp[y + 1][x - 1] + 2 == dp[y][x]) {
        ans += s[x];
        x = x - 1;
        y = y + 1;
      }
    }

    string rev = ans;
    reverse(rev.begin(), rev.end());

    if (dp[0][n - 1] % 2 == 0) {
      ans += rev;
    } else {
      ans += s[x];
      ans += rev;
    }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

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

int n;
string s;
int dp[2002][2002];
int preX[2002][2002], preY[2002][2002];

int main() {
  while (cin >> s) {
    n = s.length();
    memset(dp, 0, sizeof(dp));

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

    for (int i = 1; i < n; i++) {
      for (int j = 0; j + i < n; j++) {
        int k = i + j;

        if (s[j] == s[k] && dp[j][k] < dp[j + 1][k - 1] + 2) {
          dp[j][k] = dp[j + 1][k - 1] + 2;
          preX[j][k] = k - 1;
          preY[j][k] = j + 1;
        }

        if (dp[j][k] < dp[j + 1][k]) {
          dp[j][k] = dp[j + 1][k];
          preX[j][k] = k;
          preY[j][k] = j + 1;
        }
        if (dp[j][k] < dp[j][k - 1]) {
          dp[j][k] = dp[j][k - 1];
          preX[j][k] = k - 1;
          preY[j][k] = j;
        }
      }
    }

    string ans = "";
    int x = n - 1;
    int y = 0;

    while (y < x) {
      int nx = preX[y][x];
      int ny = preY[y][x];

      if (x - 1 == nx && y + 1 == ny) {
        ans += s[y];
      }

      x = nx;
      y = ny;
    }

    string rev = ans;
    reverse(rev.begin(), rev.end());

    if (dp[0][n - 1] % 2 == 0) {
      ans += rev;
    } else {
      ans += s[x];
      ans += rev;
    }
    cout << ans << endl;
  }
}
Problem p01160 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

short X[3001][3001];
short Y[3001][3001];

string lcs(string a, string b) {
  int n = a.size();
  int m = b.size();
  for (int i = 0; i < n + 1; i++) {
    for (int j = 0; j < m + 1; j++) {
      X[i][j] = 0;
      Y[i][j] = 0;
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i] == b[j]) {
        X[i + 1][j + 1] = X[i][j] + 1;
        Y[i + 1][j + 1] = 0;
      } else if (X[i + 1][j] < X[i][j + 1]) {
        X[i + 1][j + 1] = X[i][j + 1];
        Y[i + 1][j + 1] = +1;
      } else {
        X[i + 1][j + 1] = X[i + 1][j];
        Y[i + 1][j + 1] = -1;
      }
    }
  }

  string answer;
  for (int i = n, j = m; i > 0 && j > 0;) {
    if (Y[i][j] > 0) {
      i--;
    } else if (Y[i][j] < 0) {
      j--;
    } else {
      answer += a[i - 1];
      i--;
      j--;
    }
  }
  reverse(answer.begin(), answer.end());
  return answer;
}

int main() {
  string input;
  while (cin >> input) {
    string a = input;
    string b = input;
    reverse(b.begin(), b.end());
    string answer = lcs(a, b);
    cout << answer << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

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

int X[3001][3001];
int Y[3001][3001];

string lcs(string a, string b) {
  int n = a.size();
  int m = b.size();
  for (int i = 0; i < n + 1; i++) {
    for (int j = 0; j < m + 1; j++) {
      X[i][j] = 0;
      Y[i][j] = 0;
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i] == b[j]) {
        X[i + 1][j + 1] = X[i][j] + 1;
        Y[i + 1][j + 1] = 0;
      } else if (X[i + 1][j] < X[i][j + 1]) {
        X[i + 1][j + 1] = X[i][j + 1];
        Y[i + 1][j + 1] = +1;
      } else {
        X[i + 1][j + 1] = X[i + 1][j];
        Y[i + 1][j + 1] = -1;
      }
    }
  }

  string answer;
  for (int i = n, j = m; i > 0 && j > 0;) {
    if (Y[i][j] > 0) {
      i--;
    } else if (Y[i][j] < 0) {
      j--;
    } else {
      answer += a[i - 1];
      i--;
      j--;
    }
  }
  reverse(answer.begin(), answer.end());
  return answer;
}

int main() {
  string input;
  while (cin >> input) {
    string a = input;
    string b = input;
    reverse(b.begin(), b.end());
    string answer = lcs(a, b);
    cout << answer << endl;
  }
  return 0;
}
Problem p01160 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
using namespace std;

int dp[2001][2001];
int used[2001][2001];

string s;
int f(int l, int r) {
  if (dp[l][r] != -1)
    return dp[l][r];
  if (l >= r)
    return 0;
  if (s[l] == s[r]) {
    used[l][r] = 0;
    return dp[l][r] = f(l + 1, r - 1);
  } else {
    int a = f(l + 1, r) + 1;
    int b = f(l, r - 1) + 1;
    if (a < b) {
      used[l][r] = -1;
      return dp[l][r] = a;
    } else {
      used[l][r] = 1;
      return dp[l][r] = b;
    }
  }
}
int mask[2001];

void get(int l, int r) {
  if (l >= r)
    return;
  if (used[l][r] == 0) {
    get(l + 1, r - 1);
  } else if (used[l][r] == -1) {
    mask[l] = 1;
    get(l + 1, r);
  } else {
    mask[r] = 1;
    get(l, r - 1);
  }
}

int main() {
  while (cin >> s) {
    memset(dp, -1, sizeof(dp));
    memset(mask, 0, sizeof(mask));
    f(0, s.size() - 1);
    get(0, s.size() - 1);

    for (int i = 0; i < s.size(); i++)
      if (mask[i] == 0)
        cout << s[i];
    cout << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
using namespace std;

class NODE {
public:
  string text;
  int sPos;
  int rPos;
};
bool operator<(const NODE &a, const NODE &b) {
  pair<string, pair<int, int>> A = make_pair(a.text, make_pair(a.sPos, a.rPos));
  pair<string, pair<int, int>> B = make_pair(b.text, make_pair(b.sPos, b.rPos));
  return A > B;
}
int sfind(const string &s, int a, char c) {
  for (int i = a; i < s.length(); i++) {
    if (s[i] == c)
      return i;
  }
  return -1;
}
int main() {
  string src, rev, input;

  while (cin >> input) {
    src = input;

    rev = src;
    reverse(rev.begin(), rev.end());
    src = src.substr(0, src.length() / 2);
    rev = rev.substr(0, rev.length() / 2);

    cout << src << " " << rev << endl;
    queue<NODE> Q;
    NODE t = {"", 0, 0};
    Q.push(t);
    string ret = "";
    set<NODE> done;
    while (Q.size()) {
      NODE q = Q.front();
      Q.pop();
      // cout << q.text << endl;
      // cout << q.sPos << " " << q.rPos << endl;
      if (q.text.length() > ret.length()) {
        ret = q.text;
      }
      for (int i = q.sPos; i < src.length(); i++) {
        if (sfind(rev, q.rPos, src[i]) != -1) {
          NODE next;
          next.text = q.text + src[i];
          next.sPos = i + 1;
          next.rPos = sfind(rev, i, src[i]) + 1;
          if (done.find(next) == done.end())
            Q.push(next);

          done.insert(next);
        }
      }
    }
    string revRet = ret;
    reverse(revRet.begin(), revRet.end());
    cout << ret;
    if (input.length() % 2)
      cout << input[input.length() / 2];
    cout << revRet << endl;
  }
}
Problem p01163 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define mk make_pair
int s[30001], ss[30001], c = 2;
unordered_set<long long> se;
int main() {
  s[0] = 2;
  s[1] = 3;
  ss[2] = ss[3] = 1;
  for (int i = 2; i < 30001; i++) {
    int p = 0;
    for (int j = 2; j * j <= i; j++)
      if (i % j == 0)
        p++;
    if (!p)
      ss[i]++, s[c++] = i;
  }
  int n;
  while (cin >> n, n) {
    se.clear();
    int sum = 0;
    for (int i = 0; i < c; i++) {
      if (s[i] > n / 2 + 1)
        break;
      for (int j = 0; j < c; j++) {
        if (s[j] + s[i] > n)
          break;
        if (n - s[j] - s[i] > 0)
          if (ss[n - s[j] - s[i]]) {
            int d[3];
            d[0] = s[i];
            d[1] = s[j];
            d[2] = n - s[j] - s[i];
            sort(d, d + 3);
            if (!se.count(d[0] * 10000000000 + d[1] * 100000 + d[2]) &&
                d[0] + d[1] > d[2]) {
              se.insert(d[0] * 10000000000 + d[1] * 100000 + d[2]);
              sum++;
            }
          }
      }
    }
    cout << sum << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define mk make_pair
int s[30001], ss[30001], c = 2;
vector<int> a;
unordered_set<long long> se;
int main() {
  s[0] = 2;
  s[1] = 3;
  ss[2] = ss[3] = 1;
  for (int i = 2; i < 30001; i++) {
    int p = 0;
    for (int j = 2; j * j <= i; j++)
      if (i % j == 0)
        p++;
    if (!p)
      ss[i]++, s[c++] = i;
  }
  for (int i = 0; i < c; i++)
    for (int j = 0; j < c; j++)
      a.push_back(s[i] + s[j]);
  int n;
  while (cin >> n, n) {
    se.clear();
    int sum = 0;
    for (int i = 0; i < c; i++) {
      if (s[i] > n / 2 + 1)
        break;
      for (int j = 0; j < c; j++) {
        if (s[j] + s[i] > n)
          break;
        if (n - s[j] - s[i] > 0)
          if (ss[n - s[j] - s[i]]) {
            int d[3];
            d[0] = s[i];
            d[1] = s[j];
            d[2] = n - s[j] - s[i];
            sort(d, d + 3);
            if (!se.count(d[0] * 10000000000 + d[1] * 100000 + d[2]) &&
                d[0] + d[1] > d[2]) {
              se.insert(d[0] * 10000000000 + d[1] * 100000 + d[2]);
              sum++;
            }
          }
      }
    }
    cout << sum << endl;
  }
}
Problem p01163 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int MAX = 30001;
bool isPrime[MAX];
int primes[3250];
int primesSize;

void calcPrimes() {
  fill_n(isPrime, MAX, false);
  isPrime[2] = true;
  primes[0] = 2;
  primesSize = 1;
  for (int i = 3; i < MAX; i += 2) {
    bool add = true;
    for (int j = 0; j < primesSize; j++) {
      if (primes[j] * primes[j] > i) {
        break;
      }
      if (i % primes[j] == 0) {
        add = false;
        break;
      }
    }
    if (add) {
      isPrime[i] = true;
      primes[primesSize++] = i;
    }
  }
}

bool isTriangle(int a, int b, int c) {
  return (abs(a - b) < c) && (c < (a + b));
}

int main() {
  calcPrimes();
  while (true) {
    int t;
    cin >> t;
    if (t == 0) {
      break;
    }
    int res = 0;
    for (int i = 0; i < primesSize; i++) {
      if (primes[i] * 3 > t) {
        break;
      }
      for (int j = i; j < primesSize; j++) {
        if (primes[i] + primes[j] * 2 > t) {
          break;
        }
        int k = t - primes[i] - primes[j];
        if (isTriangle(primes[i], primes[j], k) && isPrime[k]) {
          res++;
        }
      }
    }
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

const int MAX = 30000;
int primes[3250];
int primesSize;
int sides[3];

void calcPrimes() {
  primes[0] = 2;
  primesSize = 1;
  for (int i = 3; i <= MAX; i += 2) {
    bool add = true;
    for (int j = 0; j < primesSize; j++) {
      if (primes[j] * primes[j] > i) {
        break;
      }
      if (i % primes[j] == 0) {
        add = false;
        break;
      }
    }
    if (add) {
      primes[primesSize++] = i;
    }
  }
}

bool isTriangle(int a, int b, int c) {
  return (abs(b - c) < a) && (a < (b + c));
}

int solve(int t, int k, int index) {
  if (t == 0 && k == 3) {
    return (int)isTriangle(sides[0], sides[1], sides[2]);
  } else if (t == 0 || k == 3) {
    return 0;
  }
  int res = 0;
  for (int i = index; i < primesSize; i++) {
    if (t < primes[i]) {
      break;
    }
    sides[k] = primes[i];
    res += solve(t - primes[i], k + 1, i);
  }
  return res;
}

int main() {
  calcPrimes();
  while (true) {
    int t;
    cin >> t;
    if (t == 0) {
      break;
    }
    cout << solve(t, 0, 0) << endl;
  }
}
Problem p01163 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cmath>
#include <iostream>
#include <map>
#include <vector>
#define MAX 30000
using namespace std;
vector<int> prime;
bool isprime[30001];
typedef pair<int, int> P;
typedef pair<P, int> Pi;

void sieve() {
  int n = MAX;
  int p = 0;
  for (int i = 0; i <= n; i++)
    isprime[i] = true;

  isprime[0] = isprime[1] = false;

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

bool isTriangle(int a, int b, int c) {
  if (a >= b + c || b >= a + c || c >= a + b)
    return false;
  return true;
}

int main() {
  sieve();
  int T;
  while (cin >> T, T) {
    int cnt = 0;
    for (int i = 0; i < prime.size() && prime[i] < T; i++)
      for (int j = i; j < prime.size() && prime[i] + prime[j] < T; j++)
        if (T - (prime[i] + prime[j]) >= prime[j] &&
            isprime[T - (prime[i] + prime[j])] &&
            isTriangle(prime[i], prime[j], T - (prime[i] + prime[j])))
          cnt++;

    cout << cnt << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <map>
#include <vector>
#define MAX 30000
using namespace std;
vector<int> prime;
bool isprime[30001];
typedef pair<int, int> P;
typedef pair<P, int> Pi;

void sieve() {
  int n = MAX;
  int p = 0;
  for (int i = 0; i <= n; i++)
    isprime[i] = true;

  isprime[0] = isprime[1] = false;

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

bool isTriangle(int a, int b, int c) {
  if (a >= b + c || b >= a + c || c >= a + b)
    return false;
  return true;
}

int main() {
  sieve();
  int T;
  while (cin >> T, T) {
    map<Pi, bool> used;
    int cnt = 0;
    for (int i = 0; i < prime.size() && prime[i] < T; i++)
      for (int j = 0; j < prime.size() && prime[i] + prime[j] < T; j++)
        if (!used[Pi(P(prime[i], prime[j]), T - (prime[i] + prime[j]))] &&
            T - (prime[i] + prime[j]) > 0 &&
            isprime[T - (prime[i] + prime[j])] &&
            isTriangle(prime[i], prime[j], T - (prime[i] + prime[j]))) {
          int a, b, c;
          a = prime[j], b = prime[i], c = T - (prime[j] + prime[i]);
          used[Pi(P(a, b), c)] = used[Pi(P(a, c), b)] = used[Pi(P(b, a), c)] =
              used[Pi(P(b, c), a)] = used[Pi(P(c, a), b)] =
                  used[Pi(P(c, b), a)] = true;
          cnt++;
        }
    cout << cnt << endl;
  }
  return 0;
}
Problem p01163 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <string.h>

char so[30001];

int main() {
  int t, i, j, c, b;

  memset(so, 0, sizeof(so));

  for (i = 2; i * i <= 30000; i++) {
    for (j = 2 * i; j <= 30000; j += i) {
      so[j] = 1;
    }
  }
  while (scanf("%d", &t)) {
    if (t == 0)
      break;
    c = 0;
    for (i = 2; i <= t / 3 + 1; i++) {
      if (t % 2 == 0 && c == 1)
        break;
      if (i != 2 && i % 2 == 0)
        ++i;
      if (i != 3 && i % 3 == 0)
        ++i;
      if (i != 5 && i % 5 == 0)
        ++i;
      if (i != 7 && i % 7 == 0)
        ++i;
      for (j = (i > t / 2 - i - 1 ? i : t / 2 - i - 1); j <= t / 2 + 1; j++) {
        if (t < i + j + j)
          break;
        /*				if(j!=2 && j%2==0)continue;
                                        if(j!=3 && j%3==0)continue;
                                        if(t-i-j!=2 && (t-i-j)%2==0)continue;
                                        if(t-i-j!=3 && (t-i-j)%3==0)continue;
        */
        if (2 * (i + j) <= t)
          continue;
        if (so[i] == 0 && so[j] == 0 && so[t - i - j] == 0)
          ++c;
      }
    }
    //		if(t%100==0){
    printf("%d\\n", c);
    //		}
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <string.h>

char so[30001];

int main() {
  int t, i, j, c;

  memset(so, 0, sizeof(so));

  for (i = 2; i * i <= 30000; i++) {
    for (j = 2 * i; j <= 30000; j += i) {
      so[j] = 1;
    }
  }

  while (scanf("%d", &t)) {
    if (t == 0)
      break;
    c = 0;
    for (i = 2; i <= t / 3 + 1; i++) {
      for (j = i; j <= t / 2 + 1; j++) {
        if (t < i + j + j)
          break;
        if (2 * (i + j) <= t)
          continue;
        if (so[i] == 0 && so[j] == 0 && so[t - i - j] == 0)
          ++c;
      }
    }

    printf("%d\\n", c);
  }
  return 0;
}