結果

問題 No.2756 GCD Teleporter
ユーザー AerenAeren
提出日時 2024-05-10 22:24:00
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 98 ms / 2,000 ms
コード長 5,015 bytes
コンパイル時間 3,356 ms
コンパイル使用メモリ 261,548 KB
実行使用メモリ 24,424 KB
最終ジャッジ日時 2024-12-20 06:10:00
合計ジャッジ時間 6,702 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
8,904 KB
testcase_01 AC 7 ms
8,960 KB
testcase_02 AC 7 ms
8,848 KB
testcase_03 AC 7 ms
8,904 KB
testcase_04 AC 11 ms
9,688 KB
testcase_05 AC 28 ms
12,784 KB
testcase_06 AC 69 ms
19,716 KB
testcase_07 AC 56 ms
17,452 KB
testcase_08 AC 55 ms
16,788 KB
testcase_09 AC 46 ms
15,584 KB
testcase_10 AC 67 ms
18,728 KB
testcase_11 AC 80 ms
21,344 KB
testcase_12 AC 22 ms
11,432 KB
testcase_13 AC 93 ms
21,876 KB
testcase_14 AC 87 ms
20,864 KB
testcase_15 AC 20 ms
11,464 KB
testcase_16 AC 43 ms
15,528 KB
testcase_17 AC 55 ms
16,988 KB
testcase_18 AC 21 ms
11,360 KB
testcase_19 AC 98 ms
22,216 KB
testcase_20 AC 68 ms
21,192 KB
testcase_21 AC 34 ms
14,308 KB
testcase_22 AC 64 ms
21,560 KB
testcase_23 AC 14 ms
10,288 KB
testcase_24 AC 53 ms
18,616 KB
testcase_25 AC 58 ms
19,460 KB
testcase_26 AC 64 ms
21,096 KB
testcase_27 AC 67 ms
21,844 KB
testcase_28 AC 45 ms
15,412 KB
testcase_29 AC 75 ms
18,776 KB
testcase_30 AC 93 ms
21,904 KB
testcase_31 AC 39 ms
14,300 KB
testcase_32 AC 48 ms
21,036 KB
testcase_33 AC 24 ms
13,456 KB
testcase_34 AC 14 ms
10,492 KB
testcase_35 AC 58 ms
21,428 KB
testcase_36 AC 57 ms
21,252 KB
testcase_37 AC 7 ms
8,904 KB
testcase_38 AC 74 ms
22,032 KB
testcase_39 AC 80 ms
24,424 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