結果
| 問題 |
No.1059 素敵な集合
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-05-22 21:55:10 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 20 ms / 2,000 ms |
| コード長 | 1,603 bytes |
| コンパイル時間 | 1,574 ms |
| コンパイル使用メモリ | 173,328 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-23 09:28:52 |
| 合計ジャッジ時間 | 2,284 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++)
#define all(x) x.begin(),x.end()
template<class T>
static inline std::vector<T> ndvec(size_t&& n, T val) noexcept {
return std::vector<T>(n, std::forward<T>(val));
}
template<class... Tail>
static inline auto ndvec(size_t&& n, Tail&&... tail) noexcept {
return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(n, ndvec(std::forward<Tail>(tail)...));
}
template<class T, class Cond>
struct chain {
Cond cond; chain(Cond cond) : cond(cond) {}
bool operator()(T& a, const T& b) const {
if(cond(a, b)) { a = b; return true; }
return false;
}
};
template<class T, class Cond>
chain<T, Cond> make_chain(Cond cond) { return chain<T, Cond>(cond); }
#include <vector>
#include <tuple>
struct union_find {
std::vector<int> par;
union_find(int N): par(N, -1) {}
int root(int x) {
return par[x] < 0 ? x : par[x] = root(par[x]);
}
std::tuple<int, int> unite(int x, int y) {
x = root(x);
y = root(y);
if(x == y) return { -1, -1 };
if(par[x] > par[y]) std::swap(x, y);
par[x] += par[y];
par[y] = x;
return { x, y };
}
int size(int x) {
return -par[root(x)];
}
};
int main() {
i64 L, R;
cin >> L >> R;
union_find uf(R + 1);
std::vector<i64> cost(R + 1, 1);
for(int i = R + 1; i --> L;) {
for(int j = i + i; j < R + 1; j += i) {
uf.unite(i, j);
}
}
i64 ans = 0;
rep(i,L, R + 1) {
int r = uf.root(i);
ans += cost[r];
cost[r] = 0;
}
std::cout << ans - 1 << '\n';
}