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 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
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.
See the sample for more details.
Sample Input
2
2 3
3 5
Sample Output
1
1
Source
Recommend
1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 5 int a[1000050],b[1000000],k,f[1000050],s[1000050][10]; 6 7 int Sieve(int n) 8 { 9 a[1]=0;k=0;a[0]=0; 10 for (int i = 2; i <= n; i++) 11 a[i] =1; 12 for (int i = 2; i <= sqrt(n); i++) 13 { 14 if (a[i]) 15 for (int j = i; j*i <=n; j++) 16 a[j * i] = 0; 17 } 18 for (int i = 0; i <= n; i++) 19 { 20 if (a[i]==1) 21 { 22 k++; 23 b[k]=i; 24 } 25 } 26 } 27 28 int gcd(int a,int b) 29 { 30 if(a<b) 31 return gcd(b,a); 32 else if(b==0) 33 return a; 34 else 35 return gcd(b,a%b); 36 } 37 38 int main() 39 { 40 int T; 41 int i,j,k; 42 Sieve(1000000); 43 memset(f,0,sizeof(f)); 44 for(i=2;i<=1000000;i++) 45 { 46 int x=i; 47 k=1; 48 while(1) 49 { 50 if(x==1) 51 { 52 break; 53 } 54 if(a[x]==1) 55 { 56 f[i]++; 57 break; 58 } 59 60 if(x%b[k]==0) 61 { 62 f[i]++; 63 while(x%b[k]==0) 64 { 65 x=x/b[k]; 66 } 67 } 68 k++; 69 } 70 //printf("%d ",f[i]); 71 } 72 memset(s,0,sizeof(s)); 73 for(i=1;i<=1000000;i++) 74 { 75 for(j=1;j<=7;j++) 76 s[i][j]=s[i-1][j]; 77 s[i][f[i]]++; 78 } 79 scanf("%d",&T); 80 int l,r; 81 int num[15]; 82 while(T--) 83 { 84 memset(num,0,sizeof(num)); 85 scanf("%d %d",&l,&r); 86 for(i=1;i<=8;i++) 87 num[i]=s[r][i]-s[l-1][i]; 88 int ma=0; 89 for(i=10;i>=1;i--) 90 { 91 if(num[i]>0) 92 { 93 if(num[i]>=2) 94 { 95 if(i>ma) 96 ma=i; 97 } 98 else 99 { 100 for(j=i-1;j>=1;j--) 101 { 102 if(num[j]>0) 103 if(gcd(i,j)>ma) 104 ma=gcd(i,j); 105 } 106 } 107 108 } 109 } 110 printf("%d ",ma); 111 } 112 return 0; 113 }