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:
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    if b == 0:
        return a
    return gcd(b, a % b)

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

def count(X, C, D):
    ret = X - (X//C + X//D - X//lcm(C, D))
    return ret

def main():
    A, B, C, D = map(int, input().split())
    ans = count(B, C, D) - count(A-1, C, D)
    print(ans)


if __name__ == "__main__":
    main()

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 calc(n):
    return n-n//C-n//D+n//(C*D//gcd(C, D))

print(calc(B)-calc(A-1))

Submission status: Accepted

Solution code:
A,B,C,D=map(int,input().split())
def gcd(a,b):
    while a%b>0:
        a,b=b,a%b
    return b
CC=B//C-(A-1)//C
DD=B//D-(A-1)//D
CD=C*D//gcd(C,D)
EE=B//CD-(A-1)//CD
print(B-A+1-CC-DD+EE)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

import math

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

E=lcm(C,D)

x=B-A+1
y=B//C-(A-1)//C
z=B//D-(A-1)//D
w=B//E-(A-1)//E

print(x-y-z+w)

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 F(N,C,D):
  x = N
  x -= N//C
  x -= N//D
  lcm = C*D//gcd(C,D)
  x += N//lcm
  return x

answer = F(B,C,D) - F(A-1,C,D)
print(answer)

Submission status: Accepted

Solution code:
import fractions
A, B, C, D = map(int, input().split())
total = B - A + 1
Cdiv = B // C - (A-1) // C
Ddiv = B // D - (A-1) // D
cddiv = (C*D)//fractions.gcd(C,D)
CDdiv = B // (cddiv) - (A-1) // (cddiv)

print(total - Cdiv - Ddiv + CDdiv)

Submission status: Runtime Error

Solution code:
import math

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


C_multiple = B // C - A // C
D_multiple = B // D - A // D 

if A % C == 0:
    C_multiple += 1
if A % D == 0:
    D_multiple += 1
        

CD_gcd = C * D / math.gcd(C,D) 
CD_multiple = B // CD_gcd - A// CD_gcd 
if A % CD_gcd == 0:
    CD_multiple += 1
***
print(C_multiple)
print(D_multiple)
print(CD_gcd)
print(CD_multiple)
***
ans = ( B - A + 1 ) - (C_multiple + D_multiple - CD_multiple)
print(int(ans))

Submission status: Runtime Error

Solution code:
import math


# def calc(n, c, d):
#     return n-(math.floor(n / c) +
#               math.floor(n / d) - math.floor(n / (c * d / math.gcd(c, d))))
def calc(n, c, d):
    return n-(n / c +
              n / d - n / (c * d / gcd(c, d)))


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

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


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
from math
a,b,c,d=list(map(int,input().split()))
a-=1
def calculation(x,c,d):
	x=x-(x//c)-(x//d)+(x//((c*d)//math.gcd(c,d)))
	return x
	
print(calculation(b,c,d)-calculation(a,c,d))

Submission status: Runtime Error

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

def calc_div_num(a,b,x):
  return b//x - (a-1)//x

num_c = b//c - (a-1)//c
num_d = b//d - (a-1)//d

import numpy as np
lcd = np.lcd(c,d)
num_lcd = b//lcd - (a-1)//lcd

result = b-a+1 - (num_c+num_d-num_lcd)
print(result)

Submission status: Runtime Error

Solution code:
import sys

inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

a, b, c, d = inintm()

print(b-a+1 - (-(-(b-a)//c) + -(-(b-a)//d))) + 1

Submission status: Runtime Error

Solution code:
def common_multiple(x, y):
    for i in range(2, min(x, y)+1):
        mod_x, mod_y = x % i, y % i
        if mod_x != 0 and mod_y != 0:
            next
        x0 = x // i if (mod_x == 0) else x
        y0 = y // i if (mod_y == 0) else y
        return common_multiple(x0, y0) * i
    return x * y


a = input().split(" ")
A, B, C, D = int(a[0]) - 1, int(a[1]), int(a[2]), int(a[3])

M = common_multiple(C, D)
B0 = B // C + B // D - B // M
A0 = A // C + A // D - A // M
print(B - A - B0 + A0)

Submission status: Runtime Error

Solution code:
import numpy

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

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

b_n = b // c + b // d - b // cd
a_n = a // c + a // d - a // cd
print((b - b_n) - (a - a_n))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
A,B,C,D=map(int,input().split())
E=0
for i in range(A,B+1):
  if i%C!=0 and i%D!=0:
    E+=1
peint(E)

Submission status: Runtime Error

Solution code:
x=list(map(int,input().split()))
a,b,c,d,p=x[0],x[1],x[2],x[3],0
G=int(c*d/math.gcd(c,d))
a1,a2,a3=0,0,0
if b/c-int(b/c)==0:
    a1=1
if b/d-int(b/d)==0:
    a2=1
if b/G-int(b/G)==0:
    a3=1
p=a1+a2+a3
print(b-a+1-b//c+a//c-b//d+a//d+b//G-a//G-p)

Submission status: Runtime Error

Solution code:
A,B,C,D = map(int,input().split())
num = [0]*B


tmpC = C
while C<=B:
    num[C] += 1
    C += tmpC


tmpD = D
while D<=B:
    num[D] += 1
    D += tmpD

num1 = num[A:B]
ans = num1.count(0)
print(ans)

Submission status: Runtime Error

Solution code:
A,B,C,D = map(int,input().split())
num = [0]*B


tmpC = C
while C<B:
    num[C] += 1
    C += tmpC


tmpD = D
while D<B:
    num[D] += 1
    D += tmpD

num1 = num[A:B]
ans = num1.count(0)
print(ans)

Submission status: Runtime Error

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

A,B,C,D = list(map(int,input().split()))
C_bai = list(range(A-(A%C)+C,B,C+1))
D_bai = list(range(A-(A%D)+D,B,D+1))
E = lcm(C,D)
CD_bai = list(range(A-(A%E)+E,B,E+1))
print(B-A+1-len(C_bai)-len(D_bai)+CD_bai)

Submission status: Runtime Error

Solution code:
import numpy as np
import fractions
 
A,B,C,D = map(int,input().split())
BA = np.arange(A, B+1)
BA_C = BA % C
BA_D = BA % D
BA_CD = BA % (C * D//fractions.gcd(C, D))
 
C_count = np.count_nonzero(BA_C == 0)
D_count = np.count_nonzero(BA_D == 0)
CD_count = np.count_nonzero(BA_CD == 0)
 
print(len(BA)-(C_count+D_count-CD_count))

Submission status: Runtime Error

Solution code:
import numpy as np
import math

A,B,C,D = map(int,input().split())
BA = np.arange(A, B+1)
BA_C = BA % C
BA_D = BA % D
BA_CD = BA % (C * D//math.gcd(C, D))

C_count = np.count_nonzero(BA_C == 0)
D_count = np.count_nonzero(BA_D == 0)
CD_count = np.count_nonzero(BA_CD == 0)

print(len(BA)-(C_count+D_count-CD_count))

Submission status: Runtime Error

Solution code:
import numpy as np
import math

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

BA = np.arange(A, B+1)
BA_C = BA % C
BA_D = BA % D
BA_CD = BA % (C * D//math.gcd(C, D))

C_count = np.count_nonzero(BA_C == 0)
D_count = np.count_nonzero(BA_D == 0)
CD_count = np.count_nonzero(BA_CD == 0)

print(len(BA)-(C_count+D_count-CD_count))

Submission status: Runtime Error

Solution code:
from decimal import *
A,B,C,D=list(map(int,input_[0].split()))

def gcd(a,b):
    x=max(a,b)
    y=min(a,b)
    
    while True:
#         print(x,y)
        y_ = x % y
        if y_ == 0:
            return y
        x = y
        y = y_

def lcm(a,b):
    ans=Decimal(a*b)/Decimal(gcd(a,b))
    return ans.as_integer_ratio()[0]

CD=lcm(C,D)
len_all = B-A+1
len_c = B//C-(A-1)//C
len_d = B//D-(A-1)//D
len_c_and_d = B//CD-(A-1)//CD

ans=len_all-len_c-len_d+len_c_and_d 
print(int(ans))

Submission status: Runtime Error

Solution code:
a, b, c, d = map(int, input().split())
N = list(range(a, b+1, 1))
t = 0
for n in N:
  if n % c != 0 and n % d != 0:
    t += 1
print(t)

Submission status: Runtime Error

Solution code:
a, b, c, d = map(int, input().split())
N = list(range(a, b+1, 1))
t = 0
for n in N:
  if n % c == 0 and n % d == 0:
    t += 1
print(t)

Submission status: Runtime Error

Solution code:
import math
a, b, c, d = map(int, input().split())
ans = 0
def notdiv(n, m):
  return n // m

def lcm(x, y):
    return (x * y) // math.gcd(x, y)
a -= 1
e = b - notdiv(b, c) - notdiv(b, d) + notdiv(b, lcm(c, d))
f = a - notdiv(a, c) - notdiv(a, d) + notdiv(a, lcm(c, d))
ans = e - f
print(ans)

Submission status: Runtime Error

Solution code:
import math
a,b,c,d = map(int,input().split())
num = b-a+1
ac = int((a-1)/c)
ad = int((a-1)/d)
bc = int(b/c)
bd = int(b/d)
e = c*d//(math.gcd(c,d))
a = int(a/e)
b = int(b/e)
c = bc-ac
d = bd-ad
e = b-a
print(num-(c+d-e))

Submission status: Runtime Error

Solution code:
import math
a,b,c,d=map(int,input().split())
total=b-a+1
f1=(b//c)-(a//c)
if a%c==0:
    f1+=1
f2=(b//d)-(a//d)
if a%d==0:
    f2+=1
gcd=math.gcd(c,d)
lcm=(c*d)//gcd
f3=(b//lcm)-(a//lcm)
if a%lcm==0:
    f3+=1
f=f1+f2-f3
print(total-f)

Submission status: Runtime Error

Solution code:
n,l=map(int,input().split())
a=l
b=n+l-1
x=((a+b)*n)//2
if a<=0 and b<=0:
  print(x-b)
elif a<=0:
  print(x)
else:
  print(x-a)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
# https://atcoder.jp/contests/abc131/tasks/abc131_c


import math


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

C_ = (B // C) - ((A - 1) // C)

D_ = (B // D) - ((A - 1) // D)

# 最大公約数
CD = math.gcd(C, D)

# 最小公倍数
CD = (C * D) / CD

CD_ = (B // CD) - ((A - 1) // CD)

total_num = B - (A - 1)

ans = total_num - C_ - D_ + CD_

print(int(ans))

Submission status: Runtime Error

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

#c,d,lcm(c,d)の倍数がa~bの区間にいくつあるか調べる関数
def check(p):
    if a%p==0:
        e = a//p
    else:
        e = (a//p) +1

    if b%p==0:
        f = b//p
    else:
        f = (b//p)
        
    return(f-e+1)
        

lcm = (c*d)//gcd(c,d)
    
ans = b-a+1 -(check(c)+check(d)) +check(lcm)
print(ans)

Submission status: Runtime Error

Solution code:
def resolve():
    A, B, C, D = map(int, input().split())
    d = D
    c = C
    gcd = 0
    while gcd == 0:
        if d % c == 0:
            gcd = c
            break
        else:
            d = c
            c = d % c
    lcm = C*D//gcd
    A = A-1
    print((B-B//C-B//D+B//lcm)-(A-A//C-A//D+A//lcm))
resolve()

Submission status: Runtime Error

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

Submission status: Runtime Error

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

def num(x, y):
    return (x * y) // math.gcd(x, y)

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

Submission status: Runtime Error

Solution code:
import math
a,b,c,d = map(int,input().split())
AdivC = (a-1)//c
BdivC = b//c
AdivD = (a-1)//d
BdivD = b//d
gc = math.gcd(c,d)
div = (c*d)//gc
AdivCandD = (a-1)//div
BdivCandD = b//div

divC = BdivC - AdivC
divD = BdivD - AdivD
divCandD = BdivCandD - AdivCandD
ans = b-a+1
ans = ans -(divC+divD-divCandD)
print(ans)

Submission status: Runtime Error

Solution code:

[A,B,C,D] = list(map(int,input().split()))
#print(C,D)
#both
import math
f=math.gcd(C,D)
f2=C*D//f
#print(f2)

both=B//f2 - (A-1)//f2
#print('both', both)

#C
Ccount=B//C - (A-1)//C
#print('C', Ccount)

#D
Dcount=B//D - (A-1)//D
#print('D', Dcount)

print(B-A+1 -(Ccount+Dcount-both))

Submission status: Runtime Error

Solution code:

[A,B,C,D] = list(map(int,input().split()))
#print(C,D)
#both
import math
f=math.gcd(C,D)
f2=C*D//f
#print(f2)

i=-1
flag=0
while True:
    i+=1
    if f2*i>=A and flag!=1:
        hajime=i
        flag=1
    elif f2*i>B:
        owari=i
        break
both=owari-hajime
#print('both', both)

#C
i=-1
flag=0
while True:
    i+=1
    if C*i>=A and flag!=1:
        hajime=i
        flag=1
    elif C*i>B:
        owari=i
        break
Ccount=owari-hajime
#print('C', Ccount)

#D
i=-1
flag=0
while True:
    i+=1
    if D*i>=A and flag!=1:
        hajime=i
        flag=1
    elif D*i>B:
        owari=i
        break
Dcount=owari-hajime
#print('D', Dcount)

print(B-A+1 -(Ccount+Dcount-both))

Submission status: Runtime Error

Solution code:
import math
a, b, c, d = map(int, input().split())
ans = b - a + 1

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

a -= 1
ans = ans - (b//c) - (b//d) + (b//(lcm(c,d)))
ans = ans + (a//c) + (a//d) - (a//(lcm(c,d)))
print(ans)

Submission status: Runtime Error

Solution code:
import math
a, b, c, d = map(int, input().split())
ans = b - a + 1

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

a -= 1
ans = ans - b//c - b//d + b//(lcm(c,d))
ans = ans + a//c + a//d - a//(lcm(c,d))
print(ans)

Submission status: Runtime Error

Solution code:
import math


def lcm(c, d):
    return (c * d) // math.gcd(c, d)


def main():
    a, b, c, d = map(int, input().split())
    c_num = (b//c-(a-1)//c)
    d_num = (b//d-(a-1)//d)
    cd_num = (b//lcm(c, d)-a//lcm(c, d))
    ans = b-a+1-c_num-d_num+cd_num
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import fraction

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

a = int(B / C) - int((A-1) / C)
b = int(B / D) - int((A-1) / D) 
c = C * D / fraction.gcd(C, D)
d = int(B / c) - int((A-1) / c)

Ans = (B - A + 1) - a - b + d 
print(Ans)

Submission status: Runtime Error

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

a = int(B / C) - int((A-1) / C)
b = int(B / D) - int((A-1) / D) 
c = C * D / math.gcd(C, D)
d = int(B / c) - int((A-1) / c)

Ans = (B - A + 1) - a - b + d 
print(Ans)

Submission status: Runtime Error

Solution code:
import math

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

a = int(B / C) - int((A-1) / C)
b = int(B / D) - int((A-1) / D) 
c = C * D / math.gcd(C, D)
d = int(B / c) - int((A-1) / c)

Ans = (B - A + 1) - a - b + d 
print(Ans)

Submission status: Runtime Error

Solution code:
import numpy as np
import math

gcd = math.gcd

def main():
    #n = int(input())
    a,b,c,d = map( int, input().split())

    c_baisuu = int(b/c)-int((a-1)/c)
    d_baisuu = int(b/d)-int((a-1)/d)
    cd = c * d / gcd(c, d)
    cd_baisuu = int(b/(cd))-int((a-1)/(cd))
    zenbu = b-a+1
    print(zenbu-(c_baisuu+d_baisuu-cd_baisuu))
    
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import math

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

a = int(B / C) - int(A / C)
b = int(B / D) - int(A / D) 
c = C * D / math.gcd(C, D)
d = int(B / c) - int(A / c)

Ans = (B - A) - a - b + d 
print(Ans)

Submission status: Runtime Error

Solution code:
import math 
a,b,c,d = map(int,input().split(" "))
count = 0

u = b - a + 1

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

c_num = b//c - (a-1)//c
d_num = b//d - (a-1)//d
cd_num = b//cd - (a-1)//cd

print(u - (c_num + d_num - cd_num))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
import math
A,B,C,D = map(int,input().split())
lcm = C*D // math.gcd(C,D) 
print(B-A-int(B/C-A/C)-int(B/D-A/D)+int(B/lcm-A/lcm))

Submission status: Runtime Error

Solution code:
import math
A,B,C,D = map(int, input().split())
X = B//C - (A-1)//C
Y = B//D - (A-1)//D
lcm = round(C*D//gcd(C,D))
Z = B//lcm - (A-1)//lcm
print(B-A+1-X-Y+Z)

Submission status: Runtime Error

Solution code:
from fractions import gcd
A,B,C,D = map(int, input().split())
X = B//C - (A-1)//C
Y = B//D - (A-1)//D
lcm = round(C*D//math.gcd(C,D))
Z = B//lcm - (A-1)//lcm
print(B-A+1-X-Y+Z)

Submission status: Runtime Error

Solution code:
# ABC131C - Anti-Division
import math
A,B,C,D = map(int, input().split())
X = B//C - (A-1)//C
Y = B//D - (A-1)//D
lcm = round(C*D//math.gcd(C,D))
Z = B//lcm - (A-1)//lcm
print(B-A+1-X-Y+Z)

Submission status: Runtime Error

Solution code:
import math
A,B,C,D = map(int, input().split())
X = B//C - (A-1)//C
Y = B//D - (A-1)//D
lcm = C*D//math.gcd(C,D)
Z = B//lcm - (A-1)//lcm
print(B-A+1-X-Y+Z)

Submission status: Runtime Error

Solution code:
A,B,C,D = map(int, input().split())
X = B//C - (A-1)//C
Y = B//D - (A-1)//D
Z = B//lcm - (A-1)//int(C/math.gcd(C,D) *D)
print(B-A+1-X-Y+Z)

Submission status: Runtime Error

Solution code:
import math
A,B,C,D = map(int, input().split())
X = B//C - (A-1)//C
Y = B//D - (A-1)//D
lcm = int(C/math.gcd(C,D) *D)
Z = B//lcm - (A-1)//lcm
print(B-A+1-X-Y+Z)

Submission status: Runtime Error

Solution code:
import math
A,B,C,D = map(int, input().split())
X = B//C - (A-1)//C
Y = B//D - (A-1)//D
lcm = int(C*D/math.gcd(C,D))
Z = B//lcm - (A-1)//lcm
print(B-A+1-X-Y+Z)

Submission status: Runtime Error

Solution code:
import sys
import math
input = lambda: sys.stdin.readline().rstrip()


def count_div(N, C, D):

    t = N // C + N // D - N // (C * D // math.gcd(C, D))
    return N - t


def solve():

    A, B, C, D = map(int, input().split())
    ans = count_div(B, C, D) - count_div(A - 1, C, D)
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import math


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



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

    sss = b//c - (a-1)//c
    ssss = b//d - (a-1)//d
    di = lcm(c,d)
    sssss = b // di - (a - 1) // di if di != 0 else 0
    print((b-a+1) - (sss+ssss-sssss))


if __name__ == '__main__':
    main2()

Submission status: Runtime Error

Solution code:
# cまたはdで割り切れるものを計算して、b-a+1の全体から引く
import math

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

def c_d_koubai(x, y, w, z):
    c_d_koubai =  math.gcd(w, z) * (w//math.gcd(w, z)) * (z//math.gcd(w, z))

    # 最初の公倍数が先頭から何番目に位置するか（インデックス、先頭が0として）
    a_amari = x % c_d_koubai
    if a_amari == 0:
        koubai_sentou = 0
    else:
        koubai_sentou = c_d_koubai - a_amari 
    
    # bは先頭から何番目か（インデックス）
    b_number = y - x
    
    # a~bの間に公倍数がない
    if b_number < koubai_sentou:
        koubai_num = 0
    else:
        # a~bの間に公倍数が何個あるか
        koubai_num = (b_number-koubai_sentou) // c_d_koubai + 1
    return koubai_num

def c_d_only_bai(x, y, w):
    c_d_amari = x % w
    if c_d_amari == 0:
        c_d_sentou = 0
    else:
        c_d_sentou= w - x%w
    c_d_number = y - x

    if c_d_number < c_d_sentou:
        c_d_num = 0
    else:
        c_d_num = (c_d_number-c_d_sentou) // w + 1
    return c_d_num


koubai = c_d_koubai(a, b, c, d)
c_koubai = c_d_only_bai(a, b, c)
d_koubai = c_d_only_bai(a, b, d)

print(b-a+1-(c_koubai+d_koubai-koubai))

Submission status: Runtime Error

Solution code:
import math

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


kobaisu=C//math.gcd(C,D)*D


ans=B -(B//C+B//D-B//kobaisu)

ans2=(A-1)-((A-1)//C +(A-1)//D - (A-1)//kobaisu)

print(int(ans-ans2))

Submission status: Runtime Error

Solution code:
import math

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

#最小公倍数a * b / gcd(a, b)
kobaisu=C//math.gcd(C,D)*D


ans=B -(B//C+B//D-B//kobaisu)

ans2=(A-1)-((A-1)//C +(A-1)//D - (A-1)//kobaisu)

print(int(ans-ans2))

Submission status: Runtime Error

Solution code:
import math

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

#最小公倍数a * b / gcd(a, b)
kobaisu=C/math.gcd(C,D)*D



ans=B -(B//C+B//D-B//kobaisu)

ans2=(A-1)-((A-1)//C +(A-1)//D - (A-1)//kobaisu)

print(int(ans-ans2))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

A, B, C, D = map(int, input().split())
count = 0
BCcount = B // C
BDcount = B // D
ACcount = (A - 1) // C
ADcount = (A - 1) // D
l = C * D // gcd(C, D)
BCDcount = B // l
ACDcount = (A - 1) // l
Bcount = Decimal(B - (BCcount + BDcount - BCDcount))
Acount = Decimal((A - 1) - (ACcount + ADcount - ACDcount))
print(Decimal(Bcount - Acount))

Submission status: Runtime Error

Solution code:
import math

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

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

gcd_cd = lcm(c,d)

a_c_numbers = a//c
if a % c == 0:
    a_c_numbers -= 1
a_d_numbers = a//d
if a % d == 0:
    a_d_numbers -= 1

a_numbers = a_c_numbers + a_d_numbers

b_c_numbers = b//c
b_d_numbers = b//d
b_numbers = b_c_numbers + b_d_numbers

gcd_cd_numbers = b//gcd_cd - a//gcd_cd

# print(a_c_numbers,a_d_numbers)
# print(b_c_numbers,b_d_numbers)
# print(gcd_cd_numbers)
# print(b_numbers - a_numbers - gcd_cd_numbers)
ans = b - a + 1 - (b_numbers - a_numbers - gcd_cd_numbers)

if ans > 0:
    print(ans)
else:
    print(0)

Submission status: Runtime Error

Solution code:
import math

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

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

gcd_cd = lcm(c,d)

a_c_numbers = a//c
if a % c == 0:
    a_c_numbers -= 1
a_d_numbers = a//d
if a % d == 0:
    a_d_numbers -= 1

a_numbers = a_c_numbers + a_d_numbers

b_c_numbers = b//c
b_d_numbers = b//d
b_numbers = b_c_numbers + b_d_numbers

gcd_cd_numbers = b//gcd_cd - a//gcd_cd

# print(a_c_numbers,a_d_numbers)
# print(b_c_numbers,b_d_numbers)
# print(gcd_cd_numbers)
# print(b_numbers - a_numbers - gcd_cd_numbers)
print(b - a + 1 - (b_numbers - a_numbers - gcd_cd_numbers))

Submission status: Runtime Error

Solution code:
import math

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


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


C_num = B // C - A // C

D_num = B // D - A // D

lcm = lcm(C, D)
CD_num = B // lcm - A // lcm


# Cで割れる時
tasu = 0
if (A % C == 0):
    tasu += 1
tasu += C_num

# Dで割れる時
if (A % D == 0):
    tasu += 1
tasu += D_num

# CでもDでも割れる
hiku = 0
if (A % lcm == 0):
    hiku += 1
hiku += CD_num

print((B - A + 1) - (tasu - hiku))

Submission status: Runtime Error

Solution code:
import math

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


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


C_num = B // C - A // C

D_num = B // D - A // D

lcm = lcm(C, D)
CD_num = B // lcm - A // lcm


# Cで割れる時
tasu = 0
if (A % C == 0):
    tasu += 1
tasu += C_num

# Dで割れる時
if (A % D == 0):
    tasu += 1
tasu += D_num

# CでもDでも割れる
hiku = 0
if (A % (lcm(C, D)) == 0):
    hiku += 1
hiku += CD_num

print((B - A + 1) - (tasu - hiku))

Submission status: Runtime Error

Solution code:
import math

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


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


Csita = A // C
Cue = B // C

Dsita = A // D
Due = B // D

CDsita = A // lcm(C, D)
CDue = B // lcm(C, D)


# Cで割れる時
tasu = 0
if (A % C == 0):
    tasu += 1
tasu += Cue - Csita

# Dで割れる時
if (A % D == 0):
    tasu += 1
tasu += Due - Dsita

# CでもDでも割れる
hiku = 0
if (A % (lcm(C, D)) == 0):
    hiku += 1
hiku += CDue - CDsita

print((B - A + 1) - (tasu - hiku))

Submission status: Runtime Error

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


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


Csita = A // C
Cue = B // C

Dsita = A // D
Due = B // D

CDsita = A // lcm(C, D)
CDue = B // lcm(C, D)


# Cで割れる時
tasu = 0
if (A % C == 0):
    tasu += 1
tasu += Cue - Csita

# Dで割れる時
if (A % D == 0):
    tasu += 1
tasu += Due - Dsita

# CでもDでも割れる
hiku = 0
if (A % (lcm(C, D)) == 0):
    hiku += 1
hiku += CDue - CDsita

print(B - A + 1 - (tasu - hiku))

Submission status: Runtime Error

Solution code:
A, B, C, D = map(int, input().split())
total = B - A + 1
num_C = B // C - (A-1)//C
num_D = B // D - (A-1)//D

from math import gcd
g = gcd(C, D)
l = C * D // g
num_CD = B//l - (A-1)//l
ans = total - (num_C + num_D - num_CD)
print(ans)

Submission status: Runtime Error

Solution code:

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

a,b,c,d = map(int,input().split())
all = b - a + 1
if c == 1 or d == 1:
    print(0)
    exit()

cd = lcm(a, b)

cs = b//c - a//c + 1
ds = b//d - a//d + 1
cds = b//cd - a//cd + 1

print(all - cs - ds + cds)

Submission status: Runtime Error

Solution code:
import math

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

num1 = B // C - (A-1) // C
num2 = B // D - (A-1) // D
gcdCD = C * D // math.gcd(C, D)
num3 = B // gcdCD - (A-1) // gcdCD

print(B - A - num1 - num2 + num3 + 1)

Submission status: Runtime Error

Solution code:
import math
a,b,c,d = map(int,input().split())
n = (a-1)//c #1~a-1までのcで割り切れる数
m = b//c #1~bまでのcで割り切れる数
ck = m - n #a~bまでのcで割り切れる数

o = (a-1)//d #1~a-1までのdで割り切れる数
p = b//d #1~bまでのdで割り切れる数
dk = p - o #a~bまでのdで割り切れる数

#cでもdでも割り切れる場合
e = (c * d) // math.gcd(c, d) #cとdの最小公倍数
q = (a-1)//e #1~a-1までのeで割り切れる数
r = b//e #1~bまでのeで割り切れる数
ek = r - q #a~bまでのeで割り切れる数

#cまたはdで割り切れる数
cord = ck + dk - ek

ans = b - a + 1 - cord
print(ans)

Submission status: Runtime Error

Solution code:
import math
A, B, C, D = map(int, input().split())
count = 0
BCcount = math.floor(B / C)
BDcount = math.floor(B / D)
ACcount = math.floor((A - 1) / C)
ADcount = math.floor((A - 1) / D)
l = C * D / gcd(C, D)
BCDcount = math.floor(B / l)
ACDcount = math.floor((A - 1) / l)
Bcount = B - (BCcount + BDcount - BCDcount) 
Acount = (A - 1) - (ACcount + ADcount - ACDcount)
print(Bcount - Acount)

Submission status: Runtime Error

Solution code:
from fractions import gcd
a,b,c,d = map(int,input().split())
ls = [e for e in range(a,b+1)]
y = lambda x,z = c:x%z
ss = ls[:]
cmil = list(map(y,ss))
cmin = cmil.index(0) + a
bb = ls[::-1]
cmal = list(map(y,bb))
cmax = b - cmal.index(0) 
y = lambda x,z = d:x%z
dmil = list(map(y,ss))
dmin = dmil.index(0) + a
dmal = list(map(y,bb))
dmax = b - dmal.index(0)
y = lambda x,z = (c*d)/gcd(c,d):x%z
cdmil = list(map(y,ss))
cdmin = cdmil.index(0) + a
cdmal = list(map(y,bb))
cdmax = b - cdmal.index(0)
ca = (cmax - cmin)/c +1
da = (dmax - dmin)/d +1
cda = (cdmax - cdmin)/((c*d)/gcd(c,d)) +1
print(int(b-a+1-ca-da+cda))

Submission status: Runtime Error

Solution code:
a, b, c, d = map(int, input().split())
import math
xc = (a-1)//c
yc = b//c
xd = (a-1)//d
yd = b//d    
xcd = (a - 1) // (c * d // math.gcd(c, d))
ycd = b // (c * d // math.gcd(c, d))

print(b - a + 1 -((yc - xc) + (yd - xd) - (ycd - xcd)))

Submission status: Runtime Error

Solution code:
A,B,C,D =map(int, input().split())
import math
lcm = C * D // math.gcd(C, D)
E = B - (B // C + B // D - B // lcm)
F = (A-1) - ((A-1) // C + (A-1) // D - (A-1) // lcm)
result = E-F
print(result)

Submission status: Runtime Error

Solution code:
import math

A, B, C, D = map(int, input().split())
lcm = C * D // math.gcd(C, D)

count = 0
div_C = B // C - math.ceil(A / C) + 1
div_D = B // D - math.ceil(A / D) + 1
div_C_D = B // lcm - math.ceil(A / lcm) + 1


print(B - A + 1 - div_C - div_D + div_C_D)

Submission status: Runtime Error

Solution code:
from math import *
a, b, c, d = map(int, input().split())
a -= 1
B = b - (b//c + b//d)+b//(c*d//gcd(c, d))
A = a - (a//c + a//d)+a//(c*d//gcd(c, d))

print(B - A)

Submission status: Runtime Error

Solution code:
def gcd(a, b):
    a, b = sorted([a, b])
    while(b != 0):
        a, b = (b, a%b)
    return b

def getMultiCount(start, end, times):
    if end < times: return 0
    s = start-(start%times)
    if s != start: s += times
    e = end-(end%times)
    if s > e: return 0
    return ((e-s)//times)+1
    
def answer(a, b, c, d):
    cCount = getMultiCount(a, b, c)
    dCount = getMultiCount(a, b, d)
    cdCount = getMultiCount(a, b, c*d//gcd(c,d))
    return (b-a+1)-(cCount + dCount - cdCount)

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

Submission status: Runtime Error

Solution code:
import math
# def gcd(x, y):
#     while(y != 0):
#         temp = y
#         y = x%y
#         x = temp
#     return y

def getMultiCount(start, end, times):
    if end < times: return 0
    s = start-(start%times)
    if s != start: s += times
    e = end-(end%times)
    if s > e: return 0
    return ((e-s)//times)+1
    
def answer(a, b, c, d):
    cCount = getMultiCount(a, b, c)
    dCount = getMultiCount(a, b, d)
    cdCount = getMultiCount(a, b, c*d//math.gcd(c,d))
    return (b-a+1)-(cCount + dCount - cdCount)

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

Submission status: Runtime Error

Solution code:
from math import gcd

def getMultiCount(start, end, times):
    if end < times: return 0
    s = start-(start%times)
    if s != start: s += times
    e = end-(end%times)
    if s > e: return 0
    return ((e-s)//times)+1
    
def answer(a, b, c, d):
    cCount = getMultiCount(a, b, c)
    dCount = getMultiCount(a, b, d)
    cdCount = getMultiCount(a, b, c*d//gcd(c,d))
    return (b-a+1)-(cCount + dCount - cdCount)

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

Submission status: Runtime Error

Solution code:
import math

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

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

div_c = (B // C) - (A // C)
if A // C != 0 and A % C == 0:
    div_c += 1
div_d = (B // D) - (A // D)
if A // D != 0 and A % D == 0:
    div_d += 1
div_cd = (B // (lcm)) - (A // (lcm))
if A // (lcm) != 0 and A % lcm == 0:
    div_cd += 1

ans = (B - A + 1) - (div_c + div_d - div_cd)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
# import math
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
# sys.setrecursionlimit(int(pow(10, 2)))
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
mod = int(pow(10, 9) + 7)
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]





# @lru_cache(None)
def G(x,y):
    if(x<y):
        x,y=y,x
    if(y==0):
        return x
    return G(y,x%y)


a,b,c,d=l()
ans=0
# print(a,b,c,d,b//c,a//c)
ans+=(b//c-a//c+(a%c==0))
# print(ans)
ans+=(b//d-a//d+(a%d==0))
# print(ans)
lcm=c*d//gcd(c,d)
ans-=(b//lcm-a//lcm+(a%lcm==0))
print(b-a+1-ans)

Submission status: Runtime Error

Solution code:
import math

print(math.gcd(100000003,10000003))