use std::collections::HashMap; const INF: usize = 1usize << 60; 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 val2cnt = a.into_iter().map(|v| { let mut cnt = 1usize; let mut val = v; while val % p == 0 { val /= p; cnt += 1; } (val, cnt) }) .collect::>(); let val2cnt = val2cnt.into_iter().filter(|&(v, _)| v > 1).collect::>(); if val2cnt.is_empty() { println!("-1"); return; } let limit = m / maxval; let mut dp = HashMap::new(); dp.insert(1usize, 0usize); for &(val, cnt) in val2cnt.iter() { let mut next_dp = HashMap::new(); for (&cval, &ccnt) in dp.iter() { for use_cnt in 0.. { let nkey = cval * val.pow(use_cnt as u32); let nkey = if nkey > limit { INF } else { nkey }; let ncnt = cnt + ccnt * use_cnt; let count = *next_dp.get(&nkey).unwrap_or(&INF); if count > ncnt { next_dp.insert(nkey, ncnt); } if nkey == INF { break; } } } dp = next_dp; } println!("{}", dp.get(&INF).unwrap() + 1); }