結果

問題 No.1059 素敵な集合
ユーザー htensaihtensai
提出日時 2020-05-26 11:38:31
言語 Java21
(openjdk 21)
結果
AC  
実行時間 261 ms / 2,000 ms
コード長 1,324 bytes
コンパイル時間 3,094 ms
コンパイル使用メモリ 75,032 KB
実行使用メモリ 63,392 KB
最終ジャッジ日時 2023-09-30 15:38:15
合計ジャッジ時間 7,471 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
55,860 KB
testcase_01 AC 128 ms
55,916 KB
testcase_02 AC 201 ms
58,116 KB
testcase_03 AC 129 ms
55,828 KB
testcase_04 AC 131 ms
55,384 KB
testcase_05 AC 129 ms
55,636 KB
testcase_06 AC 177 ms
57,496 KB
testcase_07 AC 176 ms
58,100 KB
testcase_08 AC 197 ms
57,544 KB
testcase_09 AC 159 ms
55,684 KB
testcase_10 AC 197 ms
57,748 KB
testcase_11 AC 187 ms
57,740 KB
testcase_12 AC 169 ms
55,768 KB
testcase_13 AC 203 ms
58,188 KB
testcase_14 AC 154 ms
55,740 KB
testcase_15 AC 228 ms
58,860 KB
testcase_16 AC 179 ms
57,976 KB
testcase_17 AC 191 ms
57,784 KB
testcase_18 AC 186 ms
62,488 KB
testcase_19 AC 257 ms
63,392 KB
testcase_20 AC 261 ms
60,832 KB
testcase_21 AC 240 ms
58,784 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();
		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();
	    }
	}
}
0