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

hdu 5317 RGCDQ 多校 思维题

时间:2015-07-28 21:11:40      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:acm

点击打开链接题目链接 

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 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的素因子种类数(就是f(4)=1,而f(6)=2)

给出l,r求在l,r中任意f(i)和f(j)的gcd的最大值

预处理出所有素数 和所有数的f

用s[i][j]代表j数之前f值为i的个数

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 1111111
using namespace std;
bool is_prime[MAXN];
int prime[MAXN];
int f[MAXN];
int s[10][MAXN];
void init1(){
    int p=1;
    for(int i=0;i<=MAXN;i++)
        is_prime[i]=1;
    is_prime[0]=is_prime[1]=0;
    for(int i=2;i<=MAXN;i++){
        if(is_prime[i]){
            prime[p++]=i;
            for(int j=2*i;j<=MAXN;j+=i)
                is_prime[j]=0;
        }
    }
}
int getfact(int x){
    int ans=0;
    for(int i=1;prime[i]*prime[i]<=x;i++){
        if(x%prime[i]==0){
            ans++;
            while(x%prime[i]==0)
                x/=prime[i];
            if(is_prime[x]){
                ans++;
                break;
            }
        }
    }
    return ans;
}
void init2(){
    memset(s,0,sizeof(s));
    s[1][2]=1;
    for(int i=1;i<=MAXN;i++){
        for(int j=1;j<=7;j++){
            if(j!=f[i])
                s[j][i]=s[j][i-1];
            else s[j][i]=s[j][i-1]+1;
        }
    }
}
int gcd(int a,int b){
    if(b==0)
        return a;
    return gcd(b,a%b);
}
int main(){
    init1();
    for(int i=2;i<=MAXN;i++){
        if(is_prime[i])
            f[i]=1;
        else
            f[i]=getfact(i);
    }
    init2();
    int l,r;
    int t;
    scanf("%d",&t);
    int cnt[10];
    while(t--){
        scanf("%d %d",&l,&r);
        l--;
        for(int i=1;i<=7;i++){
            cnt[i]=s[i][r]-s[i][l];
        }
        int ans=0;
        for(int i=7;i>=1;i--){
            if(cnt[i]>=2){
                ans=i;
                break;
            }
        }
        for(int i=1;i<=7;i++){
            if(cnt[i]){
                for(int j=i+1;j<=7;j++){
                    if(cnt[j]){
                        ans=max(ans,gcd(i,j));
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


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

hdu 5317 RGCDQ 多校 思维题

标签:acm

原文地址:http://blog.csdn.net/qq_16843991/article/details/47110333

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