Problem p00780 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define FOR(I, F, N) for (int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout << V << endl
#define pb push_back
#define INF (1 << 28)

template <typename T> void remove(vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

map<int, bool> memo;
vector<int> era;
void Eratosthenes(int N) {
  int arr[N];
  for (int i = 0; i < N; i++) {
    arr[i] = 1;
  }
  for (int i = 2; i < sqrt(N); i++) {
    if (arr[i]) {
      for (int j = 0; i * (j + 2) < N; j++) {
        arr[i * (j + 2)] = 0;
      }
    }
  }

  for (int i = 2; i < N; i++) {
    if (arr[i]) {
      memo[i] = true;
      era.push_back(i);
    }
  }
}
int main(void) {
  Eratosthenes(1 << 16);
  int in;
  while (cin >> in, in) {
    int ans = 0;
    rep(k, era.size()) {
      int i = era[k];
      if (i > in)
        break;
      if (!memo[i])
        continue;
      rep(l, era.size()) {
        int j = era[l];
        if (j > in)
          break;
        if (i + j > in)
          break;
        if (i < j)
          break;
        if (memo[j] && i + j == in) {
          // cout << i << "+" << j << " = " << i+j << endl;
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

int StrToInt(string s) {
  stringstream ss;
  ss << s;
  int val;
  ss >> val;
  return val;
}
string IntToStr(int i) {
  stringstream ss;
  ss << i;
  return ss.str();
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define FOR(I, F, N) for (int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout << V << endl
#define pb push_back
#define INF (1 << 28)

template <typename T> void remove(vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

map<int, bool> memo;
void Eratosthenes(int N) {
  int arr[N];
  for (int i = 0; i < N; i++) {
    arr[i] = 1;
  }
  for (int i = 2; i < sqrt(N); i++) {
    if (arr[i]) {
      for (int j = 0; i * (j + 2) < N; j++) {
        arr[i * (j + 2)] = 0;
      }
    }
  }

  for (int i = 2; i < N; i++) {
    if (arr[i]) {
      memo[i] = true;
    }
  }
}
int main(void) {
  Eratosthenes(1 << 16);
  int in;
  while (cin >> in, in) {
    int ans = 0;
    rep(i, 1 << 16) {
      if (i > in)
        break;
      if (!memo[i])
        continue;
      rep(j, 1 << 16) {
        if (j > in)
          break;
        if (i + j > in)
          break;
        if (i < j)
          break;
        if (memo[j] && i + j == in) {
          // cout << i << "+" << j << " = " << i+j << endl;
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

int StrToInt(string s) {
  stringstream ss;
  ss << s;
  int val;
  ss >> val;
  return val;
}
string IntToStr(int i) {
  stringstream ss;
  ss << i;
  return ss.str();
}