結果

問題 No.1059 素敵な集合
ユーザー anagohirameanagohirame
提出日時 2020-05-23 00:21:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 1,139 bytes
コンパイル時間 1,654 ms
コンパイル使用メモリ 169,284 KB
実行使用メモリ 8,116 KB
最終ジャッジ日時 2023-09-30 15:35:35
合計ジャッジ時間 2,756 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 16 ms
8,116 KB
testcase_02 AC 5 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 5 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 8 ms
4,484 KB
testcase_11 AC 5 ms
4,376 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 8 ms
4,608 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 15 ms
5,636 KB
testcase_16 AC 5 ms
4,376 KB
testcase_17 AC 4 ms
4,376 KB
testcase_18 AC 3 ms
4,640 KB
testcase_19 AC 29 ms
7,012 KB
testcase_20 AC 29 ms
6,868 KB
testcase_21 AC 14 ms
5,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

class UnionFind {
	public:
		vector<ll> table, num;

		UnionFind(){}
		UnionFind(int size){
			table.resize(size, -1);
			num.resize(size, 1);
		}

		int find(int x){
			if (table[x] < 0) return x;
			else {
				table[x] = find(table[x]);
				return table[x];
			}
		}

		bool same(int x, int y) {
			return find(x) == find(y);
		}

		bool is_root(int x){
			if (table[x] < 0) return true;
			else return false;
		}

		ll get_size(int x){
			return num[find(x)];
		}

		void unite(int x, int y){
			int r1 = find(x);
			int r2 = find(y);
			if (r1 != r2) {
				if (table[r1] > table[r2]){
					table[r2] = r1;
					table[r1]--;
					num[r1] += num[r2];
					num[r2]--;
				} else {
					table[r1] = r2;
					table[r2]--;
					num[r2] += num[r1];
					num[r1]--;
				}
			}
			return;
		}
};

int main() {
	int l, r; cin >> l >> r;
	UnionFind uf(r-l+1);
	for (int i = l; i < r+1; ++i) {
		for (int j = 2*i; j < r+1; j += i) {
			uf.unite(i-l, j-l);
		}
	}
	int ans = -1;
	for (int i = 0; i < r-l+1; ++i) {
		if (uf.is_root(i)) ans++;
	}
	cout << ans << endl;
	return 0;
}
0