hdu5317 RGCDQ

RGCDQ

题意:F(x)表示x的质因子的种数。给区间[L,R],求max(GCD(F(i),F(j)) (L≤i<j≤R)。(2<=L < R<=1000000)

题解:可以用筛选法求质因子种数。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N=1e6+5;

int f[N],num[N][10],b[10];
void init()
{
    memset(f,0,sizeof(f));
    for(int i=2;i<N;i++)
    {
        if(f[i]==0)
        {
            f[i]=1;
            for(int j=i+i;j<N;j+=i)
            {
                f[j]++;
            }
        }
    }
    for(int i=2;i<N;i++)
    {
        for(int j=1;j<8;j++)
        {
            num[i][j]=num[i-1][j];
        }
        num[i][f[i]]++;
    }
}

int main()
{
    int t,l,r;
    init();
    cin>>t;
    while(t--)
    {
        scanf("%d%d",&l,&r);
        int ans=0;
        for(int i=1;i<8;i++)
        {
            b[i]=num[r][i]-num[l-1][i];
            if(num[r][i]-num[l-1][i]>1)
                ans=i;
        }
        if(ans!=0)
            printf("%d\n",ans);
        else if((b[2]>0&&b[4]>0)||(b[2]>0&&b[6]>0)||(b[6]>0&&b[4]>0))
            printf("2\n");
        else if(b[3]>0&&b[6]>0)
            printf("3\n");
        else
            printf("1\n");
    }
    return 0;
}

RGCDQ

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

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

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
时间: 2024-12-15 05:17:14

hdu5317 RGCDQ的相关文章

hdu5317 RGCDQ 统计

// hdu5317 RGCDQ // // 题目大意: // // 给定一个闭区间[l,r],定义f(x)是x的不同的质因子的个数 // 比方: 12 = 2 * 2 * 3,是两种.所以f(x) = 2,问max GCD(f[i],f[j]) // i,j在[l,r]范围内,而且i!=j. // // 解题思路: // // 首先伟大的W神发现了一个规律.就是小于等于1e6不同的素因子个数不会超过7个 // 这样.我们就行统计出在1到i这个区间内,1-7各出现了多少次.最后R-(L-1)>=

hdu5317 RGCDQ(dp)

题意:F(x)表示数x质因子的个数,对于给定的区间(L,R),求maxGcd(F(i),F(j)) (l<=i<=j<=r) 思路:打表.2*3*5*7*11*13*17*19>MAXN,所以一个数最多只有7个质因子. #include<iostream> #include<cstdio> #include<cstring> #define MAXN 1000010 using namespace std; int isp[MAXN]; int

hdu5317(2015多校3)--RGCDQ(素数筛+枚举)

题目链接:点击打开链接 题目大意:定义f(i)为组成i的素数的种类,求在区间[l,r]内的gcd(f(i),f(j))  (l <= i < j <= r)的最大值 素数筛筛出10^6内的素数,求出每一个数的f(i),可以发现f(i) <= 7,用一个数组统计一下s[i][j]从2到i内f(x)=j有多少个,在计算区间的时候直接相减,判断一下最大的gcd #include <cstdio> #include <cstring> #include <cm

2015 HDU 多校联赛 5317 RGCDQ 筛法求解

2015 HDU 多校联赛 5317 RGCDQ 筛法求解 题目  http://acm.hdu.edu.cn/showproblem.php? pid=5317 本题的数据量非常大,測试样例多.数据量大, 所以必须做预处理.也就是用筛法求出全部的F[x],将全部F[x] 打印出来发现.事实上结果不大,最大的数值是7.所以对于每一个区间询问, 直接暴力求取有多少个 1 2 3 4 5 6 7 就可以,从大到小查找.假设出现2个以上 3-7 的数值,那么最大公约数就是该数字. 假设没有出现两个反复

HDU 5317 RGCDQ

RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 323    Accepted Submission(s): 162 Problem Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more and mo

HDOJ 5317 RGCDQ 水

预处理出每个数有多少个不同的因数,因数最多不超过7 RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 641    Accepted Submission(s): 304 Problem Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He

HDU-5317

RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 698    Accepted Submission(s): 328 Problem Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more and mor

hdu5371 RGCDQ 统计

// hdu5371 RGCDQ // // 题目大意: // // 给定一个闭区间[l,r],定义f(x)是x的不同的质因子的个数 // 比如: 12 = 2 * 2 * 3,是两种,所以f(x) = 2,问max GCD(f[i],f[j]) // i,j在[l,r]范围内,并且i!=j. // // 解题思路: // // 首先伟大的W神发现了一个规律,就是小于等于1e6不同的素因子个数不会超过7个 // 这样,我们就可以统计出在1到i这个区间内,1-7各出现了多少次,最后R-(L-1)>

hdu 5317 RGCDQ 多校 思维题

点击打开链接题目链接 RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 328    Accepted Submission(s): 164 Problem Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find