Codeforces Round #304 (Div. 2) D. Soldier and Number Game

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.

Sample test(s)
input
2
3 1
6 3
output
2
5

 

题目大意:

 

有两个士兵在玩一个游戏,游戏规则是这样的:第一个士兵选择一个数n,然后第二个士兵每次可以任选除了1并且整除n的数字x,并且每选一次数字,n就约掉一个x,直到n1则游戏结束,问最多能选几次x。并且n等于给定的a! / b!

 

大致思路:

 

一开始看到n等于a! / b! ,而且1 ≤ b ≤ a ≤ 5000000 ,立马认为是(对于我)不可做题。后来又看到那么多人过,才重新看了下题,发现其实并不需要算出n。题目其实就是求n的质因子个数有多少个。而n又等于a! / b!,推一下就可以知道,n的质因子个数等于a的质因子个数减掉b的质因子个数。然后问题就转换成求出1~5000000所有数的质因子个数,可以用埃式筛法来做,加一点处理就可以了。算法复杂度大约是O(n)

 

埃式筛法:

 

const int maxn = 5000000 + 10;
int prime[maxn];
bool is_prime[maxn];
void sieve()
{
	fill(is_prime, is_prime+maxn, 1);
	is_prime[0] = is_prime[1] = false;
	int ans = 0;
	for( int i=2; i<maxn; ++i )
	{
		if( is_prime[i] )
		{
			prime[ans++] = i;
			for( int j=i*2; j<maxn; j+=i )
			{
				is_prime[j] = false;
			}
		}
	}
	return ;
}


两种解法(其实原理是一样的):

ps:总体的时间复杂度并没有差多少,前面这种稍微快一点。

//CF_394_D.cpp
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
using namespace std;
typedef long long ll;
const int maxn = 5000000 + 10;
ll primeFactors[maxn];
int primeDiviser[maxn];
bool is_prime[maxn];
void sieve()
{
	fill(is_prime, is_prime+maxn, 1);
	is_prime[0] = is_prime[1] = 0;
	for( int i=2; i<maxn; i++ )
	{
		if( is_prime[i] )
		{
			primeDiviser[i] = i;
			for( int j=2*i; j<maxn; j+=i )
			{
                primeDiviser[j] = i;
				is_prime[j] = false;
			}
		}
	}
	for( int i=2; i<maxn; i++ )
		primeFactors[i] = primeFactors[i / primeDiviser[i]] + 1;
	for( int i=2; i<maxn; i++ )
		primeFactors[i] += primeFactors[i-1];
	return ;
}

int main()
{
	sieve();
	int T;
	scanf("%d", &T);
	int a, b;
	while( T-- )
	{
		scanf("%d %d", &a, &b);
		printf("%I64d\n", primeFactors[a]-primeFactors[b]);
	}

	return 0;
}


//CF_394_D.cpp
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
using namespace std;
typedef long long ll;
const int maxn = 5000000 + 10;
ll s[maxn], k[maxn];
bool is_prime[maxn];
void sieve()
{
	fill(is_prime, is_prime+maxn, 1);
	memset(k, 0, sizeof(k));
	is_prime[0] = is_prime[1] = 0;
	for( int i=2; i<maxn; i++ )
	{
		if( is_prime[i] )
		{
			k[i]++;
			for( int j=2*i; j<maxn; j+=i )
			{
				int cnt = 0, ans = j;
				while( !(ans%i) )
                {
                    ans /= i;
                    k[j]++;
                }
				is_prime[j] = false;
			}
		}
	}
	return ;
}
void solve()
{
	sieve();
	for( int i=1; i<=maxn; i++ )
	{
		s[i] = k[i] + s[i-1];
	}
}
int main()
{
	solve();
	int T;
	scanf("%d", &T);
	int a, b;
	while( T-- )
	{
		scanf("%d %d", &a, &b);
		printf("%I64d\n", s[a]-s[b]);
	}

	return 0;
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下 4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值