Why Runtime Error?

I am solving a problem at codeforces 17A - Noldbach problem (http://codeforces.com/problemset/problem/17/A)

I've written the following code for this. But I got RUNTIME_ERROR at test 12
for the Input 60 8.

Test: #12, time: 0 ms., memory: 0 KB, exit code: -1073741819, checker exit code: 0, verdict: RUNTIME_ERROR

But when I give input 60 8 in my pc it works well.

Why this? Where is the fault?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <vector>
#define M 1000

using namespace std;

bool marked[M];   //Globally declared bool array is initialized with false

vector <int> v;

void sieve (int n)
{
    for (int i=2; i<=n; i++)
    {
        if (marked[i] == false)
        { // i is a prime
            v.push_back(i);
            for (int j = i+i; j<=n; j+=i)
                marked[j] = true;
        }
    }
}

bool isPrime (int n)
{
    if (n < 2) return false;
    if (n == 2) return true;
    if (n % 2 == 0) return false;
        return marked[n] == false;
}

int main()
{
        int n,k,c=0;
         cin>>n>>k;
         sieve(n);
         for(int i=0; i<v.size(); i++)
         {
             int p=v[i]+v[i+1]+1;

             if(isPrime(p)==true && p<=n)
                c++;
         }

         if(c>=k)
            cout<<"YES"<<endl;
         else
            cout<<"NO"<<endl;


     return 0;
}
int p=v[i]+v[i+1]+1; when i = v.size() - 1, you will try to access v[v.size()] which is out of bounds and leads to undefined behavior.
so what should be the line? i = v.size() - 1 or i = v.size() -2
You loop condition is wrong: tou are looping one more time than needed.
Topic archived. No new replies allowed.