**质因子个数

来源:互联网 发布:阿里云ecs绑定域名 编辑:程序博客网 时间:2024/06/05 14:16
C - C
Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 546D

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
23 16 3
Output
2

5

题意:

两个士兵在玩一个游戏,开始的时候第一个士兵选择一个数n,并把这个数交给第二个士兵,第二个士兵必须选择一个x满足x>1 且n能被x整除,然后将n变为n/x,然后把这个数交给第一个士兵,依次循环,当n等于1时,游戏结束,第二个士兵所得的分数为他执行的游戏轮数(选择x的次数)。

为了使游戏更有趣…,第一个士兵选择的n满足如下的形式 a!/b!(a>=b), a!表示a的阶乘。

第二个士兵得到的最大分数是多少?

题解:

给出一个n,n开始是a!/b!,每次用一个x去整除n得到新的n,
最后当n变成1的时候经过了几轮得分就是这个轮数,
要求最大的分数是多少
[solve]
就是一个求整数质因子个数的题目,
阶乘我们不需要算,我们知道在a>b的时候,
b!都约掉了,那么我们只需计算出每个数的质因数有几个,
然后前缀和计算出1~n的质因子之和,

告诉你a, b的值, 那么只需要求出b到a之间的数, 每个数有多少个因子就可以。

起初没看懂题,后来百度了一下题解,基本上都是这个思路。。。

#include<cstdio>using namespace std;#define max 5000005int a[max];int main(){int t,r,l,k,i,j;//while(scanf("%d",&t)!=EOF)//{scanf("%d",&t);    for(i=2;i<max;i++)    if(!a[i])//确保质因数的倍数已经遍历过,不再重复遍历      {        for(j=i;j<max;j+=i)        {            k=j;            while(k%i==0)           {                a[j]++;//jde质因数个数                k/=i;           }        }      }         for(i=2;i<max;i++)         a[i]+=a[i-1];//<=i的所有数的质因数个数 之和          while(t--)        {             scanf("%d%d",&r,&l);             printf("%d\n",a[r]-a[l]);        }     //}     return 0; }
刚开始根据题解做的。。。然而wa,应该是像8=2*2*2这样,2只记了一次
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int a,b;int c[1000000+11]={1,1};void f()//素数打表 {for(int i=2;i<=100000;i++){if(c[i]==1)continue;for(int j=2*i;j<=1000000;j+=i)c[j]=1;//非素数  } }int num(int k)//有几个质数因子 {int t=0;for(int i=1;i<=k;i++){if(k%i==0&&c[i]==0)++t;if(k==i*i&&c[i]==0)t++;}return t;}int main(){int t;scanf("%d",&t);while(t--){scanf("%d%d",&a,&b);f();int sum=0; if(a>b){for(int i=b+1;i<=a;i++)sum+=num(i);//printf("%d\n",num(i));}printf("%d\n",sum);}return 0;}



0 0
原创粉丝点击