RGCDQ

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 626    Accepted Submission(s): 298


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)) (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
 
题意:F(x)表示x含有的质因子种类个数,F(2) = 1, F(10) = 2 求给定区间[l, r]内gcd(F(i), F(j))的最大值 (l <= i < j <= r)。
   数据范围 (T <= 1000000, 2 <= l < r <= 1000000)
思路:由于F(x) 最大不超过7, 所以可以打表出所有的F(x)的值。
 
 1 #include<algorithm>
 2 #include<iostream>
 3 #include<limits.h>
 4 #include<stdlib.h>
 5 #include<string.h>
 6 #include<complex>
 7 #include<cstring>
 8 #include<iomanip>
 9 #include<stdio.h>
10 #include<bitset>
11 #include<cctype>
12 #include<math.h>
13 #include<string>
14 #include<time.h>
15 #include<vector>
16 #include<cmath>
17 #include<queue>
18 #include<stack>
19 #include<list>
20 #include<map>
21 #include<set>
22 
23 #define LL long long
24 
25 using namespace std;
26 const LL mod = 1e9 + 7;
27 const double PI = acos(-1.0);
28 const double E = exp(1.0);
29 const int M = 1e6 + 1;
30 
31 int a[M];
32 bool prime[M];
33 int sum[M][8];
34 
35 void fun()
36 {
37     memset(prime, 0, sizeof(prime));
38     memset(sum, 0, sizeof(sum));
39     memset(a, 0, sizeof(a));
40     for(int i = 2; i < M; ++i){
41         if(!prime[i]){
42             a[i]++;
43             for(int j = 2 * i; j < M; j += i){
44                 prime[j] = 1;
45                 a[j]++;
46             }
47         }
48     }
49     for(int i = 2; i < M; ++i){
50         for(int j = 2; j < 8; ++j)
51             sum[i][j] = sum[i - 1][j];
52         sum[i][a[i]]++;
53     }
54 }
55 
56 int main()
57 {
58     fun();
59     int t;
60     cin >> t;
61     int l, r;
62     while( t-- ){
63         scanf("%d%d", &l, &r);
64         if(sum[r][7] - sum[l - 1][7] > 1)
65             puts("7");
66         else if(sum[r][6] - sum[l - 1][6] > 1)
67             puts("6");
68         else if(sum[r][5] - sum[l - 1][5] > 1)
69             puts("5");
70         else if(sum[r][4] - sum[l - 1][4] > 1)
71             puts("4");
72         else if(sum[r][6] + sum[r][3] - sum[l - 1][6] - sum[l - 1][3] > 1)
73             puts("3");
74         else if(sum[r][6] + sum[r][4] + sum[r][2] - sum[l - 1][6] - sum[l - 1][4] - sum[l - 1][2] > 1)
75             puts("2");
76         else
77             puts("1");
78     }
79     return 0;
80 }

 

posted on 2015-07-28 21:55  Unico  阅读(114)  评论(0)    收藏  举报