結果
問題 | No.2767 Add to Divide |
ユーザー |
![]() |
提出日時 | 2024-05-31 22:13:54 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 241 ms / 2,000 ms |
コード長 | 977 bytes |
コンパイル時間 | 2,839 ms |
コンパイル使用メモリ | 78,736 KB |
実行使用メモリ | 42,324 KB |
最終ジャッジ日時 | 2024-12-20 23:46:14 |
合計ジャッジ時間 | 7,293 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 16 |
ソースコード
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int z = 0; z < t; z++) { int a = sc.nextInt(); int b = sc.nextInt(); int d = b - a; int ans = Integer.MAX_VALUE; if (d % a == 0) { ans = 0; } else if (d < a) { ans = -1; } else { int a2 = a % d; List<Integer> list = divList(d); for (int e : list) { if (a2 <= e) { ans = Math.min(ans, e - a2); } } } System.out.println(ans); } sc.close(); } static List<Integer> divList(int n) { List<Integer> list = new ArrayList<>(); int end = (int) Math.sqrt(n); for (int i = 1; i <= end; i++) { if (n % i == 0) { list.add(i); } } int i = end * end == n ? list.size() - 2 : list.size() - 1; for ( ; i >= 0; i--) { list.add(n / list.get(i)); } return list; } }