結果

問題 No.2756 GCD Teleporter
ユーザー AerenAeren
提出日時 2024-05-10 22:24:00
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 78 ms / 2,000 ms
コード長 5,015 bytes
コンパイル時間 2,761 ms
コンパイル使用メモリ 259,496 KB
実行使用メモリ 24,400 KB
最終ジャッジ日時 2024-05-10 22:24:16
合計ジャッジ時間 5,694 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
8,740 KB
testcase_01 AC 5 ms
8,872 KB
testcase_02 AC 6 ms
8,796 KB
testcase_03 AC 6 ms
8,896 KB
testcase_04 AC 10 ms
9,600 KB
testcase_05 AC 23 ms
12,688 KB
testcase_06 AC 61 ms
19,616 KB
testcase_07 AC 50 ms
17,488 KB
testcase_08 AC 45 ms
16,744 KB
testcase_09 AC 38 ms
15,644 KB
testcase_10 AC 55 ms
18,728 KB
testcase_11 AC 67 ms
21,444 KB
testcase_12 AC 16 ms
11,364 KB
testcase_13 AC 72 ms
21,908 KB
testcase_14 AC 66 ms
20,964 KB
testcase_15 AC 17 ms
11,560 KB
testcase_16 AC 52 ms
15,360 KB
testcase_17 AC 45 ms
16,968 KB
testcase_18 AC 19 ms
11,288 KB
testcase_19 AC 71 ms
22,192 KB
testcase_20 AC 53 ms
21,100 KB
testcase_21 AC 27 ms
14,412 KB
testcase_22 AC 56 ms
21,592 KB
testcase_23 AC 13 ms
10,208 KB
testcase_24 AC 44 ms
18,588 KB
testcase_25 AC 47 ms
19,332 KB
testcase_26 AC 54 ms
21,104 KB
testcase_27 AC 56 ms
21,684 KB
testcase_28 AC 36 ms
15,504 KB
testcase_29 AC 56 ms
18,872 KB
testcase_30 AC 78 ms
21,952 KB
testcase_31 AC 31 ms
14,208 KB
testcase_32 AC 42 ms
20,940 KB
testcase_33 AC 18 ms
13,448 KB
testcase_34 AC 12 ms
10,436 KB
testcase_35 AC 49 ms
21,464 KB
testcase_36 AC 49 ms
21,308 KB
testcase_37 AC 6 ms
8,852 KB
testcase_38 AC 68 ms
22,088 KB
testcase_39 AC 73 ms
24,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
// #include <x86intrin.h>
using namespace std;
#if __cplusplus >= 202002L
using namespace numbers;
#endif

struct number_theory{
	int SZ;
	vector<int> lpf, prime;
	number_theory(int SZ): SZ(SZ), lpf(SZ + 1){ // O(SZ)
		lpf[0] = lpf[1] = numeric_limits<int>::max() / 2;
		for(auto i = 2; i <= SZ; ++ i){
			if(!lpf[i]) lpf[i] = i, prime.push_back(i);
			for(auto j = 0; j < (int)prime.size() && prime[j] <= lpf[i] && prime[j] * i <= SZ; ++ j) lpf[prime[j] * i] = prime[j];
		}
	}
	vector<int> precalc_mobius() const{
		vector<int> mobius(SZ + 1, 1);
		for(auto i = 2; i <= SZ; ++ i){
			if(i / lpf[i] % lpf[i]) mobius[i] = -mobius[i / lpf[i]];
			else mobius[i] = 0;
		}
		return mobius;
	}
	vector<int> precalc_phi() const{
		vector<int> phi(SZ + 1, 1);
		for(auto i = 2; i <= SZ; ++ i){
			if(i / lpf[i] % lpf[i]) phi[i] = phi[i / lpf[i]] * (lpf[i] - 1);
			else phi[i] = phi[i / lpf[i]] * lpf[i];
		}
		return phi;
	}
	// Returns {gcd(0, n), ..., gcd(SZ, n)}
	vector<int> precalc_gcd(int n) const{
		vector<int> res(SZ + 1, 1);
		res[0] = n;
		for(auto x = 2; x <= SZ; ++ x) res[x] = n % (lpf[x] * res[x / lpf[x]]) ? res[x / lpf[x]] : lpf[x] * res[x / lpf[x]];
		return res;
	}
	bool is_prime(int x) const{
		assert(0 <= x && x <= SZ);
		return lpf[x] == x;
	}
	int mu_large(long long x) const{ // O(sqrt(x))
		int res = 1;
		for(auto i = 2LL; i * i <= x; ++ i) if(x % i == 0){
			if(x / i % i) return 0;
			x /= i, res = -res;
		}
		if(x > 1) res = -res;
		return res;
	}
	long long phi_large(long long x) const{ // O(sqrt(x))
		long long res = x;
		for(auto i = 2LL; i * i <= x; ++ i) if(x % i == 0){
			while(x % i == 0) x /= i;
			res -= res / i;
		}
		if(x > 1) res -= res / x;
		return res;
	}
	// returns an array is_prime of length high-low where is_prime[i] = [low+i is a prime]
	vector<int> sieve(long long low, long long high) const{
		assert(high - 1 <= 1LL * SZ * SZ);
		vector<int> is_prime(high - low, true);
		for(auto p: prime) for(auto x = max(1LL * p, (low + p - 1) / p) * p; x < high; x += p) is_prime[x - low] = false;
		for(auto x = 1; x >= low; -- x) is_prime[x - low] = false;
		return is_prime;
	}
};

