use erato::Sieve; use std::io::{stdin, BufRead}; fn main() { let stdin = stdin(); let mut stdin = stdin.lock().lines().map(Result::unwrap); let n = stdin.next().unwrap().parse::().unwrap(); let mut dp = vec![None::; n + 1]; dp[0] = Some(0); for p in Sieve::new() .prime_numbers::() .take_while(|&p| p <= n) { for i in (0..=n - p).rev() { change_max!(&mut dp[i + p], dp[i].map(|x| x + 1)); } } let ans = dp[n].unwrap_or(-1); println!("{}", ans); } #[macro_export] macro_rules! change_max { ($target:expr, $src:expr) => { let src = $src; let target = $target; $crate::ordtools::Ordtools::change_max(target, src); }; } // erato {{{ #[allow(dead_code)] mod erato { mod converters { use { super::{Int, PrimeFactorsByLookup, PrimeFactorsByTrialDivision}, std::{iter::Peekable, marker::PhantomData}, }; pub trait PrimeFactors: Sized + Iterator { fn unique(self) -> Unique { Unique { iter: self, prev: None, } } fn rle(self) -> Rle { Rle { iter: self.peekable(), _marker: PhantomData, } } } impl<'a, T: Int> PrimeFactors for PrimeFactorsByTrialDivision<'a, T> {} impl<'a, T: Int> PrimeFactors for PrimeFactorsByLookup<'a, T> {} pub struct Unique> { iter: P, prev: Option, } impl> Iterator for Unique { type Item = P::Item; fn next(&mut self) -> Option { let prev = self.prev; let res = self.iter.find(|&p| Some(p) != prev); self.prev = res; res } } pub struct Rle> { iter: Peekable

