Skip to content

Commit d0cde8b

Browse files
authored
[20250811] BOJ / P5 / 멋진 부분집합 / 권혁준
1 parent ddb2f8b commit d0cde8b

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
public class Main {
51+
52+
static IOController io;
53+
54+
//
55+
56+
static int N;
57+
static int[] a;
58+
59+
public static void main(String[] args) throws Exception {
60+
61+
io = new IOController();
62+
63+
init();
64+
solve();
65+
66+
io.close();
67+
68+
}
69+
70+
static void init() throws Exception {
71+
72+
N = io.nextInt();
73+
a = new int[N];
74+
for(int i=0;i<N;i++) a[i] = io.nextInt();
75+
76+
}
77+
78+
static void solve() throws Exception {
79+
80+
for(int t=0;t<50;t++) {
81+
int idx = (int)(Math.random() * N);
82+
List<Integer> primes = new ArrayList<>();
83+
int A = a[idx];
84+
for(int i=2;i*i<=A;i++) if(A%i == 0) {
85+
primes.add(i);
86+
while(A%i == 0) A /= i;
87+
}
88+
if(A != 1) primes.add(A);
89+
90+
for(int p:primes) {
91+
int cnt = 0;
92+
for(int i=0;i<N;i++) cnt += (a[i] % p == 0) ? 1 : 0;
93+
if(cnt >= (N+1)/2) {
94+
io.write("YES\n");
95+
int c = 0;
96+
for(int j=0;j<N;j++) if(a[j] % p == 0) {
97+
c++;
98+
io.write(a[j] + " ");
99+
if(c == (N+1)/2) return;
100+
}
101+
}
102+
}
103+
}
104+
io.write("NO");
105+
106+
}
107+
108+
}
109+
```

0 commit comments

Comments
 (0)