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


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }
typedef pair<int, int> P;
typedef long long ll;
typedef unsigned long long ull;

#define For(i, a, b) for (int(i) = (a); i < (b); (i)++)
#define rep(i, n) For(i, 0, n)
#define clr(a) memset((a), 0, sizeof(a))
#define mclr(a) memset((a), -1, sizeof(a))
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define sz(a) (sizeof(a))
#define Fill(a, v) fill((int *)a, (int *)(a + (sz(a) / sz(*(a)))), v)

bool cheak(int x, int y, int xMax, int yMax) {
  return x >= 0 && y >= 0 && xMax > x && yMax > y;
}
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
const int mod = 1000000007;
const int INF = 1e9;

const int M = 500000;
bool es[M];

int main() {
  For(i, 2, M) es[i] = 1;
  For(i, 2, M) {
    if (es[i]) {
      for (int j = i * 2; j < M; j += i)
        es[j] = 0;
    }
  }

  int n, m;
  while (cin >> n >> m && n != -1 && m != -1) {

    vector<int> v;
    int i = n;
    while (v.size() < m) {
      i++;
      if (es[i])
        v.push_back(i);
    }

    vector<int> ans;
    rep(j, v.size()) For(k, j, v.size()) { ans.push_back(v[j] + v[k]); }
    sort(all(ans));
    cout << ans[m - 1] << endl;
  }

  return 0;
}

Error: -11

Fixed submission:


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }
typedef pair<int, int> P;
typedef long long ll;
typedef unsigned long long ull;

#define For(i, a, b) for (int(i) = (a); i < (b); (i)++)
#define rep(i, n) For(i, 0, n)
#define clr(a) memset((a), 0, sizeof(a))
#define mclr(a) memset((a), -1, sizeof(a))
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define sz(a) (sizeof(a))
#define Fill(a, v) fill((int *)a, (int *)(a + (sz(a) / sz(*(a)))), v)

bool cheak(int x, int y, int xMax, int yMax) {
  return x >= 0 && y >= 0 && xMax > x && yMax > y;
}
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
const int mod = 1000000007;
const int INF = 1e9;

const int M = 200000;
bool es[M];

int main() {
  For(i, 2, M) es[i] = 1;
  For(i, 2, M) {
    if (es[i]) {
      for (int j = i * 2; j < M; j += i)
        es[j] = 0;
    }
  }

  int n, m;
  while (cin >> n >> m) {

    vector<int> v;
    int i = n;
    while (v.size() < m) {
      i++;
      if (es[i])
        v.push_back(i);
    }

    vector<int> ans;
    rep(j, v.size()) For(k, j, v.size()) { ans.push_back(v[j] + v[k]); }
    sort(all(ans));
    cout << ans[m - 1] << endl;
  }

  return 0;
}