結果

問題 No.1059 素敵な集合
ユーザー tentententen
提出日時 2020-08-20 18:18:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 245 ms / 2,000 ms
コード長 1,313 bytes
コンパイル時間 2,788 ms
コンパイル使用メモリ 80,072 KB
実行使用メモリ 62,584 KB
最終ジャッジ日時 2023-09-30 15:40:20
合計ジャッジ時間 7,162 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
55,824 KB
testcase_01 AC 207 ms
55,732 KB
testcase_02 AC 192 ms
58,020 KB
testcase_03 AC 126 ms
55,692 KB
testcase_04 AC 125 ms
55,500 KB
testcase_05 AC 125 ms
55,748 KB
testcase_06 AC 168 ms
57,688 KB
testcase_07 AC 174 ms
57,896 KB
testcase_08 AC 173 ms
58,024 KB
testcase_09 AC 159 ms
55,700 KB
testcase_10 AC 191 ms
58,172 KB
testcase_11 AC 183 ms
57,820 KB
testcase_12 AC 170 ms
57,984 KB
testcase_13 AC 198 ms
58,216 KB
testcase_14 AC 144 ms
55,620 KB
testcase_15 AC 219 ms
58,756 KB
testcase_16 AC 185 ms
57,904 KB
testcase_17 AC 185 ms
57,700 KB
testcase_18 AC 188 ms
62,584 KB
testcase_19 AC 236 ms
58,956 KB
testcase_20 AC 245 ms
58,784 KB
testcase_21 AC 219 ms
58,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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);
            }
        }
    }
} 
0