結果

問題 No.1059 素敵な集合
ユーザー scol_kpscol_kp
提出日時 2020-05-22 22:40:27
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 20 ms / 2,000 ms
コード長 1,872 bytes
コンパイル時間 2,151 ms
コンパイル使用メモリ 207,272 KB
実行使用メモリ 9,224 KB
最終ジャッジ日時 2023-09-30 15:31:56
合計ジャッジ時間 3,171 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 19 ms
6,072 KB
testcase_02 AC 8 ms
4,848 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 6 ms
4,376 KB
testcase_07 AC 6 ms
4,376 KB
testcase_08 AC 8 ms
4,900 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 9 ms
4,640 KB
testcase_11 AC 6 ms
4,448 KB
testcase_12 AC 5 ms
4,380 KB
testcase_13 AC 11 ms
5,396 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 18 ms
6,540 KB
testcase_16 AC 8 ms
4,744 KB
testcase_17 AC 7 ms
4,656 KB
testcase_18 AC 15 ms
9,224 KB
testcase_19 AC 20 ms
6,520 KB
testcase_20 AC 20 ms
6,796 KB
testcase_21 AC 18 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define FASTIO
using namespace std;

using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

class UnionFind {
    using i64 = std::int64_t;

private:
    std::vector<i64> par;
    std::vector<i64> sz;

public:
    UnionFind(i64 N) : par(N), sz(N, 1) {
        for (i64 i = 0; i < N; ++i) par[i] = i;
    }
    i64 root(i64 x) {
        return par[x] == x ? x : par[x] = root(par[x]);
    }
    bool same(i64 x, i64 y) {
        return root(x) == root(y);
    }
    void unite(i64 x, i64 y) {
        x = root(x);
        y = root(y);
        if (x == y) return;
        if (sz[x] < sz[y]) std::swap(x, y);
        par[y] = x;
        sz[x] += sz[y];
    }
    i64 size(i64 x) {
        return sz[root(x)];
    }
    void reset(i64 N) {
        par.resize(N);
        for (i64 i = 0; i < N; ++i) par[i] = i;
        sz.assign(N, 1);
    }
};

void solve() {
    ll L, R;
    cin >> L >> R;

    UnionFind uf(R - L + 1);
    for (ll i = L; i <= R; i++) {
        for (ll j = 2 * i; j <= R; j += i) {
            uf.unite(i - L, j - L);
        }
    }

    unordered_set<ll> st;
    for (ll i = 0; i < R - L + 1; i++) {
        st.emplace(uf.root(i));
    }

    cout << (ll)st.size() - 1 << endl;
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int main() {
#ifdef FASTIO
    cin.tie(0), cout.tie(0);
    ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
    ifstream ifs("./in_out/input.txt");
    cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
    ofstream ofs("./in_out/output.txt");
    cout.rdbuf(ofs.rdbuf());
#endif
    solve();
    cout << flush;
    return 0;
}
0