首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【CodeForces】546D - Soldier and Number Game(线性筛法)

【CodeForces】546D - Soldier and Number Game(线性筛法)

作者头像
FishWang
发布2025-08-26 20:40:41
发布2025-08-26 20:40:41
10500
代码可运行
举报
运行总次数:0
代码可运行

点击打开题目

D. Soldier and Number Game

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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?

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

代码语言:javascript
代码运行次数:0
运行
复制
2
3 1
6 3

output

代码语言:javascript
代码运行次数:0
运行
复制
2
5

意思就是求a!/ b!有多少质因子。但是当时看题意真的是不懂。

看懂题后用一个线性筛把每个数的质因数的个数求出来即可。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define LL long long
int num[5000011];
void getSum()
{
	for (int i = 2 ; i <= 5000000 ; i++)
	{
		if (!num[i])
		{
			for (int j = i ; j <= 5000000 ; j += i)
			{
				int t = j;
				while (t % i == 0)
				{
					num[j]++;
					t /= i;
				}
			}
		}
	}
}
int main()
{
	int n;
	getSum();
	for (int i = 2 ; i <= 5000000 ; i++)
		num[i] += num[i-1];
	scanf ("%d",&n);
	int l, r;
	while (n--)
	{
		scanf ("%d%d",&r,&l);
		printf ("%d\n",num[r]-num[l]);
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档