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
题目大意:
有两个士兵在玩一个游戏,游戏规则是这样的:第一个士兵选择一个数n,然后第二个士兵每次可以任选除了1并且整除n的数字x,并且每选一次数字,n就约掉一个x,直到n为1则游戏结束,问最多能选几次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;
}