#![allow(unused_imports)] use std::cmp::*; use std::collections::*; use std::io::Write; use std::ops::Bound::*; #[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 main() { let v = read_vec::(); let (n, k, m) = (v[0], v[1], v[2]); let mut ans = 0; let mut x = 1; let mut m2 = m; for i in 0..=50 { if m2 > n { break; } let c1 = count(k, m2); let c2 = count(n, m2); let c3 = count(n - k, m2); let d1 = (0..=50).map(|x| c2[x] - c3[x]).collect::>(); let cc = (1..=50).map(|x| c1[x] * x as i128).sum::(); let dc = (1..=50).map(|x| d1[x] * x as i128).sum::(); if dc > cc { ans = x; } m2 *= m; x += 1; } println!("{}", ans); } fn count(k: i128, m: i128) -> Vec { let mut start = 0; let mut coef = 1; while coef * m <= k { start += 1; coef *= m; } let mut coef2 = coef; let mut start2 = start; let mut count2 = 0; let mut ret = vec![0; 51]; while coef2 >= 1 { let cur_count = k / coef2 - count2; ret[start2 as usize] = cur_count; count2 += cur_count; coef2 /= m; start2 -= 1; } ret } 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() }