結果

問題 No.1059 素敵な集合
ユーザー simkarensimkaren
提出日時 2020-05-23 17:40:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 728 ms / 2,000 ms
コード長 1,551 bytes
コンパイル時間 2,272 ms
コンパイル使用メモリ 190,788 KB
実行使用メモリ 8,236 KB
最終ジャッジ日時 2023-09-30 15:37:36
合計ジャッジ時間 8,768 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 721 ms
4,376 KB
testcase_02 AC 195 ms
4,432 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 113 ms
4,384 KB
testcase_07 AC 105 ms
4,376 KB
testcase_08 AC 186 ms
4,380 KB
testcase_09 AC 32 ms
4,376 KB
testcase_10 AC 198 ms
4,380 KB
testcase_11 AC 118 ms
4,380 KB
testcase_12 AC 64 ms
4,376 KB
testcase_13 AC 307 ms
4,380 KB
testcase_14 AC 7 ms
4,376 KB
testcase_15 AC 602 ms
4,580 KB
testcase_16 AC 152 ms
4,380 KB
testcase_17 AC 165 ms
4,380 KB
testcase_18 AC 480 ms
8,236 KB
testcase_19 AC 723 ms
4,376 KB
testcase_20 AC 728 ms
4,380 KB
testcase_21 AC 658 ms
5,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast", "unroll-loops")

// #define TEST

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

using namespace std;

#define ll long long

vector<ll> divisor(ll k){
    vector<ll> d;
    for (ll i = 1;i * i <= k;++i){
        if (k % i == 0){
            d.push_back(i);
            if (i * i != k)
                d.push_back(k / i);
        }
    }
    sort(d.begin(),d.end());
    return d;
}

struct UnionFind{
    vector<int> data;
    /* constructor */
    UnionFind(int sz){ data.assign(sz,-1); }
    /* merge the set to which x belongs and the set to which y belongs */
    bool unite(int x,int y){
        x = find(x); y = find(y);
        if (x == y) return false;
        if (data[x] > data[y]) swap(x, y);
        data[x] += data[y];
        data[y] = x;
        return true;
    }
    /* find the root of k */
    int find(int k){
        if (data[k] < 0) return k;
        return data[k] = find(data[k]);
    }
    /* calculate the size of the set to which k belongs */
    int size(int k){ return -data[find(k)]; }
};

int main(void){
    int L, R;
    cin >> L >> R;
    UnionFind uf(R - L + 1);
    for (int i = 0; i < R - L + 1; ++i){
        int x = L + i;
        auto d = divisor(x);
        for (auto di : d){
            if (di != x && L <= di && di <= R){
                int idx = di - L;
                uf.unite(idx, i);
            }
        }
    }
    set<int> parent;
    for (int i = 0; i < R - L + 1; ++i)
        parent.insert(uf.find(i));
    cout << (parent.size() - 1) << endl;
    return 0;
}
0