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 Kotlin (1.0.0) (judge verdict: Wrong Answer)

import java.util.*

fun main(args: Array<String>) {
    val scanner = Scanner(System.`in`)
    val a = scanner.nextLong()
    val b = scanner.nextLong()
    val c = scanner.nextLong()
    val d = scanner.nextLong()
    var firstC = 0L
    var firstD = 0L
    var firstCD = 0L
    val numC:Long
    val numD:Long
    val numCD:Long
    val gcd = c*d / cal(c, d)
    var divC = a%c
    var divD = a%d
    var divCD = a%gcd
    //for(i in a..b) {
        //if(firstC === 0L && i % c === 0L) firstC = i
        //if(firstD === 0L && i % d === 0L) firstD = i
        //if(firstCD === 0L && i % c === 0L && i % d === 0L) firstCD = i
        //if(firstC !== 0L && firstD !== 0L && firstCD !== 0L) break
    //}
    if(divC === 0L) firstC = a
    else firstC = a+(c-divC)
    if(divD === 0L) firstD = a
    else firstD = a+(d-divD)
    if(a <= gcd) firstCD = gcd
    else firstCD = a+(gcd-divCD)
    if(b < firstC) numC = 0
    else numC = (b-firstC)/c+1
    if(b < firstD) numD = 0 
    else numD = (b-firstD)/d+1
    if(b < firstCD) numCD = 0
    else numCD = (b-firstCD)/gcd+1
    /*println(firstC)
    println(firstD)
    println(firstCD)
    println(numC)
    println(numD)
    println(numCD)*/
    println((b-a+1)-(numC+numD-numCD))
}

fun cal(x:Long, y:Long): Long{
    var x2 = x;
    var y2 = y;
    if(x2 < y2) return cal(y2, x2);
    if(y2 === 0L) return x2;
    return cal(y2, x2 % y2);
}