結果

問題 No.1059 素敵な集合
ユーザー ShibuyapShibuyap
提出日時 2020-05-22 21:53:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 1,634 bytes
コンパイル時間 1,837 ms
コンパイル使用メモリ 199,780 KB
実行使用メモリ 5,964 KB
最終ジャッジ日時 2023-09-30 15:28:00
合計ジャッジ時間 2,779 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,728 KB
testcase_01 AC 12 ms
5,704 KB
testcase_02 AC 4 ms
5,696 KB
testcase_03 AC 3 ms
5,736 KB
testcase_04 AC 4 ms
5,712 KB
testcase_05 AC 3 ms
5,700 KB
testcase_06 AC 4 ms
5,808 KB
testcase_07 AC 4 ms
5,752 KB
testcase_08 AC 4 ms
5,704 KB
testcase_09 AC 3 ms
5,752 KB
testcase_10 AC 5 ms
5,676 KB
testcase_11 AC 4 ms
5,676 KB
testcase_12 AC 3 ms
5,708 KB
testcase_13 AC 5 ms
5,732 KB
testcase_14 AC 3 ms
5,664 KB
testcase_15 AC 7 ms
5,956 KB
testcase_16 AC 4 ms
5,768 KB
testcase_17 AC 4 ms
5,888 KB
testcase_18 AC 3 ms
5,728 KB
testcase_19 AC 12 ms
5,700 KB
testcase_20 AC 12 ms
5,964 KB
testcase_21 AC 7 ms
5,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}

const int MAX_N = 200100;
int par[MAX_N]; // 親
int rank_[MAX_N]; // 木の深さ

// n要素で初期化
void UFinit(){
    for(int i=0;i<MAX_N;i++){
        par[i] = i;
        rank_[i] = 0;
    }
}

// 木の根を求める
int find(int x){
    if(par[x] == x){
        return x;
    }else{
        return par[x] = find(par[x]);
    }
}

// xとyの属する集合を併合
void unite(int x, int y){
    x = find(x);
    y = find(y);
    if(x == y) return;

    if(rank_[x] < rank_[y]){
        par[x] = y;
    }else{
        par[y] = x;
        if(rank_[x] == rank_[y]) rank_[x]++;
    }
}

// xとyが同じ集合に属するか否か
bool same(int x, int y){
    return find(x) == find(y);
}



int main() {
    int l, r;
    cin >> l >> r;

    UFinit();

    int cnt = 0;
    int f[MAX_N] = {};
    srep(i,l,r+1){
        f[i] = 1;
        cnt++;
    }

    srep(i,l,r+1){
        int now = i;
        while(now + i <= r){
            now = now + i;
            f[now] = 0;
            unite(i,now);
        }
    }

    int ans = 0;
    /*
    rep(i,MAX_N){
       if(f[i]){
           ans++;
           cout << i << ' ';
       }
    }
    cout << endl;
    */

    int g[MAX_N] = {};

    srep(i,l,r+1){
        g[find(i)] = 1;
    }

    rep(i,MAX_N){
        ans += g[i];
    }

    ans--;
    cout << ans << endl;
    return 0;
}
 
 
0