結果
問題 | No.1059 素敵な集合 |
ユーザー |
![]() |
提出日時 | 2020-05-26 11:38:31 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 261 ms / 2,000 ms |
コード長 | 1,324 bytes |
コンパイル時間 | 2,232 ms |
コンパイル使用メモリ | 78,332 KB |
実行使用メモリ | 61,812 KB |
最終ジャッジ日時 | 2024-07-23 09:39:01 |
合計ジャッジ時間 | 6,950 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
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(); if (left == 1) { System.out.println(0); return; } UnionFindTree uft = new UnionFindTree(right - left + 1); for (int i = left; i <= right / 2; i++) { for (int j = 2; j * i <= right; j++) { uft.unite(i - left, i * j - left); } } System.out.println(uft.getCount() - 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 (parents[x] == 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(x)] = find(y); } } public int getCount() { HashSet<Integer> set = new HashSet<>(); for (int i = 0; i < parents.length; i++) { set.add(find(i)); } return set.size(); } } }