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
题意: F(n)=m;意思是n这个数能分解成几种素数因子,例如12=2*2*3;12有两种素数因子 ,so:f[12]=2;
2<=L < R<=1000000,【L,R】之间任选两个数 i,j,求GCD(F(i),F(j))的最大值;
思路 :素数筛法+计算素因子放在一块;即只要是素数的倍数,则这个数一定不是素数 ,但这个数的素因子一定有这个素数。
表打好后,1<= T <= 1000000,2<=L < R<=1000000, 这么多组测试数据,每组运算时间必须很短才能不超时,所以要先把所有数据处理一遍,然后直接用,我们需要知道【L,R】这个区间的数是什么,分别有多少个; 这些数最大应该是多少呢,计算2*3*5*7*11*13*17大约等于500000;说明最大的数应该是7,如果是8,则一定超过1000000了; 开数组k【1000005】【8】;k【i】【j】表示前 i 个数为 j 的有多少个;
k【R】【j】-k【L-1】【j】表示这个区间有多少个j;
附上AC代码:
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<cmath> using namespace std; int f[1000005]; int s[1000005]; int k[1000005][8]; int a[10]; int main() { int T,l,r; scanf("%d",&T); for(int i=2;i<=1000000;i++) { if(f[i]==0) for(int j=i;j<=1000000;j+=i) { f[j]=1; s[j]++; } for(int j=1;j<=7;j++) k[i][j]=k[i-1][j]; k[i][s[i]]++; } while(T--) { memset(a,0,sizeof(a)); scanf("%d%d",&l,&r); for(int i=1;i<=7;i++) a[i]=k[r][i]-k[l-1][i]; if(a[7]>=2) printf("7\n"); else if(a[6]>=2)printf("6\n"); else if(a[5]>=2)printf("5\n"); else if(a[4]>=2)printf("4\n"); else if(a[3]>=2)printf("3\n"); else if(a[3]&&a[6])printf("3\n"); else if(a[2]>=2)printf("2\n"); else if((a[6]&&a[4])||(a[4]&&a[2])||(a[6]&&a[2]))printf("2\n"); else printf("1\n"); } }
1 1