Two soldiers are playing a game. At the beginning first of them chooses a positive integer n and gives it to the second soldier. Then the second one tries to make maximum possible number of rounds. Each round consists of choosing a positive integer x > 1, such that n is divisible by x and replacing n with n / x. When n becomes equal to 1 and there is no more possible valid moves the game is over and the score of the second soldier is equal to the number of rounds he performed.
To make the game more interesting, first soldier chooses n of form a! / b! for some positive integer a and b (a ≥ b). Here by k! we denote the factorial of k that is defined as a product of all positive integers not large than k.
What is the maximum possible score of the second soldier?
First line of input consists of single integer t (1 ≤ t ≤ 1 000 000) denoting number of games soldiers play.
Then follow t lines, each contains pair of integers a and b (1 ≤ b ≤ a ≤ 5 000 000) defining the value of n for a game.
For each game output a maximum score that the second soldier can get.
2 3 1 6 3
2
5
这题用到了一个公式,primefactors[a] = primefactors[a / primediviser[a]] + 1,primefactor[a]表示a因数中素数的个数。
只要把a,b的primefactor[]算出来,减一下就行了。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 5000050
int c[maxn],prime[maxn];
void init()
{
memset(c,0,sizeof(c));
int i,j,t;
t=1;
for(i=2;i<=maxn;i++){
if(c[i]==0){
for(j=2;j*i<=maxn;j++){
c[j*i]=c[j]+1;
}
}
}
for(i=3;i<=maxn;i++){
c[i]+=c[i-1];
}
}
int main()
{
int a,b,i,j,T;
init();
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&a,&b);
printf("%d
",c[a]+a-c[b]-b);
}
return 0;
}