AtCoder Beginner Contest 297 D

https://atcoder.jp/contests/abc297/tasks/abc297_d

GCDを求めるのと同じですね。

// Count Subtractions
#![allow(non_snake_case)]


//////////////////// library ////////////////////

fn read<T: std::str::FromStr>() -> T {
    let mut line = String::new();
    std::io::stdin().read_line(&mut line).ok();
    line.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>().split_whitespace()
            .map(|e| e.parse().ok().unwrap()).collect()
}


//////////////////// process ////////////////////

fn read_input() -> (i64, i64) {
    let v = read_vec();
    (v[0], v[1])
}

fn f(A: i64, B: i64) -> i64 {
    if B == 0 {
        -1
    }
    else {
        let q = A / B;
        let r = A % B;
        q + f(B, r)
    }
}

fn main() {
    let (A, B) = read_input();
    println!("{}", f(A, B))
}