結果
問題 | No.1059 素敵な集合 |
ユーザー | tenten |
提出日時 | 2021-11-24 09:55:00 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 145 ms / 2,000 ms |
コード長 | 2,302 bytes |
コンパイル時間 | 2,225 ms |
コンパイル使用メモリ | 74,516 KB |
実行使用メモリ | 58,888 KB |
最終ジャッジ日時 | 2023-09-08 16:15:23 |
合計ジャッジ時間 | 5,284 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 45 ms
49,244 KB |
testcase_01 | AC | 92 ms
51,484 KB |
testcase_02 | AC | 107 ms
54,528 KB |
testcase_03 | AC | 43 ms
49,480 KB |
testcase_04 | AC | 44 ms
49,208 KB |
testcase_05 | AC | 44 ms
49,676 KB |
testcase_06 | AC | 94 ms
52,848 KB |
testcase_07 | AC | 96 ms
52,644 KB |
testcase_08 | AC | 106 ms
54,388 KB |
testcase_09 | AC | 76 ms
51,736 KB |
testcase_10 | AC | 95 ms
54,576 KB |
testcase_11 | AC | 94 ms
52,812 KB |
testcase_12 | AC | 84 ms
52,004 KB |
testcase_13 | AC | 115 ms
54,464 KB |
testcase_14 | AC | 51 ms
50,216 KB |
testcase_15 | AC | 134 ms
55,032 KB |
testcase_16 | AC | 99 ms
55,012 KB |
testcase_17 | AC | 101 ms
54,500 KB |
testcase_18 | AC | 105 ms
58,888 KB |
testcase_19 | AC | 145 ms
54,992 KB |
testcase_20 | AC | 144 ms
55,216 KB |
testcase_21 | AC | 140 ms
55,116 KB |
ソースコード
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int left = sc.nextInt(); int right = sc.nextInt(); UnionFindTree uft = new UnionFindTree(right - left + 1); boolean[] used = new boolean[right + 1]; for (int i = left; i <= right; i++) { if (used[i]) { continue; } for (int j = 2; j * i <= right; j++) { used[i * j] = true; 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 (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); } } public int getCount() { HashSet<Integer> set = new HashSet<>(); for (int i = 0; i < parents.length; i++) { set.add(find(i)); } return set.size(); } } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public String next() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }