Codeforces546D:Soldier and Number Game

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.

Output

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

Sample Input

Input
2
3 1
6 3
Output
2
5

虽然做出来了,但用了一个多小时时间思考,过后想想其实思路很简单的,还是自己太渣,唉难过

这道题其实是还是用到了素数筛选的思想,注意打表就行了,不然会超时,详细见代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define N 5000100
#define INF 999999999
using namespace std;
int a[N],f[N];
void flag()
{
    int i,j;
    for(i=1; i<=N; i++)
        a[i]=1;
    int m=sqrt(N);
    for(i=2; i<=m; i++)//这里注意循环到根号N就行了;
        for(j=i*i; i<=N; j+=i)//按照倍数进行筛选,类似素数筛选的方法
        {
            if(j>N)
                break;
            a[j]=a[i]+a[j/i];//这里是关键
        }
    int sum=0;
    for(i=1;i<=N;i++)//注意这里将和打表,不要一个个的进行循环计算,会超时的;
    {
        sum+=a[i];
        f[i]=sum;
    }
}

int main()
{
    int m,n,t,ans;
    flag();
    scanf("%d",&t);
    while(t--)
    {
        ans=0;
        scanf("%d%d",&n,&m);//(n!/m!等于n*(n-1)*(n-2)*.....*m+1)
        ans=f[n]-f[m];//ans等于f[n]-f[m+1-1];
        printf("%d\n",ans);
    }
    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值