use std::collections::{HashMap, VecDeque, HashSet}; fn costmap(val: usize, p: usize) -> (usize, usize) { let mut cnt = 1usize; let mut val = val; while val % p == 0 { cnt += 1; val /= p; } (val, cnt) } fn main() { let mut nmp = String::new(); std::io::stdin().read_line(&mut nmp).ok(); let nmp: Vec = nmp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let n = nmp[0]; let m = nmp[1]; let p = nmp[2]; let mut a = String::new(); std::io::stdin().read_line(&mut a).ok(); let a: Vec = a.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let maxval = *a.iter().max().unwrap(); if maxval >= m { println!("1"); return; } let mut paths = a.iter().map(|&v| costmap(v, p)).filter(|&v| v.0 > 1).collect::>(); if paths.is_empty() { println!("-1"); return; } paths.sort(); paths.reverse(); let mut npaths = HashMap::new(); for &(val, cnt) in paths.iter() { npaths.insert(val, cnt); } let mut result = 1usize << 60; let mut dp = HashMap::new(); dp.insert(maxval, 1usize); let mut deque = VecDeque::new(); deque.push_back((maxval, 1usize)); while let Some((val, cnt)) = deque.pop_front() { let mut updated_keys = HashSet::new(); for (&aval, &acnt) in npaths.iter() { if let Some(&ccnt) = dp.get(&(val + aval)) { if ccnt > cnt + acnt && val + aval < m { dp.insert(val*aval, cnt+acnt); updated_keys.insert(val*aval); } } else if val*aval < m { dp.insert(val*aval, cnt+acnt); updated_keys.insert(val*aval); } if val * aval >= m { result = result.min(cnt+acnt); } } for v in updated_keys.iter() { deque.push_back((*v, *dp.get(v).unwrap())); } } println!("{}", result); }