, _marker: PhantomData, } impl> Iterator for Rle { type Item = (P::Item, usize); fn next(&mut self) -> Option { if let Some(p) = self.iter.next() { let mut multi = 1; while self.iter.peek() == Some(&p) { multi += 1; self.iter.next(); } Some((p, multi)) } else { None } } } } mod int { use std::{ fmt::Debug, ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Sub, SubAssign}, }; pub trait Int: Debug + Copy + Ord + Add + AddAssign + Sub + SubAssign + Mul + MulAssign + Div + DivAssign + Rem + RemAssign { fn zero() -> Self; fn one() -> Self; fn two() -> Self; fn as_usize(self) -> usize; fn from_usize(src: usize) -> Self; } macro_rules! impl_int { ($($t:ty),* $(,)?) => {$( impl Int for $t { fn zero() -> Self { 0 } fn one() -> Self { 1 } fn two() -> Self { 2 } fn as_usize(self) -> usize { self as usize } fn from_usize(src: usize) -> Self { src as Self } } )*} } impl_int! { usize, u8, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128, } } mod lpd_sieve { use super::{ sieve_base::{PrimeFactorsByLookup, PrimeNumbers}, sieve_kind, Int, SieveBase, }; #[derive(Default, Debug, Clone, PartialEq)] pub struct LpdSieve { base: SieveBase, } impl LpdSieve { pub fn new() -> Self { Self { base: SieveBase::new(), } } pub fn is_empty(&self) -> bool { self.base.is_empty() } pub fn len(&self) -> usize { self.base.len() } pub fn with_len(n: usize) -> Self { Self { base: SieveBase::with_len(n), } } pub fn is_prime(&mut self, x: T) -> bool { self.base.is_prime(x) } pub fn lpd(&mut self, x: T) -> T { self.base.lpd(x) } pub fn prime_numbers(&mut self) -> PrimeNumbers { self.base.prime_numbers() } pub fn prime_factors(&mut self, n: T) -> PrimeFactorsByLookup { self.base.prime_factors_by_lookup(n) } } } mod sieve { use super::{ sieve_base::{PrimeFactorsByTrialDivision, PrimeNumbers}, sieve_kind, Int, SieveBase, }; #[derive(Default, Debug, Clone, PartialEq)] pub struct Sieve { base: SieveBase, } impl Sieve { pub fn new() -> Self { Self { base: SieveBase::new(), } } pub fn is_empty(&self) -> bool { self.base.is_empty() } pub fn len(&self) -> usize { self.base.len() } pub fn with_len(n: usize) -> Self { Self { base: SieveBase::with_len(n), } } pub fn is_prime(&mut self, x: T) -> bool { self.base.is_prime(x) } pub fn prime_numbers(&mut self) -> PrimeNumbers { self.base.prime_numbers() } pub fn prime_factors(&mut self, n: T) -> PrimeFactorsByTrialDivision { self.base.prime_factors_by_trial_division(n) } } } mod sieve_base { use { super::{ sieve_kind::{self, SieveKind}, Int, PrimeFactors, Rle, Unique, }, std::marker::PhantomData, }; #[derive(Debug, Clone, PartialEq)] pub struct SieveBase { sieve: Vec, list: Vec, } impl SieveBase { pub fn new() -> Self { Self { sieve: S::new(), list: Vec::new(), } } pub fn is_empty(&self) -> bool { self.sieve.is_empty() } pub fn len(&self) -> usize { self.sieve.len() } pub fn with_len(n: usize) -> Self { let sieve = S::construct(n); let list = sieve .iter() .enumerate() .filter(|&(index, &b)| S::is_prime(index, b)) .map(|(index, _)| index) .collect(); Self { sieve, list } } pub fn is_prime(&mut self, x: T) -> bool { assert!(T::zero() <= x); let x = x.as_usize(); if self.sieve.len() <= x { *self = Self::with_len(x + 1); } S::is_prime(x, self.sieve[x.as_usize()]) } pub fn prime_numbers(&mut self) -> PrimeNumbers { PrimeNumbers { sieve: self, index: 0, _marker: PhantomData, } } fn extend(&mut self, len: usize) { assert!(2 * self.len() <= len); *self = Self::with_len(len); } } impl Default for SieveBase { fn default() -> Self { Self::new() } } impl SieveBase { pub fn prime_factors_by_trial_division( &mut self, n: T, ) -> PrimeFactorsByTrialDivision { assert!(T::zero() < n); let mut prime_numbers = self.prime_numbers(); PrimeFactorsByTrialDivision { p: prime_numbers.next().unwrap(), prime_numbers, n, } } } pub struct PrimeNumbers<'a, S: SieveKind, T: Int> { sieve: &'a mut SieveBase, index: usize, _marker: PhantomData, } pub struct PrimeFactorsByTrialDivision<'a, T: Int> { prime_numbers: PrimeNumbers<'a, sieve_kind::Boolean, T>, p: T, n: T, } impl<'a, S: SieveKind, T: Int> Iterator for PrimeNumbers<'a, S, T> { type Item = T; fn next(&mut self) -> Option { let Self { sieve, index, .. } = self; let p = if let Some(&p) = sieve.list.get(*index) { T::from_usize(p) } else { sieve.extend((sieve.len() * 2).max(3)); T::from_usize(sieve.list[*index]) }; *index += 1; Some(p) } } impl PrimeFactorsByTrialDivision<'_, T> { pub fn unique(self) -> Unique { PrimeFactors::unique(self) } pub fn rle(self) -> Rle { PrimeFactors::rle(self) } } impl<'a, T: Int> Iterator for PrimeFactorsByTrialDivision<'a, T> { type Item = T; fn next(&mut self) -> Option { let Self { prime_numbers, p, n, } = self; if *n == T::one() { None } else { while *n % *p != T::zero() { if *n <= *p * *p { *p = *n; break; } *p = prime_numbers.next().unwrap(); } *n /= *p; Some(*p) } } } pub struct PrimeFactorsByLookup<'a, T: Int> { sieve: &'a mut SieveBase, n: T, } impl SieveBase { pub fn prime_factors_by_lookup(&mut self, n: T) -> PrimeFactorsByLookup { assert!(T::zero() < n); PrimeFactorsByLookup { sieve: self, n } } pub fn lpd(&mut self, n: T) -> T { let n = n.as_usize(); if self.sieve.len() <= n { self.extend(2 * (n + 1)); } T::from_usize(self.sieve[n]) } } impl PrimeFactorsByLookup<'_, T> { pub fn unique(self) -> Unique { PrimeFactors::unique(self) } pub fn rle(self) -> Rle { PrimeFactors::rle(self) } } impl<'a, T: Int> Iterator for PrimeFactorsByLookup<'a, T> { type Item = T; fn next(&mut self) -> Option { let Self { sieve, n } = self; if *n == T::one() { None } else { let p = sieve.lpd(*n); *n /= p; Some(p) } } } } mod sieve_kind { pub trait SieveKind { type SieveValue: Copy; fn new() -> Vec; fn construct(len: usize) -> Vec; fn is_prime(index: usize, b: Self::SieveValue) -> bool; } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Boolean {} #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Usize {} impl SieveKind for Boolean { type SieveValue = bool; fn new() -> Vec { Vec::new() } fn construct(len: usize) -> Vec { construct_is_prime_table(len) } fn is_prime(_index: usize, b: Self::SieveValue) -> bool { b } } impl SieveKind for Usize { type SieveValue = usize; fn new() -> Vec { Vec::new() } fn construct(len: usize) -> Vec { construct_lpd_table(len) } fn is_prime(index: usize, b: Self::SieveValue) -> bool { index == b } } pub fn construct_is_prime_table(n: usize) -> Vec { let mut is_prime = vec![true; n]; (0..2.min(n)).for_each(|i| is_prime[i] = false); for p in (2..).take_while(|&p| p * p < n) { if !is_prime[p] { continue; } let mut i = p * p; while i < n { is_prime[i] = false; i += p; } } is_prime } fn construct_lpd_table(n: usize) -> Vec { let mut lpd = vec![std::usize::MAX; n]; for p in 2..n { if lpd[p] != std::usize::MAX { continue; } lpd[p] = p; let mut i = p * p; while i < n { if lpd[i] == std::usize::MAX { lpd[i] = p; } i += p; } } lpd } } use sieve_base::SieveBase; pub use { converters::{PrimeFactors, Rle, Unique}, int::Int, lpd_sieve::LpdSieve, sieve::Sieve, sieve_base::{PrimeFactorsByLookup, PrimeFactorsByTrialDivision, PrimeNumbers}, }; } // }}} // ordtools {{{ #[allow(dead_code)] mod ordtools { pub trait Ordtools: PartialOrd + Sized { fn change_min(&mut self, mut rhs: Self) { if self > &mut rhs { *self = rhs; } } fn change_max(&mut self, mut rhs: Self) { if self < &mut rhs { *self = rhs; } } } impl Ordtools for T {} } // }}}