hdu5317 RGCDQ (素数筛+递推方程)

hdu5317 RGCDQ (素数筛+递推方程):http://acm.split.hdu.edu.cn/showproblem.php?pid=5317

题面描述:

RGCDQ

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2825    Accepted Submission(s): 1111


Problem 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  maxGCD(F(i),F(j))   (Li<jR)
 

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
  
  
2 2 3 3 5
 

Sample Output
  
  
1 1
 

题目大意:

定义:F(x)表示x可以被素因子分解得到的素因子的种类数,给定区间[L,R],问区间内任意两个F(i)和F(j)之间的最大公约数的最大值为多少。


题目分析:

第一步肯定要先处理出来F(x)函数(肯定是通过素数筛,素因子分解的方法),然后再思考如果每次都在给定区间内暴力寻找,这样的方法肯定会超时。

通过大表和计算发现,由于2*3*5*7*11*13*17*19=9699690>1000000,所以,1000000以内的所有的数的素因子的种类数都小于等于7,所以,我们借助于地推的思想,记sum[i][j] 为从2到i区间内含有j种素因子数的个数,则递推关系为:sum[i][j]=sum[i-1][j]+1;(如果F(j)==j); 或sum[i][j]=sum[i][j-1];(F[j]!=j);

那么区间L到R的含有i个素因子的个数就等于sum[R][i]-sum[L-1][i]个,这样如果存在大于等于两个素因子种类数的个数为一样的值,那么最大的最大公约数就是该值,但是也要考虑一下特殊情况,比如同时有2,4,6(4,6)或者3,6的时候,取一下最大值就行了。


代码实现:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int MAXN=1000100;///MAXN的大小与题目规模有关
int prime[MAXN+1];
void getPrime()
{
    memset(prime,0,sizeof(prime));
    for(int i=2;i<=MAXN;i++)
    {
        if(!prime[i])
            prime[++prime[0]]=i;
        for(int j=1;j<=prime[0]&&prime[j]<=MAXN/i;j++)
        {
            prime[prime[j]*i]=1;
            if(i%prime[j]==0)
                break;
        }
    }
}

long long factor[1000100];
int fatCnt;
int getFactors(long long x)
{
    fatCnt=0;
    long long tmp=x;
    for(int i=1;prime[i]<=tmp/prime[i];i++)
    {
        if(tmp%prime[i]==0)
        {
            factor[fatCnt]=prime[i];
            while(tmp%prime[i]==0)
            {
                tmp/=prime[i];
            }
            fatCnt++;
        }
    }
    if(tmp!=1)
    {
        factor[fatCnt++]=tmp;
    }
    return fatCnt;
}

int F[1000010];
int sum[1000010][10];
int main()
{
    int T,L,R;
    getPrime();
    memset(F,0,sizeof(F));
    memset(sum,0,sizeof(sum));
    for(long long i=2;i<=1000000;i++)
    {
        int flag=getFactors(i);
        F[i]=flag;
    }
    for(int i=2;i<=1000000;i++)///根据递推关系得到2-i(2<=i<=1000000)中含有j个素因子的数的个数
    {
        for(int j=1;j<=7;j++)
        {
            if(F[i]==j)
            {
                sum[i][j]=sum[i-1][j]+1;
            }
            else
            {
                sum[i][j]=sum[i-1][j];
            }
        }
    }
    
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&L,&R);
        int dis[10];
        memset(dis,0,sizeof(dis));
        for(int i=1;i<=7;i++)
        {
            dis[i]=sum[R][i]-sum[L-1][i];
        }
        int ans=1;
        for(int i=7;i>=1;i--)
        {
            if(dis[i]>=2)
            {
                ans=i;
                break;
            }
        }
        if((dis[2]>0) && (dis[4]>0||dis[6]>0))
            ans=max(ans,2);
        if(dis[3]>0 && dis[6]>0)
            ans=max(ans,3);
        if(dis[4]>0 && dis[6]>0)
            ans=max(ans,2);
        printf("%d\n",ans);
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值