結果
問題 | No.442 和と積 |
ユーザー |
![]() |
提出日時 | 2019-12-10 11:33:35 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 139 ms / 1,000 ms |
コード長 | 1,817 bytes |
コンパイル時間 | 2,435 ms |
コンパイル使用メモリ | 79,520 KB |
実行使用メモリ | 41,628 KB |
最終ジャッジ日時 | 2024-06-23 23:54:53 |
合計ジャッジ時間 | 6,054 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 18 |
ソースコード
import java.util.*;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);long aa = sc.nextLong();long bb = sc.nextLong();long c = aa + bb;long a = gcd(aa + bb, aa);long b = gcd(aa + bb, bb);HashSet<Long> set = new HashSet<>();for (long i = 2; i <= Math.sqrt(a); i++) {while (a % i == 0) {set.add(i);a /= i;}}if (a != 1) {set.add(a);}for (long i = 2; i <= Math.sqrt(b); i++) {while (b % i == 0) {set.add(i);b /= i;}}if (b != 1) {set.add(b);}HashMap<Long, Integer> mapX = new HashMap<>();for (long x : set) {int idx = 0;while (c % x == 0) {idx++;c /= x;}mapX.put(x, idx);}HashMap<Long, Integer> mapY = new HashMap<>();for (long x : set) {int idx = 0;while (aa % x == 0) {idx++;aa /= x;}while (bb % x == 0) {idx++;bb /= x;}mapY.put(x, idx);}long ans = 1;for (Map.Entry<Long, Integer> entry : mapX.entrySet()) {if (mapY.containsKey(entry.getKey())) {ans *= (long)(Math.pow(entry.getKey(), Math.min(entry.getValue(), mapY.get(entry.getKey()))));}}System.out.println(ans);}static long gcd(long x, long y) {if (x % y == 0) {return y;} else {return gcd(y, x % y);}}}