While I was solving this question during virtual contest, this submission gave me Memory Limit Exceeded verdict, when I changed the code slightly (I just made "vector" ans a global vector) it was accepted, here is the accepted version. The point to observe here is that "vector ans" is of size 193 only, which is very small to cross the given memory limits. I couldn't figure out what is wrong with my code. Any help is appreciated.
The last element of your "primes" will be 997 and beyond this, it contains undefined values (technically, it is an undefined behaviour and one cannot even assume that it contains something). If the undefined value at the end of "primes" have some property (e.g. zero, one, or some negative values (overflow can complicates the thing)), "tmp" never exceeds "n" and it will loop forever. Thus it will push_back repeatedly and will eventually be MLE.
Oh yeah ! I found my mistake, Thanks for the help :)
I just changed the for loop structure to
And it worked fine !!