A number is said to have n prime divisors if it can factored into n prime numbers (not necessarily distinct).
E.g.:  
12 = 2*2*3 ---> 12 has 3 prime divisors  
Given numbers a and b we have to find the number of such prime divisors of a!/b!(a>=b). Hence I decided to do the following way:  
long long pre[5000001];
long long solve(int num)
{
    if(!num)
        return 0;
    if(pre[num] || num==1)
        return pre[num];
    int num1=num;
    if(num1%2==0)
    {
        int cnt=0;
        while(num1%2==0)
        {
            num1/=2;
            cnt++;
        }
        pre[num] = solve(num-1) + (solve(num1)-solve(num1-1)) + cnt;
        return pre[num];
    }
    for(int i=3;i<=(int)(sqrt(num1))+1;++i)
    {
        if(num1%i==0)
        {
            int cnt=0;
            while(num1%i==0)
            {
                cnt++;
                num1/=i;
            }
            pre[num] = solve(num-1) + (solve(num1)-solve(num1-1)) + cnt;
            return pre[num];
        }
    }
    if(num>1)
    {
        pre[num]=1 + solve(num-1);
        return pre[num];
    }
}
int main()
{
    int t;
    cin>>t;
    pre[1]=0;
    while(t--)
    {
        int a,b;
        cin>>a>>b;
        long long ans = solve(a)-solve(b);
        cout<<ans<<endl;
    }
    return 0;
}
My approach was to basically to calculate the number of prime divisors and store them because the limit for a and b was <=5000000. pre[num] gives the sum of number of prime divisors of all numbers <=num. But it results in run-time error for larger inputs such as a=5000000 b=4999995. Is there any better way to do this or the present approach can be tweaked for a better solution?