RGCDQ
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2335 Accepted Submission(s): 946
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
11
//最初读错了题,一直WA, 后来读对了题,一直TLE... //打表求出所有数的素因子个数,然后tab[i][j]表示i之前素因子个数为j出现的次数。然后对应相减,可得范围内素因子个数出现的次数,之后枚举所有情况就好了 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <map> using namespace std; const int N = 1000200; int prime[N]; int tab[N][9]; void table() { for(int i = 2; i < N; i++) //循环到N打表,求出所有数的素因子个数 if(prime[i] == 0) { prime[i]++; for(int j = i * 2; j < N; j += i) prime[j]++; } for(int i = 2; i < N; i++) for(int j = 1; j < 8; j++) { if(prime[i] == j) tab[i+1][j] = tab[i][j] + 1; else tab[i+1][j] = tab[i][j]; } } int main() { int t, a, b; table(); scanf("%d", &t); while(t--) { scanf("%d%d", &a, &b); int tmp[10] = {}; for(int i = 1; i < 8; i++) tmp[i] = tab[b+1][i] - tab[a][i]; int res = -1; for(int i = 1; i < 8; i++) //枚举出所有情况 if(tmp[i] > 1) res = max(res, i); if(tmp[6] > 0 && tmp[4] > 0) res = max(res, 2); if(tmp[6] > 0 && tmp[3] > 0) res = max(res, 3); if(tmp[4] > 0 && tmp[2] > 0) res = max(res, 2); printf("%d\n", res); } return 0; }