結果
| 問題 |
No.917 Make One With GCD
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2021-11-04 10:44:37 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 56 ms / 2,000 ms |
| コード長 | 1,748 bytes |
| コンパイル時間 | 2,346 ms |
| コンパイル使用メモリ | 78,124 KB |
| 実行使用メモリ | 37,176 KB |
| 最終ジャッジ日時 | 2024-10-14 08:04:27 |
| 合計ジャッジ時間 | 5,424 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 32 |
ソースコード
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
int n = sc.nextInt();
HashMap<Integer, Long> counts = new HashMap<>();
for (int i = 0; i < n; i++) {
HashMap<Integer, Long> nexts = new HashMap<>();
int x = sc.nextInt();
for (int y : counts.keySet()) {
int gcd = getGCD(x, y);
nexts.put(gcd, nexts.getOrDefault(gcd, 0L) + counts.get(y));
}
nexts.put(x, nexts.getOrDefault(x, 0L) + 1);
for (int y : nexts.keySet()) {
counts.put(y, counts.getOrDefault(y, 0L) + nexts.get(y));
}
}
System.out.println(counts.getOrDefault(1, 0L));
}
static int getGCD(int x, int y) {
if (x % y == 0) {
return y;
} else {
return getGCD(y, x % y);
}
}
}
class Scanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
public Scanner() throws Exception {
}
public int nextInt() throws Exception {
return Integer.parseInt(next());
}
public long nextLong() throws Exception {
return Long.parseLong(next());
}
public double nextDouble() throws Exception {
return Double.parseDouble(next());
}
public String nextLine() throws Exception {
return br.readLine();
}
public String next() throws Exception {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten