码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5317 RGCDQ(素数个数 多校2015啊)

时间:2015-07-29 12:21:43      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:hdu   多校2015   gcd   素数   数学   

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5317


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(x)它的值是x的素因子不同的个数;

如:f(2) = 1, f(3) = 1;
其中(L<=i<j<=R),即区间内任意不相等的两个数的最大公约数的最大值;

PS:

因为2*3*5*7*11*13*17 > 1e6!

所以f(x)的值最大为7;

我们先打表求出每个f(x)的值;

//int s[maxn][10];//前i个F中j的个数

然后再利用前缀和s[r][i] - s[l-1][i]!

求出区间[l, r]的值;

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define maxn 1000000+7
int prim[maxn];
int s[maxn][10];//前i个F中j的个数
int GCD(int a, int b)
{
    if(b==0)
        return a;
    return GCD(b, a%b);
}
void init()
{
    memset(prim, 0, sizeof(prim));
    memset(s, 0, sizeof(s));
    for(int i = 2; i < maxn; i++)
    {
        if(prim[i]) continue;
        prim[i] = 1;
        for(int j = 2; j * i < maxn; j++)
        {
            prim[j*i]++;//不同素数个数
        }
    }
    s[2][1] = 1;
    for(int i = 3; i < maxn; i++)
    {
        for(int j = 1; j <= 7; j++)
        {
            s[i][j] = s[i-1][j];
        }
        s[i][prim[i]]++;
    }
}
int main()
{
    int t;
    int l, r;
    init();
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&l,&r);
        int c[17];
        int k = 0;
        for(int i  = 1; i <= 7; i++)
        {
            int tt = s[r][i] - s[l-1][i];
            if(tt >= 2)//超过两个以上记为2个即可
            {
                c[k++] = i;
                c[k++] = i;
            }
            else if(tt == 1)
            {
                c[k++] = i;
            }
        }
        int maxx = 1;
        for(int i = 0; i < k-1; i++)
        {
            for(int j = i+1; j < k; j++)
            {
                int tt = GCD(c[i],c[j]);
                maxx = max(maxx, tt);
            }
        }
        printf("%d\n",maxx);
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU 5317 RGCDQ(素数个数 多校2015啊)

标签:hdu   多校2015   gcd   素数   数学   

原文地址:http://blog.csdn.net/u012860063/article/details/47123587

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!