結果
問題 |
No.917 Make One With GCD
|
ユーザー |
![]() |
提出日時 | 2020-08-27 07:37:47 |
言語 | Java (openjdk 23) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,298 bytes |
コンパイル時間 | 2,615 ms |
コンパイル使用メモリ | 79,180 KB |
実行使用メモリ | 41,772 KB |
最終ジャッジ日時 | 2024-11-07 15:26:11 |
合計ジャッジ時間 | 9,329 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 21 WA * 11 |
ソースコード
import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); TreeMap<Integer, Long> dp = new TreeMap<>(); for (int i = 0; i < n; i++) { int a = sc.nextInt(); HashMap<Integer, Long> tmp = new HashMap<>(); for (int x : dp.keySet()) { int gcd = getGCD(a, x); if (dp.containsKey(gcd)) { dp.put(gcd, dp.get(gcd) + dp.get(x)); } else { if (tmp.containsKey(gcd)) { tmp.put(gcd, tmp.get(gcd) + dp.get(x)); } else { tmp.put(gcd, dp.get(x)); } } } if (dp.containsKey(a)) { dp.put(a, dp.get(a) + 1); } else { dp.put(a, 1L); } for (Map.Entry<Integer, Long> entry : tmp.entrySet()) { dp.put(entry.getKey(), entry.getValue()); } } if (dp.containsKey(1)) { System.out.println(dp.get(1)); } else { System.out.println(0); } } static int getGCD(int x, int y) { if (x % y == 0) { return y; } else { return getGCD(y, x % y); } } }