D - Soldier and Number Game CodeForces - 546D(素因子+前缀和+读题转化)

因为cin,T了几发没找出问题,数据打的话一定开scanf啊
D. Soldier and Number Game
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
inputCopy
2
3 1
6 3
outputCopy
2
5
题意:求游戏轮数,即求素因子的个数。
思路:题目中出现了恶心的阶乘,直接求够呛的咳咳,那么就需要对题意进行转换。怎么转换呢?,事实上阶乘中间是可以抵消的,最后只剩下一组,如5!/3!,约完后变成了5x4,那么假设15的素因子个数为num[5],13的位num[3],那么5和4的素因子之和为num[5] - num[3],这不就是前缀和了吗。理解的话就好办了,就直接素数筛了。注意其中的T很大,第三组数据就有1e5了,因此对答案打表,求前缀和差即可。
注意:数据有点大,别开cin。


//弄懂的话此题其实就是素因子+前缀和问题,还是相对比较简单,顺便复习素数筛
//T掉了??
//这。。。原来是开了cin,一看CF数据,一下给了个一百万的数据,毫无悬念的T了。。。
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

const int maxn = 5000000;

int prime[maxn];//存素数
int is_prime[maxn];//判断是不是素数
int num[maxn];

void get_prime()
{
    memset(num,0,sizeof(num));
    memset(is_prime,0,sizeof(is_prime));//全部标记为素数,假设0为素数
    int cnt = 0;
    is_prime[2] = 0;//标记为素数
    for(int i = 2;i <= maxn;i++)
    {
        if(is_prime[i] == 0)//如果是素数,那么就可以筛了
        {
            prime[cnt++] = i;//获得素数,但在本题中好像并不必要
            for(int j = i ;j <= maxn;j += i)
            {
                is_prime[j] = 1;
                int temp = j;//寻找素因子的个数
                while(temp % i == 0)
                {
                    num[j]++;//多一个素数因子
                    temp /= i;
                }
            }
        }
    }
    for(int i = 2;i <= maxn;i++)
    {
        num[i] += num[i - 1];
    }
}

int main()
{
    get_prime();
    int T;
//    cin>>T;
    scanf("%d",&T);
    while(T--)
    {
        int a,b;
//        cin>>a>>b;
        scanf("%d%d",&a,&b);
        printf("%d\n",num[a] - num[b]);//前缀和之差为答案
    }
    return 0;
}

posted on 2019-03-26 16:05  tomjobs  阅读(144)  评论(0编辑  收藏  举报