-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRGQ_01.java
More file actions
59 lines (48 loc) · 1.58 KB
/
RGQ_01.java
File metadata and controls
59 lines (48 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package _aTemplate;
// Range GCD Query
public class RGQ_01 {
public static class RGQ {
public int[][] gcd;
public RGQ(int[] arr) {
int n = arr.length;
int k = power2(n);
gcd = new int[n + 1][k + 1]; // 下标从1开始
for (int i = 1; i <= n; i++) {
gcd[i][0] = arr[i - 1];
}
for (int j = 1; (1 << j) <= n; j++) {
for (int i = 1; i + (1 << j) - 1 <= n; i++) {
gcd[i][j] = gcd(
gcd[i][j - 1],
gcd[i + (1 << (j - 1))][j - 1]);
}
}
}
public static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
public int rangeGCD(int l, int r) {
// l...r -> r - l + 1 -> 2的哪个次方最接近它!
int k = power2(r - l + 1);
return gcd(gcd[l][k], gcd[r - (1 << k) + 1][k]);
}
// 离n最近的2的某次幂
private int power2(int n) {
int ans = 0;
// m >> 1 : m先减半为了退出循环时 ans 不用--
while ((1 << ans) <= (n >> 1)) {
ans++;
}
return ans;
}
}
// 为了测试
public static void main(String[] args) {
// int[] nums = {9, 6, 9, 3, 15};
int[] nums = {8, 2, 6, 10};
int N = nums.length;
RGQ rgcd = new RGQ(nums);
System.out.println(rgcd.rangeGCD(1, N)); // 下标从1开始
System.out.println("Finish.");
}
}