結果

問題 No.1059 素敵な集合
ユーザー pockynypockyny
提出日時 2020-05-22 21:42:03
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 13 ms / 2,000 ms
コード長 684 bytes
コンパイル時間 1,099 ms
コンパイル使用メモリ 72,784 KB
実行使用メモリ 4,952 KB
最終ジャッジ日時 2023-09-30 15:26:16
合計ジャッジ時間 1,784 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 12 ms
4,920 KB
testcase_02 AC 4 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 5 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 8 ms
4,848 KB
testcase_16 AC 4 ms
4,380 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 3 ms
4,876 KB
testcase_19 AC 12 ms
4,952 KB
testcase_20 AC 13 ms
4,832 KB
testcase_21 AC 8 ms
4,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;
int par[200010],sz[200010];
void init(int n){
	for(int i=0;i<n;i++){
		par[i] = i; sz[i] = 1;
	}
}

int find(int x){
	if(par[x]==x) return x;
	return par[x] = find(par[x]);
}

void unite(int x, int y){
	x = find(x); y = find(y);
	if(x==y) return;
	if(sz[x]>sz[y]) swap(x,y);
	par[x] = y;
	sz[y] += sz[x];
}

bool same(int x, int y){
	return find(x)==find(y);
}

int main(){
    int i,j,l,r;
    cin >> l >> r;
    int ans = 0;
    init(r + 1);
    for(i=l;i<=r;i++){
        for(j=i;j<=r;j+=i){
            unite(j,i);
        }
    }
    for(i=l;i<=r;i++){
        if(find(i)==i) ans++;
    }
    cout << ans - 1 << endl;
}
0