RGCDQ (HDU5317)

RGCDQ

题目描述

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)) (L≤i< j≤R)

题意

定义函数f(x)表示:x的不同素因子个数。

给定L和R(L<=i< j<=R),求区间内任意不相等的两个数f(x)的最大公约数的最大值。

解法

2*3*5*7*11*13*17 > 10 ^ 6 ,所以f(x)的最大值为7.

先用筛素数的方法求出每个数素因子的个数,然后记录一下每个数i是否有j个素因子(f[i][j])再求出其前缀和就是2到i中素因子个数为j的数的个数。

对于每次查询[l, r],如果存在j对任意k(j < k)有sum[l-1][j] - sum[r][j] > 1并且sum[l-1][k] - sum[r][k] <= 1, 那么说明存在 l <= x < y <=r 使 gcd(f(x),f(y)) = f(x) = f(y) 即此时的f(x)为答案。

#include
#include
#include

using namespace std;

const int pn = 1000000;
int mark[pn+1];
int sum[pn+1][8];//,prime[pn+1],cnt; // mark[i]代表i的最小素因子,prime[i]代表第i个素数
void Get_Prime(int n=pn){
    for(int i=2; i<=n; i++){
        if(!mark[i])// mark[i] = prime[cnt++] = i;
            for(int j = i; j <= n; j += i){
                mark[j]++;
            }
    }
}

void init(){
    for(int i = 2; i <= pn; i++){
        for(int j = 1; j <= 7; j++) sum[i][j] = sum[i-1][j];
        sum[i][mark[i]]++;
    }
}

int main(){
    Get_Prime();
    init();
    int T;
    scanf("%d",&T);
    while(T--){
        int l, r;
        scanf("%d%d",&l,&r);
        int ans = 1;
        for(int i = 7; i > 0; i--)
        {
            if(sum[r][i] - sum[l-1][i] > 1){
                ans = i;
                break;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(acm,math,数论,hdu)