HDU5317 RGCDQ 质因子分解

传送门:HDU 5317

RGCDQ

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


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
 


Source

题意:定义F(i)为i的质因子的个数(去重),给定一个L,R。求gcd(f(i),f(j))的最大值,要求2<=L<=i<j<=R<=100w.测试组数小于100w
思路:测试组数太多了,必须要O(1)得出每一组测试。先预处理出1到100w的每个数的f(i)。由于i最大只有100w,所以f(i)的最大值只有7。用sum[i][j]记录从0~j,f[j]==i的数的个数。查询的时候,直接枚举f(i)和f(j)的值即可。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 1000005
int prime[10000];
int f[maxn];
void getprime()
{
    bool isprime[10005];
    memset(isprime,1,sizeof(isprime));
    for(int i=2; i*i<=10000; i++)
        if(isprime[i])
            for(int j=i*i; j<=10000; j+=i) isprime[j]=0;
    prime[0]=0;
    for(int i=2; i<=10000; i++) if(isprime[i]) prime[++prime[0]]=i;
}
inline int get_num_prime(int x)
{
    int cnt=0;
    for(int i=1; prime[i]*prime[i]<=x; i++)
    {
        if(x%prime[i]==0)
        {
            cnt++;
            while(x%prime[i]==0) x/=prime[i];
        }
    }
    if(x>1) cnt++;
    return cnt;
}
using namespace std;
int sum[8][maxn];
int gcd(int a,int b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}
int main()
{
    getprime();
    for(int i=2; i<maxn; i++)
    {
        sum[get_num_prime(i)][i]++;
    }
    for(int j=0; j<8; j++)
        for(int i=0; i<maxn; i++)
            sum[j][i]+=sum[j][i-1];
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int L,R;
        scanf("%d %d",&L,&R);
        int have[8];
        memset(have,0,sizeof(have));
        for(int i=1; i<8; i++) have[i]=sum[i][R]-sum[i][L-1];
        int ans=0;
        for(int i=1; i<8; i++)
            if(have[i]>=2) ans=i;
            else if(have[i])
                for(int j=i+1; j<8; j++)
                    if(have[j])
                        ans=max(ans,gcd(j,i));
        printf("%d\n",ans);
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值