hdu 5317 RGCDQ 预处理和素因子的个数

来源:互联网 发布:java round 编辑:程序博客网 时间:2024/04/30 07:39
E - RGCDQ
Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 5317

Description

Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more and more interesting things about GCD. Today He comes up with Range Greatest Common Divisor Query (RGCDQ). What’s RGCDQ? Please let me explain it to you gradually. For a positive integer x, F(x) indicates the number of kind of prime factor of x. For example F(2)=1. F(10)=2, because 10=2*5. F(12)=2, because 12=2*2*3, there are two kinds of prime factor. For each query, we will get an interval [L, R], Hdu wants to know

Input

There are multiple queries. In the first line of the input file there is an integer T indicates the number of queries. 
In the next T lines, each line contains L, R which is mentioned above. 

All input items are integers. 
1<= T <= 1000000 
2<=L < R<=1000000 

Output

For each query,output the answer in a single line. 
See the sample for more details. 

Sample Input

22 33 5

Sample Output

1

1

思路:本题给出f(x)的定义,f(x)=x的素数因子个数,然后给出一个区间[l,r],让我们求出在这个区间之内任意两个元素x1,x2,使得gcd(f(x1),f(x2))取得最大值,输出这个最大值。首先题目中只要求求出数的素数因子的个数和次数没啥关系,那我们就要发动脑筋了,2*3*5*7*11*13*17>10^6;显然所给出的数的素数因子的个数不超过7个1.首先我们模仿素数表的过程打出f(x)的表2.运用递归算出小于i的元素中因子个数为j的元素的个数3.将区间中出现的数存在数组中(出现多次的要存两次--很好懂吧),然后求最大值就好了,枚举就可以(最大也就14*14);ac:代码

#include <iostream>#include <cstdio>#include <cstring>using namespace std;int c[1000005][8],b[8]= {0,1,3,5,7,11,13,17},a[1000005];int gcd(int a,int b){    return b?gcd(b,a%b):a;}int main(){    int n,x,y;    cin>>n;    memset(c,0,sizeof(c));    memset(a,0,sizeof(a));    for(int i=2; i<=1000005; i++)//打素数个数表    {        if(!a[i])        {            for(int j=i;j<1000005;j+=i)            a[j]++;        }    }    for(int i=2; i<1000005; i++)//统计小于i的素数因子有j个的数的个数    {        for(int j=1; j<=7; j++)        {            if(a[i]==j)                c[i][j]=c[i-1][j]+1;            else                c[i][j]=c[i-1][j];        }    }    while(n--)    {        scanf("%d%d",&x,&y);        int sum=1;int d[100];        int k=0;            for(int j=7;j>=1;j--)            {               if(c[y][j]-c[x-1][j]>=2)//都与两个的按两个统计用于求自身和自身的gcd               {                  d[k++]=j;                  d[k++]=j;               }               if(c[y][j]-c[x-1][j]==1)               d[k++]=j;            }            for(int i=0;i<k;i++)//枚举            {                for(int j=i+1;j<k;j++)                {                    sum=max(sum,gcd(d[i],d[j]));                }            }        cout<<sum<<endl;    }    return 0;}

0 0
原创粉丝点击