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
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的素因子种数。在给定区间[L R]内找出f(x)的最大值并要求最大值有两个以上。
求解过程:先求出2到1000000中每个数的f(x)值(类素数筛选法)。定义a[i][j]从2到i-1中f(x)=j出现的次数。易知最大是7(如何知道自己去想)。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 #define N 1000010 6 int s[N],a[N][8],str[8]; 7 void f() 8 { 9 int i,j; 10 for (i=2;i<N;i++) 11 { 12 if (!s[i]) 13 for (j=i;j<N;j+=i) s[j]++; 14 } 15 } 16 int main() 17 { 18 int t,l,r,ans; 19 int i,j; 20 f(); 21 for (i=2;i<N;i++) 22 { 23 for (j=1;j<8;j++) 24 { 25 a[i][j]=str[j]; 26 } 27 str[s[i]]++; 28 } 29 scanf("%d",&t); 30 while (t--) 31 { 32 scanf("%d%d",&l,&r); 33 for (i=7;i>=1;i--) 34 { 35 if (a[r+1][i]-a[l][i]>=2) 36 { 37 printf("%d ",i); 38 break; 39 } 40 } 41 } 42 }