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 Common Lisp (SBCL 1.1.14) (judge verdict: Time Limit Exceeded)



(defun splitat (c line)
  (labels ((rec (line acc)
				(let ((pos (position-if (lambda(cc) (char= c cc)) line)))
				  (if pos
					  (rec (subseq line (1+ pos)) (cons (subseq line 0 pos) acc))
					(nreverse (cons line acc))))))
	(rec line nil)))
(defun compute-remove(n minn maxn)
  (labels ((rec (acc m)
			 (if (< m minn)
			   (rec acc (+ m n))
			   (if (<= m maxn)
				 (rec (cons m acc) (+ m n))
				 acc))))
	(rec nil n)))
(compile 'compute-remove)
(defun a ()
(let* (
	   (line (read-line nil nil))
	   ;(line "314159265358979323 846264338327950288 419716939 937510582")
	   ;(line "4 9 2 3")
	   (splited (mapcar #'parse-integer (splitat #\space line)))
	   (minn (car splited))
	   (maxn (cadr splited))
	   (c (caddr splited))
	   (d (cadddr splited))
	   (remove0 (compute-remove c minn maxn))
	   (remove1 (compute-remove d minn maxn))
	   (remove2 (reduce (lambda(acc x) (if (null acc)
										 (cons x acc)
										 (if (= (car acc) x)
										   acc
										 (cons x acc)))) (sort (concatenate 'list remove0 remove1) #'<) :initial-value nil))
	   )
  (format t "~A~%" (- (1+ (- maxn minn)) (length remove2)))
	   ;(format t "~A~%" (length (loop for i from (car splited) to (cadr splited) when (and (not (zerop (mod i (caddr splited)))) (not (zerop (mod i (cadddr splited))))) collect i)))
	   ))
(compile 'a)
(a)