码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces546D:Soldier and Number Game(求质因子个数)

时间:2015-06-18 17:20:26      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:codeforces

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,n开始是a!/b!,每次用一个x去整除n得到新的n,最后当n变成1的时候经过了几轮得分就是这个轮数,要求最大的分数是多少

思路:
很明显,就是一个求整数质因子个数的题目,阶乘我们不需要算,我们知道在a>b的时候,b!都约掉了,那么我们只需压迫计算出每个数的质因数有几个,然后计算出1~n的质因子之和,那么就可以迅速得到答案了

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <list>
#include <algorithm>
#include <climits>
using namespace std;

#define lson 2*i
#define rson 2*i+1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 5000005
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)
const int mod = 1e9+7;
int p[N];//每个数有多少个素数因子
bool v[N]; //是否为素数
int a[N];//每个数最小的素数因子
int prime[N/10];//素数表
LL sum[N];
void init()
{
    for(int i=2; i<N; ++i)
        a[i] = i;
    int num=-1;
    for(int i=2; i<N; ++i)
    {
        if(!v[i]) prime[++num] = i;
        for(int j=0; j<=num && i*prime[j] < N; ++j)
        {
            int t = i*prime[j];
            v[t] =1;
            if(a[t] > prime[j]) a[t] = prime[j];
            if(i%prime[j] == 0) break;
        }
    }
    p[2] = 1;
    for(int i=3; i <N; ++i)
        p[i] = p[i/a[i]] + 1;
}
int main()
{
    int i,j,k;
    init();
    sum[1] = 0;
    for(i = 2; i<=5000000; i++)
    {
        sum[i] = sum[i-1]+p[i];
    }
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&i,&j);
        if(i == j)
            printf("0\n");
        else
            printf("%I64d\n",sum[i]-sum[j]);
    }

    return 0;
}


Codeforces546D:Soldier and Number Game(求质因子个数)

标签:codeforces

原文地址:http://blog.csdn.net/libin56842/article/details/46549231

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!