Problem:
Score : 300 points

Problem Statement

You are given four integers A, B, C, and D. Find the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Constraints

1\leq A\leq B\leq 10^{18}

1\leq C,D\leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Sample Input 1

4 9 2 3

Sample Output 1

2

5 and 7 satisfy the condition.

Sample Input 2

10 40 6 8

Sample Output 2

23

Sample Input 3

314159265358979323 846264338327950288 419716939 937510582

Sample Output 3

532105071133627368

Solution in Rust (1.15.1) (judge verdict: Accepted)

//use std::io::{stdout, Write, BufWriter}

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let mut s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    input!{
        a: i64,
        b: i64,
        c: i64,
        d: i64,
    }

    println!("{}",
         (b - a + 1) - multiples(a..b, c) - multiples(a..b, d) + multiples(a..b, lcm(c, d))
    );
}

fn gcd<T>(x: T, y: T) -> T
where T: PartialEq + Copy + std::ops::Rem<Output = T> + std::convert::From<u8>
{
    if y == T::from(0u8) {
        x
    } else {
        gcd(y, x % y)
    }
}

fn lcm<T>(x: T, y: T) -> T
where T: PartialEq
        + Copy
        + std::ops::Rem<Output = T>
        + std::ops::Mul<Output = T>
        + std::ops::Div<Output = T>
        + std::convert::From<u8>
{
    x * y / gcd(x, y)
}

// 区間r(両端含む)に含まれるnの倍数の個数を求める
use std::ops::Range;
fn multiples<T>(r: Range<T>, n: T) -> T
where T: Copy
    + std::ops::Add<Output = T>
    + std::ops::Sub<Output = T>
    + std::ops::Div<Output = T>
    + std::convert::From<u8>
 {
    (r.end / n) - (((r.start + n - T::from(1u8))/ n) - T::from(1u8))
}