PTUPLES - Editorial

As I see, both of your approaches are not efficient. In your first solution, you are dynamically checking if the number is prime. The solution is fairly inefficient. In your 2nd solution, you did sieve for every test case. You are supposed to pre-Compute, and store them globally or in a static variable, only once. Then access the required thing in O(1) for every test case.

You can get complete information about memory limits here

1 Like

Because your stack cant take this big array. Just decrease the array size to 10^2 something. Your code will run on local machine. As different machine got a different stack size. Or you can just declare your globally. That will store your array in heap.

what wrong with my code it is printing correct output upto 5
after onwards it is printing output as 1

#include<bits/stdc++.h>
using namespace std;
vector<bool> prime(1000005,true),add(1000005);
void prim()
{
    prime[0]=prime[1]=false;
    for (int i = 2; i*i <=1000005; i++) {
        if(prime[i]and prime[i-2])
        {
        add[i]=1;
        add[i]= add[i]+add[i-1];
        }
        else
        add[i]=add[i-1];
        if(prime[i])
        {
            for (int j = i*i;j<=1000005;j+=i)
            {
                prime[j]=false;
            }
        }
    }
}
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    prim();
    int t;cin>>t;
    while(t--)
    {
        int n;cin>>n;
        cout<<add[n]<<"\n";
    }
}