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)) (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
 


Author
ZSTU
 


Source
 
/*素数筛
然后对所有情况讨论
*/


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int flag[1000005];
int cout[1000005];
int sum[1000005][10];
int res[10];
void inti()
{
    for(int i = 2; i <= 1000000; i++){
        if(!flag[i]){
            for(int j = i;j <= 1000000; j+=i){
                flag[j] = 1;
                cout[j]++;
            }
        }
    }
    for(int i = 0; i <= 7; i++){
        for(int j = 2; j <= 1000000; j++){
            sum[j][i] = sum[j-1][i] + (cout[j] == i + 1);
        }
    }
}


int main()
{
inti();
int T;
scanf("%d", &T);
int L, R;
while(T--){
    scanf("%d%d", &L,&R);
    int ans = 1;
    for(int i = 0 ; i <= 7; i++)
        res[i+1] = sum[R][i] - sum[L-1][i];
        if(res[2] + res[4] + res[6] >= 2) ans = 2;
        if(res[3] + res[6] >= 2) ans = 3;
        if(res[4] >= 2) ans = 4;
        if(res[5] >= 2) ans = 5;
        if(res[6] >= 2) ans = 6;
        if(res[7] >= 2) ans = 7;
        printf("%d\n", ans);
}
return 0;
}

  

 

posted @ 2015-08-01 15:38  Painting、时光  阅读(200)  评论(0编辑  收藏  举报