SIGFPE eroor in NFACTOR of spoj

The link to my code is
Please help me to find out why I am getting SIGFPE error in this code on spoj.
Thanks in Advance.

SIGFPE is a divide by zero error.Just store something in a[1] 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

The above comments explains you about SIGFEPE. I would like to add some extra point about your code.

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;
    	p[a[i]*j]++;   //p is initialized with 0


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

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

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



  4. Apply lower_bound and upper_bound and give the answer.

    vector::iterator l,u;


    l = lower_bound(q[n].begin(),q[n].end(),a);
    u = upper_bound(q[n].begin(),q[n].end(),b);


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 [10][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