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

质因子打表

时间:2018-04-30 16:41:36      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:isp   can   net   pair   and   void   als   efi   打表   

出处: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;
}

 

 

质因子打表

标签:isp   can   net   pair   and   void   als   efi   打表   

原文地址:https://www.cnblogs.com/6262369sss/p/8973771.html

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