Soldier and Number Game CodeForces - 546D ()

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 nbecomes 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?

Input

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.

Output

For each game output a maximum score that the second soldier can get.

Examples
Input
2
3 1
6 3
Output
2
5

题意:n等于 a!/b! 的阶乘,n = n/n的因子,看看最多能除多少次到1;

思路:找 (a,b] 这之间的所有素因子个数之和;

重点:

一个数的所有素因子不一定 都小于等于根号n,如 14的素因子 2和7,7就大于根号n,所以找一个数的所有素因子时,只需枚举到 (n/2+1)就行了;
但是 判断一个数,是不是素数时,就到根号n就行了

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Max 5000000

int a[Max+10];      // a[i] 先存i,找到i的素因子时,就直接一直除i,一直到除到不能整除; 
int sum[Max+10];   // sum[i] 记录i的所有素因子的个数; 
bool is_prime[Max+10];  // 判断 是不是素数;
void fff()
{
	int i,j;
	for(i = 1;i<=Max;i++)
	{
		a[i] = i;
		sum[i] = 0;
	}
	
	memset(is_prime ,true,sizeof(is_prime));
	is_prime[0] = false;
	is_prime[1] = false;
	for(i = 2;i <= (Max+1)/2;i++)   // 一个数的所有素因子 是都小于等于 根号n; 
	{
		if(is_prime[i])
		{
			for(j = 1;j*i<=Max;j++)
			{
				if(j!=1) is_prime[j*i] = false;
				while(a[j*i]%i==0 && a[j*i]!=1)
				{
					a[j*i] /=i;
					sum[j*i]++;   
				}
			}
		}
		sum[i] += sum[i-1];
	}
	for(;i<=Max;i++)
	{
		if(is_prime[i])
			sum[i] = sum[i-1] + 1;
		else sum[i] += sum[i-1]; 
	}
} 
int main()
{
	int i,j,k,t;
	fff();
	scanf("%d",&t);
	int a,b;
	
	while(t--)
	{
		scanf("%d%d",&a,&b);
		printf("%d\n",sum[a]-sum[b]);
	}
	return 0;
} 


猜你喜欢

转载自blog.csdn.net/obsorb_knowledge/article/details/80191046
今日推荐