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



Sample input:
4 9 2 3


Sample output:
2

Submission status: Accepted

Solution code:
import fractions
A,B,C,D=map(int,input().split())
A-=1
f=fractions.gcd(C,D)
f2=C*D//f
print(B-A-(B//C-A//C+B//D-A//D-B//f2+A//f2))

Submission status: Accepted

Solution code:
def gcd(a,b):
	if b == 0:
		return a
	return gcd(b,a % b)

def lcm(a,b):
	return a // gcd(a,b) * b

def candiv(l,u,x):
	return u // x - (l - 1) // x

a,b,c,d = map(int,input().split())
print(b - a + 1 - candiv(a,b,c) - candiv(a,b,d) + candiv(a,b,lcm(c,d)))

Submission status: Accepted

Solution code:
import fractions
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
 
 
a,b,c,d = map(int,input().split())
l = lcm(c,d)
 
def xika(x,v1,v2,vl):
  c1 = x//v1
  c2=x//v2
  c3 = x//vl
  return c1+c2-c3

print((b-a+1)-(xika(b,c,d,l)-xika(a-1,c,d,l)))

Submission status: Accepted

Solution code:
from fractions import gcd
a,b,c,d = (int(x) for x in input().split())
val = gcd(c,d)
lcm = (c*d) // val
div_by_c = (b // c) - (a // c) + (1 if a % c == 0 else 0)
div_by_d = (b // d) - (a // d) + (1 if a % d == 0 else 0)
div_by_lcm = (b // lcm) - (a // lcm) + (1 if a % lcm == 0 else 0)

print(b-a+1 - div_by_c - div_by_d + div_by_lcm)

Submission status: Accepted

Solution code:
A,B,C,D = map(int, input().split())


def gcd(a,b):
  if (a%b==0):
    return(b)
  else:
    return(gcd(b,a%b))

def lcm(m, n):
	if ( ( 0 == m ) or ( 0 == n ) ):
		return 0;
	return ((m // gcd(m, n)) * n); 


ans = (B-A+1)
ans = ans - (B//C - (A-1)//C)
ans = ans - (B//D - (A-1)//D)
lcmnum = lcm(C, D)
ans = ans + (B//lcmnum - (A-1)//lcmnum)
print(ans)

Submission status: Accepted

Solution code:
a,b,c,d = map(int, input().split())
from fractions import gcd
num_c = b // c - (a - 1) // c
num_d = b // d - (a - 1) // d
num_cd = b // (c * d // gcd(c, d)) - ((a-1) // (c * d // gcd(c, d)))
print((b - a + 1) - (num_c + num_d - num_cd))

Submission status: Accepted

Solution code:
import fractions
a, b, c, d = map(int, input().split())
cr = b // c - (a - 1) // c
dr = b // d - (a - 1) // d

cdr = b // ((c * d) // fractions.gcd(c, d)) - (a - 1) // ((c * d) // fractions.gcd(c, d))
print(b - a + 1 - cr - dr + cdr)

Submission status: Accepted

Solution code:
A,B,C,D=map(int,input().split())

# CDの最小公倍数 = CD/CDの最大公約数
def euclid(C,D):
    return C if D==0 else euclid(D,C%D)

E=C*D//euclid(C,D)

ac=A//C
ad=A//D
acd=A//E
bc=B//C
bd=B//D
bcd=B//E

ans=(B-A+1) - (bc-ac+bd-ad-bcd+acd)

if A%C==0 or A%D==0:
    ans-=1
print(ans)

Submission status: Accepted

Solution code:
#a以上b以下の整数のうちcでもdでも割り切れないものの個数を求めてください
#1以上b以下の整数の条件を満たすものから1以上a-1以下のもので条件を満たすものを取り除くようにすればいい
import fractions

a,b,c,d=map(int,input().split())
x=c*d//fractions.gcd(c,d)
n=b-((b//c)+(b//d)-b//(x))
m=(a-1)-((a-1)//c+(a-1)//d-(a-1)//(x))
print(n-m)

Submission status: Accepted

Solution code:
import fractions
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

A,B,C,D=map(int, input().split())

#CとDの最小公倍数
LCM=lcm(C,D)
#A未満でCで割れる個数
Ac=(A-1)//C
#範囲内でCで割れる個数
div_C=B//C-Ac
#A未満でDで割れる個数
Ad=(A-1)//D
#範囲内でDで割れる個数
div_D=B//D-Ad
#A未満でLCMで割れる個数
Alcm=(A-1)//LCM
#範囲内でLCMで割れる個数
div_CD=B//LCM-Alcm
#解答
Ans=(B-A+1)-div_C-div_D+div_CD
print(Ans)

Submission status: Accepted

Solution code:
# 総数-Cで割り切れる個数-Dで割り切れる個数+CでもDでも割り切れる個数


def gcd(x, y):
    return x if y == 0 else gcd(y, x % y)


a, b, c, d = map(int, input().split())

total = b-a+1
sub_c = total//c
if total % c != 0 and (a % c > b % c or (a % c == 0 and b % c == 0)):
    sub_c += 1

sub_d = total//d
if total % d != 0 and (a % d > b % d or (a % d == 0 and b % d == 0)):
    sub_d += 1

cd = c*d//gcd(c, d)
sub_cd = total//cd
if total % cd != 0 and (a % cd > b % cd or (a % cd == 0 and b % cd == 0)):
    sub_cd += 1

ans = total - sub_c - sub_d + sub_cd
print(ans)

Submission status: Accepted

Solution code:
import fractions


a, b, c, d = list(map(int, input().split()))

def lcm(x, y):
    return x * y // fractions.gcd(x, y)

def get_num(x):
    ans = x - x // c - x // d + x // lcm(c, d)
    return ans


print(get_num(b) - get_num(a -1))

Submission status: Accepted

Solution code:
from fractions import gcd
A, B, C, D = map(int, input().split())

def f(x):
    ans = x
    ans -= x // C
    ans -= x // D
    ans += x // (C*D//gcd(C, D))
    return ans

print(f(B) - f(A-1))

Submission status: Accepted

Solution code:
import fractions

def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

a,b,c,d = map(int,input().split())

# cの倍数
if a == 0:
    print(b - (b // c + b // d - b // lcm(c,d)))
else:
    c_wari = b // c - (a-1) // c
    
    d_wari = b // d - (a-1) // d
    
    cd_wari = b // lcm(c,d) - (a-1) // lcm(c,d)
    
    print((b-a+1) - (c_wari+d_wari-cd_wari))

Submission status: Accepted

Solution code:
def gcd(a, b):
    if a < b:
        a, b = b, a
    while a % b != 0:
        a, b = b, a % b
    return b


def lcm(a, b):
    x = a * b // gcd(a, b)
    return x


a, b, c, d = map(int, input().split())
lcm_cd = lcm(c, d)
a -= 1

x = b - ((b // c + b // d) - (b // lcm_cd))
y = a - ((a // c + a // d) - (a // lcm_cd))
print(x - y)

Submission status: Accepted

Solution code:
import fractions

def solve():
    A, B, C, D = map(int, input().split())
    gcd = (C * D) // fractions.gcd(C, D)
    ans_sub = A-1 - ((A-1) // C + (A-1) // D - (A-1) // gcd)
    ans = B - (B // C + B // D - B // gcd) - ans_sub
    print(ans)

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
def gcd(m,n):
    if m < n:
        m,n=n,m
    if n==0:
        return m
    if m % n == 0:
        return n
    else:
        return gcd(n,m%n)

A,B,C,D=map(int,input().split())
X=B-(B//C)-(B//D)+ B//(C*D//gcd(C,D))
Y=(A-1)-((A-1)//C)-((A-1)//D)+(A-1)//(C*D//gcd(C,D))
print(X-Y)

Submission status: Accepted

Solution code:
import fractions

A, B, C, D = map(int, input().split())

# B以下でC, Dで割り切れるものの個数
b_c = B // C
b_d = B // D

# B以下でCでもDでも割り切れるものの個数
b_c_d = B // ((C*D)//fractions.gcd(C,D))

# A未満でC, Dで割り切れるものの個数
a_c = (A-1) // C
a_d = (A-1) // D

# A未満でCでもDでも割り切れるものの個数
a_c_d = (A-1) // ((C*D)//fractions.gcd(C,D))


count = b_c + b_d - b_c_d - (a_c + a_d - a_c_d)

ans = B-A+1 - count
print(int(ans))

Submission status: Accepted

Solution code:
import fractions

a, b, c, d = map(int, input().split())
cdiv = b//c - (a-1)//c
ddiv = b//d - (a-1)//d
e =  c*d // fractions.gcd(c, d)
ediv = b//e - (a-1)//e
count = b-a+1 - (cdiv + ddiv - ediv)
print(count)

Submission status: Accepted

Solution code:
import fractions

a, b, c, d = map(int, input().split())

cd = (c*d) // fractions.gcd(c, d)
am = (a-1)//c + (a-1)//d - (a-1)//cd
bm = b//c + b//d - b//cd

print((b-a+1) - (bm-am))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from fractions import gcd

A,B,C,D = map(int,read().split())

def F(N):
    # N以下でどちらでも割れない
    x = N//C
    x += N//D
    lcm = C*D//gcd(C,D)
    x -= N//lcm
    return N-x

answer = F(B) - F(A-1)
print(answer)

Submission status: Accepted

Solution code:
import fractions
A, B, C, D = map(int, input().split())
CD = int((C*D)/fractions.gcd(C, D))

ac = (A-1)//C
bc = B//C
ad = (A-1)//D
bd = B//D
acd = (A-1)//CD
bcd = B//CD

print(int(B - A + 1 - ((bc - ac) + (bd - ad) - (bcd - acd))))

Submission status: Accepted

Solution code:
a,b,c,d=map(int,input().split())

# Aまでの数で,C,Dの倍数
a_c=(a-1)//c
a_d=(a-1)//d
# Bまでの数で,C,Dの倍数
b_c=b//c
b_d=b//d
# C,Dの最小公倍数
import fractions
cd = (c*d)//fractions.gcd(c,d) 

a_cd = (a-1)//cd
b_cd = b//cd

print((b-a+1)-((b_c-a_c)+(b_d-a_d)-(b_cd-a_cd)))

Submission status: Accepted

Solution code:
import fractions
a,b,c,d = map(int, input().split())

lcm = c*d//fractions.gcd(c, d)
count = b-a+1 - (b//c+b//d-b//lcm - ((a-1)//c+(a-1)//d-(a-1)//lcm))

print(int(count))

Submission status: Accepted

Solution code:
# coding: utf-8
from fractions import gcd
a,b,c,d = map(int,input().split())
lcm = c*d//gcd(c,d)
print((b-a + 1) - (b//c-(a-1)//c) - (b//d-(a-1)//d) + (b//(lcm) - (a-1)//(lcm)))

Submission status: Accepted

Solution code:
A, B, C, D = map(int, input().split())

def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x%y)
        

def cntNum(n, x, y):
    return n - (n//x+n//y-n*gcd(x, y)//(x*y))


if A%C != 0 and A%D:
    print(cntNum(B, C, D) - cntNum(A, C, D) + 1)
else:
    print(cntNum(B, C, D) - cntNum(A, C, D))

Submission status: Accepted

Solution code:
a,b,c,d=map(int, input().split())

import fractions

def lcm(x,y):
	return (x*y)//fractions.gcd(x,y)

lcmcd=lcm(c,d)
divc=b//c-(a-1)//c
divd=b//d-(a-1)//d
divcd=b//lcmcd-(a-1)//lcmcd

print(b-a+1-divc-divd+divcd)

Submission status: Accepted

Solution code:
def gcd(x,y):
    x,y = max(x,y),min(x,y)
    while x != 0:
        y,x = x, y%x
    return y

def solve():
    a,b,c,d = map(int, input().split())
    cc = b//c - (a-1)//c
    dd = b//d - (a-1)//d
    e = c*d//gcd(c,d)
    cd = b//e - (a-1)//e
    print(b-a+1 - cc - dd + cd)

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
a,b,c,d=map(int,input().split())
import fractions
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
a_0=(a-1)//c
a_1=(a-1)//d
a_2=(a-1)//lcm(c,d)
n=a_0+a_1-a_2
#print(n)
b_0=b//c
b_1=b//d
b_2=b//lcm(c,d)
m=b_0+b_1-b_2
#print(m)
#b_1-b_2
print(b-a+1-(m-n))

Submission status: Accepted

Solution code:
import fractions

def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

A, B, C, D = map(int, input().split())

lcm_CD = lcm(C, D)
count = (B // C - (A-1) // C) + (B // D - (A-1) // D) - (B // lcm_CD - (A-1) // lcm_CD)
    
print((B - A + 1) - count)

Submission status: Accepted

Solution code:
def lmn(a,b):
    ans=a*b
    while b>0:
        tmp=b
        b=a%b
        a=tmp
    return ans//a

A,B,C,D=map(int,input().split())
print((B-A+1)-(B//C+B//D-B//lmn(C,D))+((A-1)//C+(A-1)//D-(A-1)//lmn(C,D)))

Submission status: Accepted

Solution code:
A,B,C,D=list(map(int,input().split()))
x=max(C,D)
y=min(C,D)
if x%y==0:
    gcd=C*D//y
else:
    while x%y!=0:
        z=x%y
        x=y
        y=z
    gcd=C*D//z
print(B-A+1-(B//C-(A-1)//C+B//D-(A-1)//D-B//gcd+(A-1)//gcd))

Submission status: Accepted

Solution code:
from fractions import gcd

a, b, c, d = map(int, input().split())

e = c * d // gcd(c, d)
rest = (b//c - (a-1)//c) + (b//d - (a-1)//d) - (b//e - (a-1)//e)
ans = b-a+1 - rest

print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd


def lcm(p, q):
    return p * q // gcd(p, q)


def anti_division():
    a, b, c, d = map(int, input().split())
    numbers = b - a + 1
    cd_gcd = lcm(c, d)
    a_multiples = [a // min(c, d), a // max(c, d)]
    b_multiples = [b // min(c, d), b // max(c, d)]
    gcd_multiples = [a // cd_gcd, b // cd_gcd]
    ans = numbers - ((b_multiples[1] - a_multiples[1]) + (b_multiples[0] - a_multiples[0]) - (
            gcd_multiples[1] - gcd_multiples[0]))
    if a % c == 0:
        ans -= 1
    if a % d == 0:
        ans -= 1
    if a % cd_gcd == 0:
        ans += 1
    print(ans)


if __name__ == '__main__':
    anti_division()

Submission status: Accepted

Solution code:
import fractions
a,b,c,d=map(int,input().split())
x=c*d//fractions.gcd(c,d)
print(b-~-a-(b//c-~-a//c)-(b//d-~-a//d)+b//x-~-a//x)

Submission status: Accepted

Solution code:
A,B,C,D = map(int,input().split())

def gcd(m,n):
	while n!=0:
		m,n = n,m % n
	return m

def lcm(m,n):
	return m * n // gcd(m,n)

CD = lcm(C,D)

supC = B // C
supD = B // D
supCD = B // (CD)

infC = (A-1) // C
infD = (A-1) // D
infCD = (A-1) // (CD)

num = B - A + 1
num = num - supC - supD + supCD + infC + infD - infCD
print(num)

Submission status: Accepted

Solution code:
from fractions import gcd
def lcm(a, b):
  return a * b // gcd (a, b)
A,B,C,D = map(int,input().split())
cnt_c = B // C - (A - 1) // C
cnt_d = B // D - (A - 1) // D
cnt_cd = B // lcm(C,D) - (A - 1) // lcm(C,D)
print(B - A + 1 - (cnt_c + cnt_d - cnt_cd))

Submission status: Accepted

Solution code:
A, B, C, D = map(int,input().split())

# A以上B以下の整数のうち、(CまたはDの倍数)でないものの個数を求める
# CまたはDの倍数の個数 = Cの倍数の個数+Dの倍数の個数-CとDの最小公倍数の倍数の個数
# 途中式でfloatが出てこないようにしないと大きな数の計算時に結果がズレるので注意

def ceil(x,y):
    if x%y == 0:
        return x//y
    else:
        return x//y+1

def n_multiple(x):
    return B//x - ceil(A,x) + 1

def lcd(x,y):
    while y>0:
        x,y = y, x%y
    return(x)

def gcd(x,y):
    return x*y//lcd(x,y)

print(int((B-A+1) - (n_multiple(C)+n_multiple(D)-n_multiple(gcd(C,D)))))

Submission status: Accepted

Solution code:
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

def lcm(a, b):
    return a * b // gcd (a, b)

a,b,c,d = map(int,input().split())

c_m = b//c - (-(-a//c)) +1
d_m = b//d  - (-(-a//d)) + 1
cd = lcm(c,d)
cd_m = b//(cd) - (-(-a//(cd))) + 1

print(b-a+1 -(c_m + (d_m -cd_m)*(d%c != 0)))

Submission status: Accepted

Solution code:
A, B, C, D = map(int, input().split())


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    g = gcd(a, b)

    return a * (b // g)


def f(n):
    return n - n // C - n // D + n // lcm(C, D)


def main():
    print(f(B) - f(A - 1))
    return


main()

Submission status: Accepted

Solution code:
def getDivisableCount(min_bound, max_bound, div):
    return max_bound // div - (min_bound - 1) // div

def LCM(a, b):
    return a * b // GCD(a, b)

def GCD(a, b):
    if a > b:
        a, b = b, a
    while b % a != 0:
        a, b = b % a, a
    return a

if __name__ == '__main__':
    A, B, C, D = [int(i) for i in input().split()]
    ret = (B - A + 1) - getDivisableCount(A, B, C) - getDivisableCount(A, B, D) + getDivisableCount(A, B, LCM(C, D))
    print(ret)

Submission status: Accepted

Solution code:
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    g = gcd(a, b)

    return a * (b // g)


def main():
    A, B, C, D = map(int, input().split())
    c = B // C - (A - 1) // C
    d = B // D - (A - 1) // D
    tmp = lcm(C, D)
    both = B // tmp - (A - 1) // tmp
    print(B - A + 1 - c - d + both)
    return


main()

Submission status: Accepted

Solution code:
import fractions
A, B, C, D = map(int, input().split())
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
print((B-A+1)-(B//C-(A-1)//C)-(B//D-(A-1)//D)+(B//lcm(C,D)-(A-1)//lcm(C,D)))

Submission status: Accepted

Solution code:
# バージョン 3.4.X 以前
import fractions
# バージョン 3.5 以降
# import math


def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)


def main():
    A, B, C, D = map(int, input().split())
    first = B // C - (A - 1) // C
    second = B // D - (A - 1) // D
    tmp = lcm(C, D)
    both = B // tmp - (A - 1) // tmp
    print(B - A + 1 - first - second + both)
    return


main()

Submission status: Accepted

Solution code:
# バージョン 3.4.X 以前
import fractions
# バージョン 3.5 以降
# mport math


def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)


def main():
    A, B, C, D = map(int, input().split())
    first = B // C - A // C
    first += 1 if A % C == 0 else 0
    second = B // D - A // D
    second += 1 if A % D == 0 else 0
    tmp = lcm(C, D)
    both = B // tmp - A // tmp
    both += 1 if A % tmp == 0 else 0
    print(B - A + 1 - first - second + both)
    return


main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
A,B,C,D=map(int,input().split())
A-=1

def gcd(a,b):
    ans=1
    for i in range(1,int(min(a,b)**0.5)+1):
        
        if a%i==0 and b%i==0:
            ans=max(ans,i)
        temp=min(a,b)//i
        if a%temp==0 and b%temp==0:
            ans=max(ans,temp)
    return ans

num1=A-A//C-A//D+(A//(C*D//gcd(C, D)))
num2=B-B//C-B//D+(B//(C*D//gcd(C, D)))

print(num2-num1)

Submission status: Accepted

Solution code:
import fractions
a,b,c,d = map(int, input().split())
cc = b//c - (a-1)//c
dd = b//d - (a-1)//d
gcd = int(c*d / fractions.gcd(c, d))
cd = b//gcd - (a-1)//gcd

print((b-a+1) + cd - cc -dd)

Submission status: Accepted

Solution code:
import fractions
a, b, c, d = map(int,input().split())
print(b - a + 1 - (b // c + b // d - b // ((c * d) // fractions.gcd(c, d)) - ((a-1) // c + (a-1) // d - (a-1) // ((c * d) // fractions.gcd(c, d)))))

Submission status: Accepted

Solution code:
from fractions import gcd
a, b, c, d = map(int, input().split())
e = c * d // gcd(c, d)
tmp = []
for i in (c, d, e):
    if not a % i:
        l = a
    else:
        l = a + (i - (a % i))
    r = b - b % i
    tmp.append((r - l) // i + 1)

print((b-a+1) - tmp[0] - tmp[1] + tmp[2])

Submission status: Accepted

Solution code:
A,B,C,D = map(int,input().split())
def gcd(a, b):
  while b:
    a, b = b, a % b
  return a
E = C * D // gcd(C,D)

dif = B - A + 1
rc = B//C + (-A//C) + 1
rd = B//D + (-A//D) + 1
re = B//E + (-A//E) + 1
x = dif - rc - rd + re
print(x)

Submission status: Accepted

Solution code:
from fractions import gcd

A, B, C, D = map(int, input().split())

def calc(div):
  return B // div - (A - 1) // div

print((B - A + 1) - (calc(C) + calc(D) - calc(C * D // gcd(C, D))))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def get_min_koubai(a, b):
    if a > b:
        tmp = b
        b = a
        a = tmp

    x = a * b
    r = a % b
    while r != 0:
        a = b
        b = r
        r = a % b

    return x // b


def solve(A: int, B: int, C: int, D: int):
    # Bまでの間で
    #  Cで割り切れる物の数
    #  + Dで割り切れるもののかず
    #  - CDの最小公倍数で割り切れる物の数
    min_koubai = get_min_koubai(C, D)
    b1 = B // C
    b2 = B // D
    b3 = B // min_koubai
    b_count = b1 + b2 - b3

    # Aまでの間で
    #  同上
    a1 = (A - 1) // C
    a2 = (A - 1) // D
    a3 = (A - 1) // min_koubai
    a_count = a1 + a2 - a3

    # Bの数 - Aの数
    # B - A - 上の数
    print(B - (A - 1) - (b_count - a_count))
    return


# Generated by 1.1.5 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    D = int(next(tokens))  # type: int
    solve(A, B, C, D)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from fractions import gcd

a, b, c, d = map(int, input().split())
n = b-a+1
lcm = c*d//gcd(c,d)

ac=(a-1)//c
ad=(a-1)//d
alcm=(a-1)//lcm

bc=b//c
bd=b//d
blcm=b//lcm

print(n-((bc-ac)+(bd-ad)-(blcm-alcm)))

Submission status: Accepted

Solution code:
def solve():
    def gcd(x,y):
        while y != 0:
            x , y = y , x % y
        return x

    A,B,C,D = map(int ,input().split())
    A -= 1
    gcd_val = gcd(C , D)
    lcm_val = C * D // gcd_val
    ReduceFromB = B - (B // C + B // D - B // lcm_val)
    ReduceFromA = A - (A // C + A // D - A // lcm_val)
    print(ReduceFromB - ReduceFromA)

solve()

Submission status: Accepted

Solution code:
from fractions import gcd

a, b, c, d = map(int, input().split())
x = b-a+1
l = c//gcd(c, d)*d
ans = (b//c - (a-1)//c) + (b//d - (a-1)//d) - (b//l - (a-1)//l)
print(x-ans)

Submission status: Accepted

Solution code:
# ABC131 C Anti-Division
from fractions import gcd
a,b,c,d = map(int,input().split())
n = b-a+1
cd = (c*d)//gcd(c,d)

a_c = (a-1)//c
a_d = (a-1)//d
a_cd = (a-1)//cd

b_c = b//c
b_d = b//d
b_cd = b//cd

print(n- ((b_c-a_c)+(b_d-a_d)-((b_cd-a_cd))))

Submission status: Accepted

Solution code:
a,b,c,d=map(int,input().split())
def gcd(x,y):
  if x==0:
    return y
  else:return gcd(y%x,x)

def f(a,s,d):
  x=s*d//gcd(s,d)
  return a-a//s-a//d+a//x

print(f(b,c,d)-f(a-1,c,d))

Submission status: Accepted

Solution code:
# 最大公約数GCD(x,y)
def GCD(x,y):
    x,y=max(x,y),min(x,y)
    if x%y==0:
        return y
    else:
        return GCD(y,x%y)

A,B,C,D=map(int,input().split())

L=(C*D)//GCD(C,D)
X=B//C-(A-1)//C
Y=B//D-(A-1)//D
Z=B//L-(A-1)//L
print((B-A+1)-(X+Y-Z))

Submission status: Accepted

Solution code:
import fractions
A,B,C,D = map(int, input().split())
BC = B//C
if A%C == 0:
    AC = A//C-1
else:    
    AC = A//C
BD = B//D
if A%D == 0:
    AD = A//D - 1
else:
    AD = A//D
GCD = C*D//fractions.gcd(C,D)
BB = B//GCD
if A%GCD == 0:
    AA = A//GCD - 1
else:
    AA = A//GCD
print((B-A+1)-(BC-AC)-(BD-AD)+(BB-AA))

Submission status: Accepted

Solution code:
from fractions import gcd


a, b, c, d = map(int, input().split())
lcm = int(c * d / gcd(c, d))
A = a - 1
l_cd = A - A // c - A // d + A // lcm
r_cd = b - b // c - b // d + b // lcm
print(r_cd - l_cd)

Submission status: Accepted

Solution code:
import fractions
def answer(x, i, j):
    lcm = (i * j) // fractions.gcd(i, j)
    xi = x // i
    xj = x // j
    xlcm = x // lcm
    return x - xi - xj + xlcm
a, b, c, d = map(int, input().split())
ans = answer(b, c, d) - answer(a-1, c, d)
print(ans)

Submission status: Accepted

Solution code:
A, B, C, D = map(int, input().split())
def gcd(a, b):
    if a < b:
        a, b = b, a
    a, b = b, a % b
    if b == 0 :
        return a
    else:
        return gcd(a, b)
cd = C * D //gcd(C, D)
print(B - B // C - B // D + B // cd - A + 1 + (A - 1) // C + (A - 1)// D - (A - 1)// cd)

Submission status: Accepted

Solution code:
from fractions import gcd

def get_num(div):
  num = (B)//div - (A-1)//div
  return num

A,B,C,D = map(int,input().split())
print(B-A+1-get_num(C)-get_num(D)+get_num(C*D//gcd(C,D)))

Submission status: Accepted

Solution code:
import fractions
a, b, c, d = map(int, input().split())
cd = c * d // (fractions.gcd(c, d))
s = ((a - 1) // c) + ((a - 1) // d) - ((a - 1) // cd)
e = (b // c) + (b // d) - (b // cd)
print((b - (a - 1)) - (e - s))

Submission status: Accepted

Solution code:
A, B, C, D = map(int, input().split())

def gcd(a, b):
  if b > a:
    a, b = b, a
  a,b = b, a % b
  if b == 0:
    return a
  else:
    return gcd(a, b)
G = C * D // gcd(C, D)

print (B -B // C - B // D + B // G -(A-1)+ (A-1) // C + (A-1) // D - (A-1) // G)

Submission status: Accepted

Solution code:
A,B,C,D = map(int,input().split())
def dev_num(X):
  m = A//X if A%X == 0 else A//X + 1
  n = B//X
  return n-m+1
def gcd(x,y):
  if x>y:
    x,y=y,x
  if y%x == 0:
    return x
  else:
    return gcd(y%x,x)
def lcm(x,y):
  return x*y//gcd(x,y)

print(B-A+1 - dev_num(C) - dev_num(D) + dev_num(lcm(C,D)))

Submission status: Accepted

Solution code:
a, b, c, d = [int(i) for i in input().split()]

def gcd(x, y):
	if x % y == 0:
		return y
	return gcd(y, x % y)
	
def num(x, c, d):
	return x - x // c - x // d + x // (c * d // gcd(c, d))

undera = num(a - 1, c, d)
underb = num(b, c, d)
print(underb - undera)

Submission status: Accepted

Solution code:
from fractions import gcd
import sys
input = sys.stdin.readline


def main():
    A, B, C, D = map(int, input().split())
    
    A_C = (A-1)//C
    A_D = (A-1)//D
    A_CD = (A-1)//((C*D)//gcd(C, D))
    A_nCD = (A-1) - (A_C + A_D - A_CD)

    B_C = B//C
    B_D = B//D
    B_CD = B//((C*D)//gcd(C, D))
    B_nCD = B - (B_C + B_D - B_CD)

    print(B_nCD - A_nCD)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import fractions
a,b,c,d=map(int,input().split())
ans=0
ca=[]
da=[]
cda=[]
minimunc=-(-a//c)
minimund=-(-a//d)
minimuncd=-(-a//((c*d)//fractions.gcd(c,d)))
maximunc=b//c
maximund=b//d
maximuncd=b//((c*d)//fractions.gcd(c,d))
totalc=maximunc-minimunc+1
totald=maximund-minimund+1
totalcd=maximuncd-minimuncd+1

print((b-a+1)-(totalc+totald-totalcd))

Submission status: Accepted

Solution code:
def gcd(x,y):
	while y:
		x,y = y,x % y
	return x


a,b,c,d = map(int,input().split())
ans = 0

cd = c * d // gcd(c,d)

if a%c == 0:
	start_c = a
else:
	start_c = a + c - a % c
if a%d == 0:
	start_d = a
else:
	start_d = a + d - a % d
if a%cd == 0:
	start_cd = a
else:
	start_cd = a + cd - a % cd

end_c = b - b % c
end_d = b - b % d
end_cd = b - b % cd

if b >= c:
	ans += (end_c - start_c) // c + 1
if b >= d:
	ans += (end_d - start_d) // d + 1
if b >= cd:
	ans -= (end_cd - start_cd) // cd + 1

print(b-a+1-ans)

Submission status: Accepted

Solution code:
import fractions

A = list(map(int, input().split())) 
a = A[0]
b = A[1]
c = A[2]
d = A[3]
aa = a-1
all = b-aa
cdiv = b//c - aa//c
ddiv = b//d - aa//d
cd_lcm = c*d // (fractions.gcd(c,d))

cddiv = b//cd_lcm - aa//cd_lcm

print(all-cdiv-ddiv+cddiv)

Submission status: Accepted

Solution code:
import fractions as math
a,b,c,d = map(int,input().split())
"""
aからbまでの倍数を求めるのではなく、
0からa-1,0からbまでの２つに分けてそれぞれ出して差で求める。
"""

a = a-1 #0からb迄を求めるときにaはもう使わないので変えちゃっていい
lcm = (c*d//math.gcd(c,d))
c = b//c - a//c
d = b//d - a//d
cd = b//lcm - a//lcm

#求めるのは割り切れる数ではなく割り切れない数
print(b-a-(c+d-cd))

Submission status: Accepted

Solution code:
import fractions
a, b, c, d = map(int, input().split())

lcm = (c * d) // fractions.gcd(c, d)

total = b - (b//c + b//d - b//lcm)
total_del = a-1 -((a-1)//c + (a-1)//d - (a-1)//lcm)

print(total - total_del)

Submission status: Accepted

Solution code:
from fractions import gcd

def lcm(x, y):
    return x * y // gcd(x, y)

a, b, c, d = list(map(int, input().split()))
print((b-a+1)-(b//c - (a-1)//c)-(b//d - (a-1)//d)+(b//lcm(c, d) - (a-1)//lcm(c, d)))

Submission status: Accepted

Solution code:
from fractions import gcd

a, b, c, d = list(map(int, input().split()))
lcm = c * d // gcd(c, d)
print((b-a+1)-(b//c - (a-1)//c)-(b//d - (a-1)//d)+(b//lcm - (a-1)//lcm))

Submission status: Accepted

Solution code:
import fractions
a, b, c, d = map(int, input().split())
div_c = b//c + (-a//c) + 1
div_d = b//d + (-a//d) + 1
div_cd = b//(c*d//fractions.gcd(c, d)) + (-a//(c*d//fractions.gcd(c,d))) + 1
print(b-a+1 - div_c - div_d + div_cd)

Submission status: Accepted

Solution code:
from fractions import gcd
from math import ceil
def div(n,a,b):
    m = b//n-(a-1)//n
    #print(m)
    return max(m,0)
a,b,c,d = map(int,input().split())
e = c*d//gcd(c,d)
print(b-a+1-(div(c,a,b) + div(d,a,b) - div(e,a,b)))

Submission status: Accepted

Solution code:
import fractions
a, b, c, d = map(int, input().split())
a = a-1
lcm = int(c*d/fractions.gcd(c, d))
ans = (b-b//c-b//d+b//lcm)-(a-a//c-a//d+a//lcm)
print(ans)

Submission status: Accepted

Solution code:
#最大公約数
def gcd(a,b):
    while b!=0:
        a,b=b,a%b
    return a

#最小公倍数
def lcm(a,b):
    return a*b//gcd(a,b)

a,b,c,d = map(int, input().split())
cd = lcm(c,d)
bdiv = b//c + b//d - b//cd
adiv = (a-1)//c + (a-1)//d - (a-1)//cd
ans = b - (a-1) - bdiv + adiv
print(ans)

Submission status: Accepted

Solution code:
import fractions
A, B, C, D = map(int, input().split())

def lcm(a,b):
    return a*b//fractions.gcd(a,b)

b_n = B//C + B//D - B//lcm(C,D)
a_n = (A-1)//C + (A-1)//D - (A-1)//lcm(C,D)
ans_n = b_n - a_n
print(int(B-A+1-ans_n))

Submission status: Accepted

Solution code:
#最大公約数
def gcd(a,b):
  while b!=0:
    a,b=b,a%b
  return a
#最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)
  
A,B,C,D = map(int,input().split())

#ans = 全体 - (cで割れる+Dで割れる-(CかつDで割れる))
#Cで割れる
#(ex)C=3
#1~A : Cで割れる A//3個
#1~B : Cで割れる B//3個
#A~B : Cで割れる (Aが3の倍数なら) B//3 - A//3 + 1個 (Aが3の倍数でないなら) B//3 - A//3個

#全体
all = B-A+1
#Cで割れる
if(A%C == 0): c_count = B//C-A//C+1
else: c_count = B//C-A//C
#Dで割れる
if(A%D == 0): d_count = B//D-A//D+1
else: d_count = B//D-A//D
#CかつDで割れる
C_D = lcm(C,D)
if(A%C_D == 0): c_d_count = B//C_D-A//C_D+1
else: c_d_count = B//C_D-A//C_D

ans = all - (c_count+d_count-c_d_count)
print(ans)

Submission status: Accepted

Solution code:
import fractions
a,b,c,d = map(int,input().split())
lcm = (c*d)//fractions.gcd(c,d)
print(((b-((b//c)+(b//d)-(b//lcm)))-((a-1)-(((a-1)//c)+((a-1)//d)-((a-1)//lcm)))))

Submission status: Accepted

Solution code:
import fractions
a,b,c,d=map(int,input().split())

f1=fractions.gcd(c,d)
f2=c*d//f1

a=a-1
aa=a-a//c-a//d+a//f2
bb=b-b//c-b//d+b//f2

print(bb-aa)

Submission status: Accepted

Solution code:
A, B, C, D = map(int,input().split())
ans = 0
x1c = B // C
x1d = B // D
x2c = (A-1) // C
x2d = (A-1) // D
def gcd(x,y):
    if x < y:
        z = x
        x = y
        y = z
    while y != 0:
        x, y = y, x%y
    return x
g = gcd(C,D)
l = (C*D) // g
x1cd = B // l
x2cd = (A-1) // l
ans_B = B - (x1c+x1d-x1cd)
ans_A = (A-1) - (x2c+x2d-x2cd)
ans = ans_B - ans_A
ans = int(ans)
print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd
def lcm(x, y):
    return (x * y) // gcd(x, y)

A, B, C, D = map(int, input().split())

E = lcm(C, D)
count_A = (A - 1) - (A - 1)//C - (A - 1)//D + (A - 1)//E
count_B = B - B//C - B//D + B//E

print(count_B - count_A)

Submission status: Accepted

Solution code:
A, B, C, D = map(int, input().split())
def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)

def lcm(a, b):
    return a * b // gcd(a, b)

print((B-A+1) - ((B//C - (A-1)//C) + (B//D - (A-1)//D) - (B//lcm(C, D) - (A-1)//lcm(C, D))))

Submission status: Accepted

Solution code:
from fractions import gcd

A,B,C,D = list(map(int,input().split()))

lcm = C*D/gcd(C,D)

a = (A-1) - ((A-1)//C + (A-1)//D - (A-1)//int(lcm))
b = (B) - ((B)//C + (B)//D - (B)//int(lcm))
print(int(b-a))

Submission status: Accepted

Solution code:
a,b,c,d = map(int,input().split())
ans = b - a + 1
x = c * d
e = c
f = d
if e < f:
    e = f
    f = c
r = e % f
while r != 0:
    e = f
    f = r
    r = e % f
x //= f
ans -= b // c - (a-1) // c + b // d - (a-1) // d
ans += b // x - (a-1) // x
print(ans)

Submission status: Accepted

Solution code:
a,b,c,d=map(int,input().split())
def gcd(x,y):
  z=x%y
  if z==0:
    return y
  else:
    return gcd(y,z)
ans=(b-b//c-b//d+b//(c*d//gcd(c,d)))-((a-1)-(a-1)//c-(a-1)//d+(a-1)//(c*d//gcd(c,d)))
print(ans)

Submission status: Accepted

Solution code:
import fractions

a, b, c, d = map(int, input().split())
lcm = c*d//fractions.gcd(c, d)

c_num = b//c - (a-1)//c
d_num = b//d - (a-1)//d
lcm_num = b//lcm - (a-1)//lcm

ans = c_num + d_num - lcm_num

print(b-a+1-ans)

Submission status: Accepted

Solution code:
from sys import stdin
import fractions
a,b,c,d = [int(x) for x in stdin.readline().rstrip().split()]
kouyaku = fractions.gcd(c, d)
koubai = c * d // fractions.gcd(c, d)
b_s = b - b//c - b//d + b//koubai
a_s = a-1 - (a-1)//c - (a-1)//d + (a-1)//koubai
print(b_s - a_s)

Submission status: Accepted

Solution code:
#C
def gcd(x,y):
    #x>y
    if y==0:
        return x
    else:
        return gcd(y,x%y)
a,b,c,d=map(int,input().split())
a-=1
def f(i):
    ai=a%i
    bi=b%i
    if ai==bi:
        return (b-a)//i
    elif ai>bi:
        return (b-a)//i+1
    else:
        return (b-a)//i
C=c//gcd(c,d)
D=d//gcd(c,d)
H=C*D*gcd(c,d)
print(b-a-f(c)-f(d)+f(H))

Submission status: Accepted

Solution code:
A,B,C,D=map(int,input().split())
c_dividable=B//C-(A-1)//C
d_dividable=B//D-(A-1)//D
def gcd(a,b):
  if a==b:
    return a
  else:
    if a%b==0:
      return b
    while a%b!=0:
      return gcd(b,a%b)
v=C*D//gcd(C,D)
cd_dividable=B//v-(A-1)//v
print(B-A+1-(c_dividable+d_dividable-cd_dividable))

Submission status: Accepted

Solution code:
from fractions import gcd

A, B, C, D = map(int, input().split())
C_div = B // C - (A - 1) // C
D_div = B // D - (A - 1) // D
CD = C * D // gcd(C, D)
CD_div = B // CD - (A - 1) // CD
print(B - A + 1 - C_div - D_div + CD_div)