 # SIGFPE eroor in NFACTOR of spoj

The link to my code is http://ideone.com/CLOFdB
Please help me to find out why I am getting SIGFPE error in this code on spoj.

SIGFPE is a divide by zero error.Just store something in a and a[ number which are prime ] because whenever they will be accessed, then if you divide by a[i] it can go infinite. If a number is prime store a[i]=i and in 1 you can store 1 or you make sure these values are not used

Your approach will give a TLE. As you are calculating everything again and again during each test case. You can follow this to avoid TLE:

1. Calculate all the primes using Sieve. (which you did)

2. Store all the number of distinct prime factors of each element ranging from 2,1000000(=MAX).
m are the number of primes that you got using Sieve. a is the array in which they are stored.

*for(int i = 0;i<m;i++){

``````ll j = 1;

while(a[i]*j<=MAX){

p[a[i]*j]++;   //p is initialized with 0

j++;

}
``````

}*

3. Store in 2D vector the n factorful ,i.e., q[k].push_back(i) {if i is a k factorful}

q.push_back(1); //given in the question

for(int i = 2;i<=MAX;i++){

``````q[p[i]].push_back(i);
``````

}

4. Apply lower_bound and upper_bound and give the answer.

vector::iterator l,u;

while(t–){

``````        scanf("%d%d%d",&a,&b,&n);
l = lower_bound(q[n].begin(),q[n].end(),a);
u = upper_bound(q[n].begin(),q[n].end(),b);
printf("%d\n",u-l);
``````

}

Can you please explain me the code in C language?I dont know this concept of vector…Thanks

First two points will be same.
In 3rd point use 2D array (it will be of size [x] x can be MAX for worst case).
In 4th point apply simple iteration and count if(q[n][i] belongs to [a,b]).
Hope its clear now