結果
問題 | No.1059 素敵な集合 |
ユーザー |
|
提出日時 | 2020-05-23 00:48:28 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 12 ms / 2,000 ms |
コード長 | 1,532 bytes |
コンパイル時間 | 1,624 ms |
コンパイル使用メモリ | 171,292 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-23 09:35:48 |
合計ジャッジ時間 | 2,376 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
#include <bits/stdc++.h>using namespace std;using ll = long long;constexpr char newl = '\n';struct UnionFind {//各要素が属する集合の代表(根)を管理する//もし、要素xが根であればdata[x]は負の値を取り、-data[x]はxが属する集合の大きさに等しいvector<int> data;UnionFind(int sz) : data(sz, -1) {}bool unite(int x, int y) {x = find(x);y = find(y);bool is_union = (x != y);if (is_union) {if (data[x] > data[y]) swap(x, y);data[x] += data[y];data[y] = x;}return is_union;}int find(int x) {if (data[x] < 0) { //要素xが根であるreturn x;} else {data[x] = find(data[x]); //data[x]がxの属する集合の根でない場合、根になるよう更新されるreturn data[x];}}bool same(int x, int y) {return find(x) == find(y);}int size(int x) {return -data[find(x)];}};int main() {cin.tie(nullptr);ios::sync_with_stdio(false);int L, R;cin >> L >> R;UnionFind uf(R - L + 1);for (int i = L; i <= R; ++i) {for (int j = 2 * i; j <= R; j += i) {uf.unite(i - L, j - L);}}int ans = 0;for (int i = L; i < R; i++) {if (uf.same(i - L, i + 1 - L)) continue;uf.unite(i - L, i + 1 - L);++ans;}cout << ans << newl;return 0;}