-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMain.java
44 lines (35 loc) · 1.12 KB
/
Main.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
package dev.idion.baekjoon.gcdsum;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int repeat = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
for (int i = 0; i < repeat; i++) {
String[] inputs = br.readLine().split(" ");
int size = Integer.parseInt(inputs[0]);
int[] input = new int[size];
for (int j = 0; j < size; j++) {
input[j] = Integer.parseInt(inputs[j + 1]);
}
long sum = 0;
for (int j = 0; j < size; j++) {
for (int k = j + 1; k < size; k++) {
int max = Math.max(input[j], input[k]);
int min = Math.min(input[j], input[k]);
sum += gcd(max, min);
}
}
sb.append(sum).append("\n");
}
System.out.print(sb.deleteCharAt(sb.lastIndexOf("\n")).toString());
}
public static int gcd(int p, int q) {
if (q == 0) {
return p;
}
return gcd(q, p % q);
}
}