Test case 0 and 1 failing

#include
#include
#include
using namespace std;

#define ll long long int
bool arr[100004];
vectorprime;

int main() {
ll num;
num = 100001;
arr[2] = true;
prime.push_back(2);
for (ll i = 3; i < num; i = i + 2)
{ arr[i] = true; }
for (ll i = 3; i < num; i = i + 2)
{
if (arr[i]) {
prime.push_back(i);
for (ll j = i * i; j < num; j = j + i)
arr[j] = false;
}
}
int size, q;
cin>>size>>q;
list v;
for(int i=0; i<size; i++)
{
int val;
cin>>val;
v.push_back(val);
}
list A(v), B;
for(int j=0; j<q; j++)
{
size = A.size();
for(int i=0; i<size; i++)
{
int val = A.front();
A.pop_front();
if(val%prime[j]==0)
{
B.push_back(val);
}
else
{
A.push_back(val);
}
}
for(auto x: B){cout<<x<<endl;}
B.clear();
}
for(auto x: A){cout<<x<<endl;}
return 0;
}

@varun.saxena
it is giving WA because the list you are using, not able to maintain the required stack type of order for printing, push and pop.

Also take a larger limit for num around 10^7 for safety, since here is n is till 10^5 and it is possible that it q is also large and it will need more large range than 10^5 for finding the required primes.

Ok right. understood that it needed a stack based print. Got it. Thanks.

1 Like