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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define vci vector<int>
#define vcs vector<string>
#define pb push_back
#define sz size()
#define mapii map<int, int>
#define mapci map<char, int>
#define mapsi map<string, int>
#define all(x) x.begin(), x.end()
#define minit(a, i) memset(a, i, sizeof(a));

#define for_(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define for_d(i, a, b) for (int i = (int)a - 1; i >= b; i--)
#define for_r(i, a, b, c) for (int i = (int)a; i < (int)b; i += c)
#define for_dr(i, a, b, c) for (int i = (int)a - 1; i >= b; i -= c)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

typedef long long ll;
typedef double D;

const int iINF = 2147483647;
const ll lINF = 9223372036854775807;

template <class T> inline void dbg(T t) { cout << t << endl; }

vector<int> n_prime(int n) {
  vector<int> ret;
  vector<bool> is_prime(n + 1, 1);
  for (int i = 2; i <= n; i++) {
    if (is_prime[i]) {
      ret.push_back(i);
      for (int j = i + i; j <= n; j += i)
        is_prime[j] = 0;
    }
  }
  return ret;
}

vci primes;
vci cnt;
int MAX = 50000;

void solve() {
  cnt.assign(MAX + 10, 0);

  int size = (int)primes.sz;
  for_(i, 0, size) {
    for_(j, i, size) {
      int t = primes[i] + primes[j];
      if (t > MAX)
        break;
      cnt[t]++;
    }
  }
}

int main() {
  int n;
  primes = n_prime(MAX);
  solve();

  while (1) {
    cin >> n;
    if (n == 0)
      break;
    cout << cnt[n] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define vci vector<int>
#define vcs vector<string>
#define pb push_back
#define sz size()
#define mapii map<int, int>
#define mapci map<char, int>
#define mapsi map<string, int>
#define all(x) x.begin(), x.end()
#define minit(a, i) memset(a, i, sizeof(a));

#define for_(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define for_d(i, a, b) for (int i = (int)a - 1; i >= b; i--)
#define for_r(i, a, b, c) for (int i = (int)a; i < (int)b; i += c)
#define for_dr(i, a, b, c) for (int i = (int)a - 1; i >= b; i -= c)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

typedef long long ll;
typedef double D;

const int iINF = 2147483647;
const ll lINF = 9223372036854775807;

template <class T> inline void dbg(T t) { cout << t << endl; }

vector<int> n_prime(int n) {
  vector<int> ret;
  vector<bool> is_prime(n + 1, 1);
  for (int i = 2; i <= n; i++) {
    if (is_prime[i]) {
      ret.push_back(i);
      for (int j = i + i; j <= n; j += i)
        is_prime[j] = 0;
    }
  }
  return ret;
}

vci primes;

int solve(int n) {
  int ans = 0, size = (int)primes.sz;
  for_(i, 0, size) {
    int t = n - primes[i];

    int lb = i, rb = size;
    while (rb - lb > 1) {
      int med = (lb + rb) / 2;

      if (t > primes[med])
        lb = med;
      if (t <= primes[med])
        rb = med;
    }

    if (t - primes[lb] == 0 || t - primes[rb] == 0)
      ans++;
  }
  return ans;
}

int main() {
  int n;
  primes = n_prime(50000);

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

  return 0;
}