您的位置:首页 > 其它

Codeforces Round #304 (Div. 2) D. Soldier and Number Game 数学 质因数个数

2015-05-23 03:37 531 查看

D. Soldier and Number Game

Time Limit: 20 Sec Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/546/problem/D

Description

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.

[b]Output[/b]

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

Sample Input

2
3 1
6 3

Sample Output

2
5

HINT

[b]题意 [/b]

给你 a,b,求a到b的数的质因数个数和

[b]题解:[/b]

线性筛法,类似与dp的一种筛法

[b]代码:[/b]

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 5000001
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
/*

inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
*/
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
//**************************************************************************************

int cnt[maxn];
long long sum[maxn];

void init()
{
for(int i=2;i<maxn;i++)
if(!cnt[i])
for(int j=i,c=1;j<maxn;j+=i,c++)
{
int t=c;
while(1)
{
cnt[j]++;
if (t%i) break;
t/=i;
}
}
for(int i=2;i<=maxn;i++)
sum[i]=sum[i-1]+cnt[i];
}
int main()
{
init();
int t,a,b;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&a,&b);
printf("%d\n",sum[a]-sum[b]);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: