アルゴリズムと数学 007

https://atcoder.jp/contests/math-and-algorithm/tasks/math_and_algorithm_g

包除原理ですね。これを使うために最小公倍数が要ります。

// Number of Multiples 1
#![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()
}

fn gcd(n: i32, m: i32) -> i32 {
    if m == 0 {
        n
    }
    else {
        gcd(m, n % m)
    }
}

fn lcm(n: i32, m: i32) -> i32 {
    n / gcd(n, m) * m
}


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

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

fn f(N: i32, X: i32, Y: i32) -> i32 {
    let D = lcm(X, Y);
    N / X + N / Y - N / D
}

fn main() {
    let (N, X, Y) = read_input();
    println!("{}", f(N, X, Y))
}