质因子打表

Posted 6262369sss

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了质因子打表相关的知识,希望对你有一定的参考价值。

出处:CodeForces 546D (质因子打表法) - CSDN博客  https://blog.csdn.net/fenghoumilin/article/details/52203585

CodeForces 546D:https://vjudge.net/problem/173132/origin

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

Input
2
3 1
6 3
Output
2
5
输入t组数据,每组数据有a,b两个数,求a的阶乘除以b的阶乘(!a/!b)算出的结果最多可以除多少个数最终变成1.
#include<stdio.h>
#include<string.h>
#define maxn 5000050
int n,m,k,t;
int num[maxn];
bool isprime[maxn];
void init()
{
    memset(num,0,sizeof(num));
    memset(isprime,true,sizeof(isprime));
    isprime[1]=false;
    for(int i=2;i<=maxn;i++)
    {
        if(!isprime[i])
        continue;
        for(int j=i;j<=maxn;j+=i)
        {
            int temp=j;
            while(temp%i==0)
            {
                num[j]++;//求j可以被i的几次方整除,也就是有几个质数i 
                temp/=i; 
            }
            isprime[j]=false;
        }
    }
    for(int i=1;i<=maxn;i++)
    {
        num[i]+=num[i-1];//求前缀和 
     } 
}
int main()
{
    init();
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        printf("%d\n",num[n]-num[m]);
     } 
     return 0;
}

 

 

以上是关于质因子打表的主要内容,如果未能解决你的问题,请参考以下文章

求整数n的最小质因子出现次数 打表

质因子分解问题

Codeforces Round #599 (Div. 2) Tile Painting

Prime Distance POJ - 2689 线性筛

P2043 质因子分解

P2043 质因子分解