結果

問題 No.1059 素敵な集合
ユーザー hiro71687khiro71687k
提出日時 2023-02-28 19:39:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 565 ms / 2,000 ms
コード長 3,532 bytes
コンパイル時間 4,802 ms
コンパイル使用メモリ 269,404 KB
実行使用メモリ 110,140 KB
最終ジャッジ日時 2023-10-14 02:32:55
合計ジャッジ時間 8,882 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
7,844 KB
testcase_01 AC 565 ms
110,140 KB
testcase_02 AC 46 ms
15,288 KB
testcase_03 AC 26 ms
7,840 KB
testcase_04 AC 26 ms
7,840 KB
testcase_05 AC 25 ms
7,796 KB
testcase_06 AC 44 ms
15,036 KB
testcase_07 AC 44 ms
15,008 KB
testcase_08 AC 53 ms
16,280 KB
testcase_09 AC 30 ms
9,936 KB
testcase_10 AC 81 ms
22,004 KB
testcase_11 AC 47 ms
16,032 KB
testcase_12 AC 34 ms
11,768 KB
testcase_13 AC 70 ms
22,424 KB
testcase_14 AC 26 ms
8,256 KB
testcase_15 AC 127 ms
35,716 KB
testcase_16 AC 48 ms
15,348 KB
testcase_17 AC 45 ms
16,992 KB
testcase_18 AC 37 ms
15,072 KB
testcase_19 AC 474 ms
109,996 KB
testcase_20 AC 455 ms
110,100 KB
testcase_21 AC 111 ms
35,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll=long long;
using ld=long double;
ld pie=3.14159265359;
ll inf=10010010010010010;
ll mod=998244353;
struct Eratosthenes {
    // テーブル
    vector<bool> isprime;

    // 整数 i を割り切る最小の素数
    vector<ll> minfactor;

    vector<ll>mobius;

    // コンストラクタで篩を回す
    Eratosthenes(ll N) : isprime(N+1, true),
                          minfactor(N+1, -1),
                          mobius(N+1,1) {
        // 1 は予めふるい落としておく
        isprime[1] = false;
        minfactor[1] = 1;

        // 篩
        for (ll p = 2; p <= N; ++p) {
            // すでに合成数であるものはスキップする
            if (!isprime[p]) continue;

            // p についての情報更新
            minfactor[p] = p;
            mobius[p]=-1;

            // p 以外の p の倍数から素数ラベルを剥奪
            for (ll q = p * 2; q <= N; q += p) {
                // q は合成数なのでふるい落とす
                isprime[q] = false;

                // q は p で割り切れる旨を更新
                if (minfactor[q] == -1) minfactor[q] = p;
                if ((q / p) % p == 0) mobius[q] = 0;
                else mobius[q] = -mobius[q];
            }
        }
    }

    // 高速素因数分解
    // pair (素因子, 指数) の vector を返す
    vector<pair<ll,ll>> factorize(ll n) {
        vector<pair<ll,ll>> res;
        while (n > 1) {
            ll p = minfactor[n];
            ll exp = 0;

            // n で割り切れる限り割る
            while (minfactor[n] == p) {
                n /= p;
                ++exp;
            }
            res.emplace_back(p, exp);
        }
        return res;
    }
    vector<ll>divisors(ll n){
        vector<ll>res({1});
        auto pf=factorize(n);
        for (auto p : pf)
        {
            ll s=(ll)res.size();
            for (ll i = 0; i < s; i++)
            {
                ll v=1;
                for (ll j = 0; j < p.second; j++)
                {
                    v*=p.first;
                    res.push_back(res[i]*v);
                }
                
            }
            
        }
        return res;
    }  
};
struct unionfind
{
    vector<ll>par,siz;
    unionfind(ll n): par(n,-1),siz(n,1){}
    ll root(ll x){
        if (par[x]==-1)
        {
            return x;
        }else{
            return par[x]=root(par[x]);
        }
    }
    bool issame(ll x,ll y){
        return root(x)==root(y);
    }
    bool unite(ll x,ll y){
        x=root(x);
        y=root(y);
        if (x==y)
        {
            return false;
        }
        if (siz[x]<siz[y])
        {
            swap(x,y);
        }
        par[y]=x;
        siz[x]+=siz[y];
        return true;
    }
    ll size(ll x){
        return siz[root(x)];
    }
};
int main(){
    ll l,r;
    cin >> l >> r;
    Eratosthenes er(300000);
    unionfind uf(r+1);
    vector<pair<ll,pair<ll,ll>>>p;
    for (ll i = l; i <r; i++)
    {
        p.push_back({1,{i,i+1}});
        for (ll j = 2; j*i<=r; j++)
        {
            p.push_back({0,{i,i*j}});
        }
    }
    sort(p.begin(),p.end());
    ll ans=0;
    for (ll i = 0; i < p.size(); i++)
    {
        if (!uf.issame(p[i].second.first,p[i].second.second))
        {
            uf.unite(p[i].second.first,p[i].second.second);
            ans+=p[i].first;
        }
    }
    cout << ans << endl;
}
0