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;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值