-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJ07023_NguyenToVaThuanNghich.java
71 lines (65 loc) · 2.11 KB
/
J07023_NguyenToVaThuanNghich.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
//B21DCCN441
import java.util.*;
import java.io.*;
import java.math.*;
public class J07023_NguyenToVaThuanNghich {
public static int N = 1000000;
public static int[] p = new int[N + 1];
public static void Sang() {
for (int i = 0; i <= N; i++) {
p[i] = 1;
}
p[0] = p[1] = 0;
int k = (int) Math.sqrt(N + 1);
for (int i = 2; i <= k; i++) {
if (p[i] == 1) {
for (int j = i * i; j <= N; j += i) {
p[j] = 0;
}
}
}
}
public static boolean check(int k) {
String s = "" + k;
int n = s.length();
int l = 0;
int r = n - 1;
char[] c = s.toCharArray();
while (l < r) {
if (c[l++] != c[r--]) {
return false;
}
}
return true;
}
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
Sang();
ObjectInputStream ois1 = new ObjectInputStream(new FileInputStream("DATA1.in"));
ArrayList<Integer> a1 = (ArrayList<Integer>) ois1.readObject();
ObjectInputStream ois2 = new ObjectInputStream(new FileInputStream("DATA2.in"));
ArrayList<Integer> a2 = (ArrayList<Integer>) ois2.readObject();
TreeSet<Integer> ts = new TreeSet<>();
TreeMap<Integer, Integer> tm1 = new TreeMap<>();
TreeMap<Integer, Integer> tm2 = new TreeMap<>();
for (int k : a1) {
ts.add(k);
if (tm1.containsKey(k)) {
tm1.put(k, tm1.get(k) + 1);
} else {
tm1.put(k, 1);
}
}
for (int k : a2) {
if (tm2.containsKey(k)) {
tm2.put(k, tm2.get(k) + 1);
} else {
tm2.put(k, 1);
}
}
for (int k : ts) {
if (p[k] == 1 && tm2.containsKey(k) && check(k)) {
System.out.println(k + " " + tm1.get(k) + " " + tm2.get(k));
}
}
}
}