-
Notifications
You must be signed in to change notification settings - Fork 0
/
BearAndPrimeNumbers.java
125 lines (112 loc) · 2.9 KB
/
BearAndPrimeNumbers.java
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/** https://codeforces.com/problemset/problem/385/C #binary-search #implementation */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Set;
import java.util.StringTokenizer;
public class BearAndPrimeNumbers {
private static class Range {
int left, right;
Range(int left, int right) {
this.left = left;
this.right = right;
}
}
private static int max = 10000001;
public static void main(String[] args) {
MyScanner sc = new MyScanner(System.in);
int n = sc.nextInt();
// get input to map
HashMap<Integer, Integer> hashMap = new HashMap<>();
for (int i = 0; i < n; i++) {
int v = sc.nextInt();
if (!hashMap.containsKey(v)) {
hashMap.put(v, 1);
} else {
hashMap.replace(v, hashMap.get(v) + 1);
}
}
int m = sc.nextInt();
Range[] queries = new Range[m];
int left, right;
for (int i = 0; i < m; i++) {
left = sc.nextInt();
right = sc.nextInt();
queries[i] = new Range(left, right);
}
// calcualte largetFactor that is prime which each key in arrKey can separate.
int[] largestFactor = new int[max];
for (int i = 2; i < max; i++) {
largestFactor[i] = i;
}
for (int i = 2; i < largestFactor.length; i++) {
if (largestFactor[i] != i) continue;
for (int k = 2; i * k < largestFactor.length; k++) {
largestFactor[i * k] = i;
}
}
int[] result = new int[max];
Set<Integer> set = hashMap.keySet();
for (int value : set) {
int cnt = hashMap.get(value);
while (value > 1) {
int x = largestFactor[value];
result[x] += cnt;
while (value % x == 0) {
value /= x;
}
}
}
for (int i = 2; i < result.length; i++) {
result[i] += result[i - 1];
}
// process
for (Range range : queries) {
if (range.left >= max) {
System.out.println(0);
} else {
if (range.right >= max) {
range.right = max - 1;
}
System.out.println(result[range.right] - result[range.left - 1]);
}
}
}
}
class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}