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)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
use libprocon::*;

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

    // a以上b以下の整数は a-b+1 個
    // そのうち、cの倍数であるもの、dの倍数であるもの、LCM(c, d)の倍数であるもの、の個数を求めて、
    // いい感じに集合の演算をすればおｋ
    let cc = count(a, b, c);
    let dd = count(a, b, d);
    let ccdd = count(a, b, lcm(c as u64, d as u64) as usize);
    println!("{}", b - a + 1 - cc - dd + ccdd);
}

// a以上b以下の整数のうち、mの倍数であるものの個数を二分探索でカウントする
fn count(a: usize, b: usize, m: usize) -> usize {
    dbg!((a, b, m));
    if m > b {
        return 0;
    }
    let lower = {
        let mut ng: usize = 0;
        let mut ok: usize = (b + 1000) / m;
        while ok - ng > 1 {
            let mid = (ok + ng) / 2;
            let v = mid * m;
            if a <= v {
                ok = mid;
            } else {
                ng = mid;
            }
        }
        ok
    };
    let upper = {
        let mut ok: usize = 0;
        let mut ng: usize = (b / m) + 1;
        while ng - ok > 1 {
            let mid = (ok + ng) / 2;
            let v = mid * m;
            if v <= b {
                ok = mid;
            } else {
                ng = mid;
            }
        }
        ok
    };
    dbg!(upper) - dbg!(lower) + 1
}
*/

fn main() {
    let exe = "/tmp/bin633E3B30";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";