結果

問題 No.1059 素敵な集合
ユーザー okok
提出日時 2020-05-22 22:06:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 1,767 bytes
コンパイル時間 801 ms
コンパイル使用メモリ 83,692 KB
実行使用メモリ 9,388 KB
最終ジャッジ日時 2023-09-30 15:29:05
合計ジャッジ時間 2,022 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
9,140 KB
testcase_01 AC 9 ms
9,184 KB
testcase_02 AC 8 ms
9,188 KB
testcase_03 AC 7 ms
9,336 KB
testcase_04 AC 7 ms
9,388 KB
testcase_05 AC 7 ms
9,184 KB
testcase_06 AC 9 ms
9,228 KB
testcase_07 AC 8 ms
9,192 KB
testcase_08 AC 8 ms
9,296 KB
testcase_09 AC 7 ms
9,228 KB
testcase_10 AC 9 ms
9,332 KB
testcase_11 AC 8 ms
9,292 KB
testcase_12 AC 7 ms
9,384 KB
testcase_13 AC 10 ms
9,184 KB
testcase_14 AC 7 ms
9,164 KB
testcase_15 AC 11 ms
9,292 KB
testcase_16 AC 8 ms
9,228 KB
testcase_17 AC 8 ms
9,124 KB
testcase_18 AC 8 ms
9,388 KB
testcase_19 AC 12 ms
9,328 KB
testcase_20 AC 12 ms
9,372 KB
testcase_21 AC 12 ms
9,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007; 

struct fast_io {
	fast_io(){
		std::cin.tie(nullptr);
		std::ios::sync_with_stdio(false);
	};
} fio;


class union_find
{
	int  _setnum;
	vector<int> par, nume;
public:
	union_find(){
	}
	
	union_find(int x){
		par.resize(x);
		nume.resize(x);
		init();
	}
	
	~union_find(){
		//
		
	}
	
	void clear(){
		_setnum = 0;
		par.clear();
		nume.clear();
	}
	
	void init(){
		_setnum = par.size();
		for(int i = 0; i < par.size(); i++){
			par[i] = i;
			nume[i] = 1;
		}
	}
	
	void resize(int x){
		
		par.resize(x);
		nume.resize(x);
		init();
	}

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

	void unite(int x, int y){
		x = find(x);
		y = find(y);
	
		if(x == y)return;
		
		_setnum--;
		
		if(nume[x] > nume[y]) std::swap(x,y);
		
		par[x] = y;
		nume[y] += nume[x];
	}
	
	bool same(int x, int y){
		return find(x) == find(y);
	}
	
	int numel(int x){
		return nume[find(x)];
	}
	
	int size(){
		return par.size();
	}
	
	int setnum(){
		return _setnum;
	}
};



signed main(){
	cout<<fixed<<setprecision(10);
	
	const int MAX = 200010;
	union_find uf(MAX);
	int L, R;
	int con = 0;
	
	vector<int> hoge(MAX, false);
	vector<int> used(MAX, false);
	
	cin>>L>>R;
	
	for(int i = L; i <= R; i++){
		if(hoge[i] == false) {
			for(int j = i * 2; j <= R; j += i){
				hoge[j] = true;
				uf.unite(i, j);
			}
			
		} else {
			
		}
	}
	
	for(int i = L; i <= R; i++){
		if(used[uf.find(i)]) continue;
		used[uf.find(i)] = true;
		con++;
	}
	cout<<con-1<<endl;
	
	
	return 0;
}
0