結果

問題 No.1059 素敵な集合
ユーザー ks2mks2m
提出日時 2020-05-22 22:26:07
言語 Java19
(openjdk 21)
結果
AC  
実行時間 186 ms / 2,000 ms
コード長 1,136 bytes
コンパイル時間 2,039 ms
コンパイル使用メモリ 76,460 KB
実行使用メモリ 67,340 KB
最終ジャッジ日時 2023-09-30 15:30:52
合計ジャッジ時間 6,402 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
55,876 KB
testcase_01 AC 183 ms
67,340 KB
testcase_02 AC 163 ms
55,964 KB
testcase_03 AC 126 ms
55,472 KB
testcase_04 AC 127 ms
56,012 KB
testcase_05 AC 127 ms
56,008 KB
testcase_06 AC 145 ms
55,940 KB
testcase_07 AC 145 ms
55,712 KB
testcase_08 AC 150 ms
55,960 KB
testcase_09 AC 132 ms
56,036 KB
testcase_10 AC 149 ms
56,028 KB
testcase_11 AC 150 ms
55,636 KB
testcase_12 AC 141 ms
55,668 KB
testcase_13 AC 155 ms
56,256 KB
testcase_14 AC 126 ms
56,032 KB
testcase_15 AC 161 ms
57,788 KB
testcase_16 AC 145 ms
55,484 KB
testcase_17 AC 146 ms
55,556 KB
testcase_18 AC 139 ms
57,824 KB
testcase_19 AC 181 ms
63,332 KB
testcase_20 AC 186 ms
62,172 KB
testcase_21 AC 169 ms
58,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int l = sc.nextInt();
		int r = sc.nextInt();
		sc.close();

		UnionFind uf = new UnionFind(r + 1);
		for (int i = l; i <= r; i++) {
			for (int j = 2; i * j <= r; j++) {
				uf.union(i, i * j);
			}
		}
		System.out.println(uf.num - l - 1);
	}

	static class UnionFind {
		int[] parent, size;
		int num = 0; // 連結成分の数

		UnionFind(int n) {
			parent = new int[n];
			size = new int[n];
			num = n;
			for (int i = 0; i < n; i++) {
				parent[i] = i;
				size[i] = 1;
			}
		}

		void union(int x, int y) {
			int px = find(x);
			int py = find(y);
			if (px != py) {
				parent[px] = py;
				size[py] += size[px];
				num--;
			}
		}

		int find(int x) {
			if (parent[x] == x) {
				return x;
			}
			parent[x] = find(parent[x]);
			return parent[x];
		}

		/**
		 * xとyが同一連結成分か
		 */
		boolean same(int x, int y) {
			return find(x) == find(y);
		}

		/**
		 * xを含む連結成分のサイズ
		 */
		int size(int x) {
			return size[find(x)];
		}
	}
}
0