結果
| 問題 | No.1059 素敵な集合 |
| コンテスト | |
| ユーザー |
KoD
|
| 提出日時 | 2020-05-22 21:48:48 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 222 ms / 2,000 ms |
| コード長 | 2,508 bytes |
| コンパイル時間 | 908 ms |
| コンパイル使用メモリ | 77,760 KB |
| 最終ジャッジ日時 | 2025-01-10 14:38:53 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
ソースコード
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <numeric>
template <class T, class U>
inline bool chmin(T &lhs, const U &rhs) {
if (lhs > rhs) {
lhs = rhs;
return true;
}
return false;
}
template <class T, class U>
inline bool chmax(T &lhs, const U &rhs) {
if (lhs < rhs) {
lhs = rhs;
return true;
}
return false;
}
// [l, r) from l to r
struct range {
struct itr {
int i;
constexpr itr(int i_): i(i_) { }
constexpr void operator ++ () { ++i; }
constexpr int operator * () const { return i; }
constexpr bool operator != (itr x) const { return i != x.i; }
};
const itr l, r;
constexpr range(int l_, int r_): l(l_), r(std::max(l_, r_)) { }
constexpr itr begin() const { return l; }
constexpr itr end() const { return r; }
};
// [l, r) from r to l
struct revrange {
struct itr {
int i;
constexpr itr(int i_): i(i_) { }
constexpr void operator ++ () { --i; }
constexpr int operator * () const { return i; }
constexpr bool operator != (itr x) const { return i != x.i; }
};
const itr l, r;
constexpr revrange(int l_, int r_): l(l_ - 1), r(std::max(l_, r_) - 1) { }
constexpr itr begin() const { return r; }
constexpr itr end() const { return l; }
};
class union_find {
private:
int component;
std::vector<int> parent;
public:
union_find() = default;
union_find(int size_) { init(size_); }
void init(int size_) {
component = size_;
parent.assign(size_, -1);
}
int count_components() const {
return component;
}
int component_size(int i) {
return -parent[find_parent(i)];
}
bool same_component(int i, int j) {
return find_parent(i) == find_parent(j);
}
int find_parent(int i) {
if (parent[i] < 0) {
return i;
}
else {
return parent[i] = find_parent(parent[i]);
}
}
bool unite(int i, int j) {
i = find_parent(i);
j = find_parent(j);
if (i == j) {
return false;
}
if (parent[i] > parent[j]) {
std::swap(i, j);
}
parent[i] += parent[j];
parent[j] = i;
--component;
return true;
}
};
int main() {
int L, R;
std::cin >> L >> R;
union_find uni(R - L + 1);
for (int i = L; i <= R; ++i) {
for (int x = 1; x * x <= i; ++x) {
if (i % x == 0) {
if (x >= L) uni.unite(i - L, x - L);
if (i / x >= L) uni.unite(i - L, i / x - L);
}
}
}
std::cout << uni.count_components() - 1 << '\n';
return 0;
}
KoD