template<bool Enable_small_to_large = true>
struct disjoint_set{
	int n, _group_count;
	vector<int> p;
	vector<list<int>> group;
	disjoint_set(){ }
	disjoint_set(int n): n(n), _group_count(n), p(n, -1), group(n){ assert(n >= 0);
		for(auto i = 0; i < n; ++ i) group[i] = {i};
	}
	int make_set(){
		p.push_back(-1);
		group.push_back(list<int>{n});
		++ _group_count;
		return n ++;
	}
	int root(int u){
		return p[u] < 0 ? u : p[u] = root(p[u]);
	}
	bool share(int a, int b){
		return root(a) == root(b);
	}
	int size(int u){
		return -p[root(u)];
	}
	bool merge(int u, int v){
		u = root(u), v = root(v);
		if(u == v) return false;
		-- _group_count;
		if constexpr(Enable_small_to_large) if(p[u] > p[v]) swap(u, v);
		p[u] += p[v], p[v] = u;
		group[u].splice(group[u].end(), group[v]);
		return true;
	}
	bool merge(int u, int v, auto act){
		u = root(u), v = root(v);
		if(u == v) return false;
		-- _group_count;
		bool swapped = false;
		if constexpr(Enable_small_to_large) if(p[u] > p[v]) swap(u, v), swapped = true;
		act(u, v, swapped);
		p[u] += p[v], p[v] = u;
		group[u].splice(group[u].end(), group[v]);
		return true;
	}
	int group_count() const{
		return _group_count;
	}
	const list<int> &group_of(int u){
		return group[root(u)];
	}
	vector<vector<int>> group_up(){
		vector<vector<int>> g(n);
		for(auto i = 0; i < n; ++ i) g[root(i)].push_back(i);
		g.erase(remove_if(g.begin(), g.end(), [&](auto &s){ return s.empty(); }), g.end());
		return g;
	}
	void clear(){
		_group_count = n;
		fill(p.begin(), p.end(), -1);
		for(auto i = 0; i < n; ++ i) group[i] = {i};
	}
	friend ostream &operator<<(ostream &out, disjoint_set dsu){
		auto gs = dsu.group_up();
		out << "{";
		if(!gs.empty()) for(auto i = 0; i < (int)gs.size(); ++ i){
			out << "{";
			for(auto j = 0; j < (int)gs[i].size(); ++ j){
				out << gs[i][j];
				if(j + 1 < (int)gs[i].size()) out << ", ";
			}
			out << "}";
			if(i + 1 < (int)gs.size()) out << ", ";
		}
		return out << "}";
	}
};

int main(){
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(ios::badbit | ios::failbit);
	number_theory nt{200'000};
	int n;
	cin >> n;
	vector<vector<int>> appear(200'001);
	for(auto i = 0; i < n; ++ i){
		int x;
		cin >> x;
		while(x >= 2){
			int p = nt.lpf[x];
			appear[p].push_back(i);
			while(x % p == 0){
				x /= p;
			}
		}
	}
	disjoint_set dsu(n);
	for(auto p = 2; p <= 200'000; ++ p){
		for(auto i = 0; i < (int)appear[p].size() - 1; ++ i){
			dsu.merge(appear[p][i], appear[p][i + 1]);
		}
	}
	int size = dsu.group_count();
	if(ranges::all_of(appear, [&](auto &a){ return a.empty(); })){
		cout << 2 * size << "\n";
	}
	else if(!appear[2].empty()){
		cout << 2 * (size - 1) << "\n";
	}
	else{
		int p = 3;
		while(appear[p].empty()){
			++ p;
		}
		cout << min(2LL * size, 1LL * p * (size - 1)) << "\n";
	}
	return 0;
}

/*

*/
0