#![allow(unused_imports)] #![allow(non_snake_case)] use std::cmp::*; use std::collections::*; use std::io::Write; #[allow(unused_macros)] macro_rules! debug { ($($e:expr),*) => { #[cfg(debug_assertions)] $({ let (e, mut err) = (stringify!($e), std::io::stderr()); writeln!(err, "{} = {:?}", e, $e).unwrap() })* }; } fn get_primes(n: i64) -> Vec { let mut is_prime = vec![true; n as usize + 1]; let mut primes = Vec::new(); is_prime[0] = false; is_prime[1] = false; for i in 2..n + 1 { if is_prime[i as usize] { primes.push(i); let mut j = 2 * i; while j < n { is_prime[j as usize] = false; j += i; } } } primes } fn factorize(mut num: i64, primes: &Vec) -> std::collections::HashMap { // max_primes >= (num)^(1/2) let mut dict = std::collections::HashMap::new(); for &p in primes.iter() { while num % p == 0 { *dict.entry(p).or_insert(0) += 1; num /= p; } if num == 1 { break; } } if num != 1 { dict.insert(num, 1); } dict } fn collect_all_factors(dict: &std::collections::HashMap) -> Vec { let mut ret = vec![1]; for &key in dict.keys() { let mut new = ret.clone(); for coef in 1..dict[&key] + 1 { let coef_1 = pow(key, coef); for &num in ret.iter() { new.push(num * coef_1); } } ret = new; } ret } fn pow(n: i64, mut p: i64) -> i64 { let mut r = n; let mut ret = 1; while p > 0 { if p % 2 == 0 { r = r * r; p /= 2; } else { ret = ret * r; p -= 1; } } ret } fn main() { let n = read::(); let a = read_vec::(); let a_max = *a.iter().max().unwrap(); let mut i = 0; while i * i <= a_max { i += 1; } let primes = get_primes(i as i64); let mut dp = vec![0; a_max as usize + 1]; dp[a[0] as usize] = 1; for i in 1..n { let dict = factorize(a[i] as i64, &primes); for num in collect_all_factors(&dict) { if num == a[i] { continue; } dp[a[i] as usize] = max(dp[a[i] as usize], dp[num as usize] + 1); } } println!("{}", dp.iter().max().unwrap()); } fn read() -> T { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); s.trim().parse().ok().unwrap() } fn read_vec() -> Vec { read::() .split_whitespace() .map(|e| e.parse().ok().unwrap()) .collect() }