Ques Link: https://hack.codingblocks.com/contests/c/547/760
Code Link: https://ide.codingblocks.com/s/43745
Any Optimisations suggested for this code, its getting TLE in last 2 cases
Instead of calling prime seive function again and again for each test case ,compute it once and store the values in a vector and use it for the segmented seive.
Doing that also gives TLE idk why
for(long long i = 2; i*i<=b;i++){ // going form 2 to root(b)
for(long long j = a; j<=b; j++){
if(p[i]){
if(j==i) // if overlapping is there ignore it
continue;
if(j%i==0){ // if number is not prime then set its value to false with shifting
// a ->0.........b->b-a
// shifted by a to left
prime[j-a]=0;
}
}
}
This part of your code is producing a TLE. Iterating from a to b will for each prime will lead to TLE.
Instead, find first divisor of p[i] that is >=a. Let this be j. Now mark j+p[i], j+2p[i], j+3p[i],… till this number is less than b.
I have used this in my approach, so try this part yourself or take help from my code.