結果
問題 |
No.1059 素敵な集合
|
ユーザー |
![]() |
提出日時 | 2020-08-20 18:18:36 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 251 ms / 2,000 ms |
コード長 | 1,313 bytes |
コンパイル時間 | 2,245 ms |
コンパイル使用メモリ | 78,404 KB |
実行使用メモリ | 63,688 KB |
最終ジャッジ日時 | 2024-07-23 09:40:58 |
合計ジャッジ時間 | 7,300 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int left = sc.nextInt(); int right = sc.nextInt(); UnionFindTree uft = new UnionFindTree(right + 1); for (int i = left; i <= right; i++) { for (int j = 2; j * i <= right; j++) { uft.unite(i, i * j); } } HashSet<Integer> set = new HashSet<>(); for (int i = left; i <= right; i++) { set.add(uft.find(i)); } System.out.println(set.size() - 1); } static class UnionFindTree { int[] parents; public UnionFindTree(int size) { parents = new int[size]; for (int i = 0; i < size; i++) { parents[i] = i; } } public int find(int x) { if (x == parents[x]) { return x; } else { return parents[x] = find(parents[x]); } } public boolean same(int x, int y) { return find(x) == find(y); } public void unite(int x, int y) { if (!same(x, y)) { parents[find(y)] = find(x); } } } }