-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP9613_조합.java
53 lines (47 loc) · 1.25 KB
/
P9613_조합.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
package net.acmicpc.순열조합;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class P9613_조합 {
static int N;
static int[] numbers;
static int[] result;
static long sum;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int t = 1; t <= T; ++t) {
String[] line = br.readLine().split(" ");
N = Integer.parseInt(line[0]);
numbers = new int[N];
for (int i = 0; i < N; ++i) {
numbers[i] = Integer.parseInt(line[i + 1]);
}
result = new int[2];
sum = 0;
Arrays.sort(numbers);
combination(0, 0);
System.out.println(sum);
}
}
private static void combination(int count, int valueIndex) {
if (count == 2) {
int a = result[0];
int b = result[1];
int gcd = 0;
for (int i = a; i > 0; --i) {
if (a % i == 0 && b % i == 0) {
gcd = i;
break;
}
}
sum += gcd;
return;
}
for (int i = valueIndex; i < N; ++i) {
result[count] = numbers[i];
combination(count + 1, i + 1);
}
}
}