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 C (GCC 5.4.1) (judge verdict: Accepted)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

unsigned long gcm(unsigned long _a, unsigned long _b){
        unsigned long a, b;
        unsigned long r=0;
        if(_a<_b){
                a = _b;
                b = _a;
        }
        else{
                a = _a;
                b = _b;
        }
        r = a % b;
        while(r!=0){
                a = b;
                b = r;
                r = a % b;
        }
        return b;
}

unsigned long lcm(unsigned long a, unsigned long b){
        return (a*b)/gcm(a, b);
}

long count_multiple(long a, long b, long c){
        long ret = 0;
        ret = b/c - (a-1)/c;
#ifdef DEBUG2
        printf("count_multiple: a=%ld, b=%ld, c=%ld, ret=%ld\n", a, b, c, ret);
#endif
        return ret;
}

int main(int argc, char* argv[]){
        char buf[64]={0};
        char* p=NULL;
        long A = 0;
        long B = 0;
        long C = 0;
        long D = 0;
        long ret = 0;

        if(fgets(buf, sizeof(buf)-1, stdin)==NULL){
                return 1;
        }

        p = strtok(buf, " ");
        A = atol(p);
        if(A<0){return 1;}

        p = strtok(NULL, " ");
        B = atol(p);
        if(B<0){return 2;}

        p = strtok(NULL, " ");
        C = atol(p);
        if(C<0){return 3;}

        p = strtok(NULL, " ");
        D = atol(p);
        if(D<0){return 4;}

#ifdef DEBUG1
        printf("input: A=%ld, B=%ld, C=%ld, D=%ld\n", A, B, C, D);
#endif
        ret = B-A+1;
        ret -= count_multiple(A, B, C);
        ret -= count_multiple(A, B, D);
        ret += count_multiple(A, B, lcm(C, D));

        printf("%ld\n", ret);
        return 0;
}