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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const int MAX = 1000000;
bool is_prime[MAX + 10];
int key(int n) {
  vector<int> p;
  for (int i = 2; i <= n; ++i)
    if (is_prime[i] && n % i == 0)
      p.push_back(i);
  return 2 * p.back() - accumulate(all(p), 0);
}
int main() {
  fill_n(is_prime, MAX, true);
  for (int i = 2; i <= MAX; ++i)
    if (is_prime[i])
      for (int j = 2 * i; j <= MAX; j += i)
        is_prime[j] = false;

  int a, b;
  while (scanf("%d%d", &a, &b), a | b)
    puts(key(a) > key(b) ? "a" : "b");
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const int MAX = 1000000;
bool is_prime[MAX + 10];
int key(int n) {
  vector<int> p;
  for (int i = 2; i < n; ++i)
    if (is_prime[i] && n % i == 0)
      p.push_back(i);
  int largest = p.back();
  int sum = accumulate(p.begin(), p.end() - 1, 0);
  int sub = largest - sum;
  if (sub >= 2 && is_prime[sub])
    return sub;
  else
    return 0;
}
int main() {
  fill_n(is_prime, MAX, true);
  for (int i = 2; i <= MAX; ++i)
    if (is_prime[i])
      for (int j = 2 * i; j <= MAX; j += i)
        is_prime[j] = false;

  int a, b;
  while (scanf("%d%d", &a, &b), a | b) {
    if (key(a) > key(b))
      puts("a");
    else
      puts("b");
  }
}