pub use __cargo_equip::prelude::*; use proconio::input; use std::collections::*; use acl_dsu::*; fn main() { input! { N: usize, A: [usize; N], } let max = *A.iter().max().unwrap(); let (_, sieve) = linear_sieve(max); let mut by_prime_factor = HashMap::new(); for i in 0 .. N { let mut n = A[i]; while n > 1 { let p = sieve[n]; while n % p == 0 { n /= p; } by_prime_factor.entry(p).or_insert_with(Vec::new).push(i); } } let mut dsu = Dsu::new(N); for (_, vertices) in &by_prime_factor { for i in 1 .. vertices.len() { dsu.merge(vertices[0], vertices[i]); } } // let mut min_prime_factor = vec![200000; N]; // let mut leaders = vec![]; // for v in 0 .. N { // let r = dsu.leader(v); // leaders.push(r); // let p = &mut min_prime_factor[r]; // let p0 = *p; // let q = sieve[A[v]]; // if q > 1 { // *p = p0.min(q); // } // } let min_prime_factors = dsu.groups().iter().map(|group| { let mut ans = 1_000_000_000; for &v in group { if A[v] != 1 { ans = ans.min(sieve[A[v]]); } } ans }).collect::>(); // println!("{:?}", min_prime_factors); let n = min_prime_factors.len(); let mut ans = 2 * n; let p = *min_prime_factors.iter().min().unwrap(); ans = ans.min(p * (n - 1)); println!("{}", ans); } fn linear_sieve(limit: usize) -> (Vec, Vec) { let mut primes = vec![]; let mut table = vec![1; limit + 1]; for d in 2 ..= limit { if table[d] == 1 { table[d] = d; primes.push(d); } for &p in &primes { if p * d > limit || p > table[d] { break; } table[p * d] = p; } } (primes, table) } // The following code was expanded by `cargo-equip`. /// # Bundled libraries /// /// - `ac-library-rs-parted-dsu 0.1.0 (path+██████████████████████████████████████████████████████████████████████)` published in https://github.com/qryxip/ac-library-rs-parted licensed under `CC0-1.0` as `crate::__cargo_equip::crates::acl_dsu` #[cfg_attr(any(), rustfmt::skip)] #[allow(unused)] mod __cargo_equip { pub(crate) mod crates { pub mod acl_dsu {pub use self::dsu::*;mod dsu{pub struct Dsu{n:usize,parent_or_size:Vec,}impl Dsu{pub fn new(size:usize)->Self{Self{n:size,parent_or_size:vec![-1;size],}}pub fn merge(&mut self,a:usize,b:usize)->usize{assert!(abool{assert!(ausize{assert!(ausize{assert!(aVec>{let mut leader_buf=vec![0;self.n];let mut group_size=vec![0;self.n];for i in 0..self.n{leader_buf[i]=self.leader(i);group_size[leader_buf[i]]+=1;}let mut result=vec![Vec::new();self.n];for i in 0..self.n{result[i].reserve(group_size[i]);}for i in 0..self.n{result[leader_buf[i]].push(i);}result.into_iter().filter(|x|!x.is_empty()).collect::>>()}}}} } pub(crate) mod macros { pub mod acl_dsu {} } pub(crate) mod prelude {pub use crate::__cargo_equip::crates::*;} mod preludes { pub mod acl_dsu {} } }