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: Wrong Answer

Solution code:
def gcd(a, b):

    while b:

        a, b = b, a % b

    return a

A,B,C,D = (int(i) for i in input().split())

X = (C * D) // gcd(C, D)
Y = (C + D) // gcd(C, D) - 1

if C == D :
    Y = 1
#print(X,Y)

N = A % X 
N_S = N-1
UA = Y - (N_S // C + N_S // D)

#print(UA)
M = B % X
UB = M // C + M // D 

#print(UB)


SA =  A + ( X - N )
SB =  B - M 
#print(SA,SB)
count = (SB - SA) // X * Y 

#print(count)
add = count + UA + UB
    
Z = B - A - add + 1

if C == 1:
    Z = 0
if D == 1:
    Z = 0
print(Z)

Submission status: Wrong Answer

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)


number =str(input())
numbers = number.split(" ")
A = int(numbers[0])
B = int(numbers[1])
C = int(numbers[2])
D = int(numbers[3])

number = B - A + 1

answer_C = number // C

answer_D = number // D

answer_E = number // lcm(C,D)
final_answer = number - answer_C - answer_D + answer_E
final_answer = int(final_answer)
print(final_answer)

Submission status: Wrong Answer

Solution code:
import fractions

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

G = C*D // fractions.gcd(C,D)

Conly = 1 + ( ( ( B - (B%C) ) - ( A + C - (A%C) ) ) // C )
Donly = 1 + ( ( ( B - (B%D) ) - ( A + D - (A%D) ) ) // D )
CandD = 1 + ( ( ( B - (B%G) ) - ( A + G - (A%G) ) ) // G )

if C == 1 or D == 1:
  result = 0
else:
  result = B - A + 1 - ( Conly + Donly - CandD )
  
if A == C or A == D:
  result -= 1

if B == C or B == D:
  result -= 1
  
if (A == C or A == D) and (B == C or B == D):
  result += 1
  
print(result)

Submission status: Wrong Answer

Solution code:
import math

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 = [int(i) for i in input().split()]

C_count = math.floor(B/C) - math.ceil(A/C) + 1
D_count = math.floor(B/D) - math.ceil(A/D) + 1
E = lcm(C, D)
both_count = math.floor(B/E) - math.ceil(A/E) + 1
answer = B - A + 1 - (C_count + D_count - both_count)

print(answer)

Submission status: Wrong Answer

Solution code:
A, B, C, D = map(int,input().split())
timeC = 0
sumC = C*int(A/C)
while(1):
    if sumC > B:
        break
    sumC += C
    timeC += 1

    
timeD = 0
sumD = D*int(A/D)
while(1):
    if sumD > B:
        break
    sumD += D
    timeD += 1
    
E = C*D
timeE = 0
sumE = E*int(A/E)
while(1):
    if sumE > B:
        break
    sumE += E
    timeE += 1
    
print(B - A - timeC - timeD + timeE + 1)

Submission status: Wrong Answer

Solution code:

import fractions
import math

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

U = B - A + 1

nA = ( math.floor(B / C) ) - (math.ceil(A / C)) +1

nB = ( math.floor(B / D) ) - (math.ceil(A / D)) +1

saisyoukoubaisuu = C * D / fractions.gcd(C, D)

saisyoukoubaisuu = int(saisyoukoubaisuu)

nAB = (B//saisyoukoubaisuu) -(A//saisyoukoubaisuu) + 1

print( U -(nA + nB - nAB) )

Submission status: Wrong Answer

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

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

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

lcm_num = lcm(c,d)
c_num = (b//c - a//c) if a%c!=0 else (b//c - a//c + 1)
d_num = (b//d - a//d) if a%d!=0 else (b//d - a//d + 1)
ba_num = b//lcm_num - a//lcm_num

if c == 1 or d == 1:
    print(0)
else:
    print(b-a+1-(c_num+d_num-ba_num) if b-a+1-(c_num+d_num-ba_num) > 0 else 0)

Submission status: Wrong Answer

Solution code:
import fractions

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

if a % c == 0:
    c_start = int(a/c)
else:
    c_start = int(a/c)+1

if a % d == 0:
    d_start = int(a/d)
else:
    d_start = int(a/d)+1

if a % lcm == 0:
        lcm_start = int(a/lcm)
else:
        lcm_start = int(a/lcm)+1

c_end = int(b/c)
d_end = int(b/d)
lcm_end = int(b/lcm)

c_num = c_end - c_start + 1
d_num = d_end - d_start + 1
lcm_num=lcm_end - lcm_start + 1

print(b - a + 1 - c_num - d_num + lcm_num)

Submission status: Wrong Answer

Solution code:
A,B,C,D=map(int, input().split())
N=B-A+1-((B//C-A//C+B//D-A//D)-(B//(C*D)-A//(C*D)))
if (A%C==0 or B%C==0):
    N-=1
    if (A%D==0 or B%D==0):
        N-=1
print(N)

Submission status: Wrong Answer

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

a = (B // C) - (A // C)
b = (B // D) - (A // D)
l = lcm(C, D)
d = (B // l) - (A // l)
print(B - A + 1 - (a + b - d))

Submission status: Wrong Answer

Solution code:
import math


def get_num(i):
    return int(b / i) - int((a-1) / i)

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 = [int(i) for i in input().split()]

all_num = b - a + 1
c_num = get_num(c)
d_num = get_num(d)
lcm = lcm(c, d)
lcm_num = get_num(lcm) 

print(all_num - c_num - d_num + lcm_num)

Submission status: Wrong Answer

Solution code:
import fractions

ABCD = list(map(int, input().split()))

A = ABCD[0]
B = ABCD[1]
C = ABCD[2]
D = ABCD[3]


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

AB = B-A
count = 0

firstC = int(A/C)
if A%C == 0 and firstC > 0:
    firstC -= 1
firstD = int(A/D)

if A%D == 0 and firstD > 0:
    firstD -= 1

firstCD = int(A/lcm(C,D))
if A%lcm(C,D) ==0 and firstCD > 0:
    firstCD -= 1

endC = int(B/C)
endD = int(B/D)
endCD = int(B/lcm(C,D))

thereC = endC - firstC
thereD = endD - firstD
thereCD = endCD - firstCD

NoP = thereC+thereD - thereCD
#
# print(thereC)
# print(thereD)
# print(thereCD)
# print(NoP)

print((B-A+1)-NoP)

Submission status: Wrong Answer

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):
    return a * b // gcd (a, b)

N_C = (B-A) // C
N_D = (B-A) // D
N_CD = (B-A) // lcm(C,D)
ans = 0

if A == B:
    if A % C != 0:
        ans = 1
elif C == 1:
    ans == 0
    print()
elif D == 1:
    ans == 0
else:
    ans = (B-A)-N_C-N_D+N_CD
print(ans)

Submission status: Wrong Answer

Solution code:
import math
a = [int(i) for i in input().split()]


s_c = math.floor(a[0]/a[2])
e_c = math.floor(a[1]/a[2])
s_d = math.floor(a[0]/a[3])
e_d = math.floor(a[1]/a[3])
c_set = {i*a[2] for i in range(s_c,e_c+1)}
d_set = {i*a[3] for i in range(s_d,e_d+1)}

union = c_set | d_set

answer = (a[1]-a[0])-len(union)
print(answer)

Submission status: Wrong Answer

Solution code:
A,B,C,D=10,40,6,8
if B<C:
    if B<D:
        print(int(B-A+1))
    else:
        ist=(A-A%D)/D
        ien=(B-B%D)/D
        print(int(B-A+1-ien+ist-1))
elif B<D:
    ist=(A-A%C)/C
    ien=(B-B%C)/C
    print(int(B-A+1-ien+ist-1))
else:
    ist,ien,jst,jen,kst,ken=1,1,1,1,1,0
    ist=(A-A%C)/C
    ien=(B-B%C)/C
    jst=(A-A%D)/D
    jen=(B-B%D)/D
    ist=(A-A%(C*D))/(C*D)
    ien=(B-B%(C*D))/(C*D)
    print(int(B-A+1-ien+ist-1-jen+jst-1+ken-kst+1-2))

Submission status: Wrong Answer

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

import fractions
import math

p = float(format(a/c,"f"))
q = float(format(a/d,"f"))
x = b//c - math.ceil(p) + 1
y = b//d - math.ceil(q) + 1
e = c*d//fractions.gcd(c,d)
r = float(format(a/e,"f"))
z = b//e - math.ceil(r) + 1

print(b-a+1-x-y+z)

Submission status: Wrong Answer

Solution code:
def gcd(m,n):
    x = max(m,n)
    y = min(m,n)
    if x%y == 0:
        return y
    else:
        while x%y!=0:
            z = x%y
            x = y
            y = z
        else:
            return z

# import math
a, b, c, d = map(int, input().split())
b_c = int(b/c)
b_d = int(b/d)
cd = int(c * d / gcd(c,d))
b_cd = int(b / cd)
bn = b_c + b_d - b_cd
# print("bn:",bn)
a -= 1
a_c = int(a/c)
a_d = int(a/d)
a_cd = int(a / cd)
an = a_c + a_d - a_cd
# print("an:",an)

num = b - a - (bn - an)
print(num)

Submission status: Wrong Answer

Solution code:

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

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

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

    diff = B - A
    count_c = int(diff / C)
    count_d = int(diff / D)
    count_union = int(diff / lcm(C, D))

    print(diff - count_c - count_d + count_union)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

for i in range(a,b+1):
    if gcd(i,c*d) == 1:
        count +=1

print(count)

Submission status: Wrong Answer

Solution code:
a,b,c,d = map(int,input().split())
#cnt = 0
"""
c2 = b//c - a//c
d2 = b//d - a//d
cd = b//(c*d) - a//(c*d)
print(c2+d2-cd)
"""
a-=1
a2 = a//c + a//d - a//(c*d)
print(b-a-(b//c + b//d - b//(c*d)) - a2)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
def lcm(x, y):
    return (x * y) // gcd(x, y)
A,B,C,D = map(int,input().split())
if A%C==0 and A%D==0:
    C_div = (B-A+1)//C + 1
else:
    C_div = (B-A+1)//C
if B%C==0 and B%D==0:
    D_div = (B-A+1)//D + 1
else:
    D_div = (B-A+1)//D
if A%(lcm(C,D)) == 0 and B%(lcm(C,D)) == 0:
    CD_div = (B-A+1)//(C*D) + 1
else:
    CD_div = (B-A+1)//(C*D)
if C == 1 or D == 1:
    print(0)
elif C == D:
    print((B-A+1) - C_div)
elif C%D==0 or D%C==0:
    if C > D:
        print((B-A+1) - D_div)
    else:
        print((B-A+1) - C_div)
else:
    print((B-A+1) - C_div -D_div + CD_div)

Submission status: Wrong Answer

Solution code:

import fractions
import math

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

U = B - A + 1

nA = ( math.floor(B / C) ) - (math.ceil(A / C)) +1

nB = ( math.floor(B / D) ) - (math.ceil(A / D)) +1

saisyoukoubaisuu = C * D / fractions.gcd(C, D)

saisyoukoubaisuu = int(saisyoukoubaisuu)

nAB = (B//saisyoukoubaisuu) -(A//saisyoukoubaisuu)

print( U -(nA + nB - nAB) )

Submission status: Wrong Answer

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


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


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


ac = int(A / C)
bc = int(B / C)
ad = int(A / D)
bd = int(B / D)
acd = int(A / lcm(C, D))
bcd = int(B / lcm(C, D))

if A % C == 0:
    c_div = bc - ac + 1
else:
    c_div = bc - ac
if A % D == 0:
    d_div = bd - ad + 1
else:
    d_div = bd - ad
if A % lcm(C, D) == 0:
    cd_div = bcd - acd + 1
else:
    cd_div = bcd - acd

ans=B - A + 1 - c_div - d_div + cd_div
if ans<0:
    print(0)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import fractions

A, B, C, D = (int(i) for i in input().split())

if C == D:
    duplicate = C
else:
    duplicate = C * D // fractions.gcd(C, D)

cCnt = (B - A) // C
dCnt = (B - A) // D
duplicateCnt = (B - A) // duplicate

print(B - A - cCnt - dCnt + duplicateCnt)

Submission status: Wrong Answer

Solution code:
import fractions
from math import ceil

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

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

count = B - A + 1

if A <= ceil(A / C) * C and ceil(A / C) * C <= B:
  count -= max(B - ceil(A / C) * C, 0) // C + 1

if A <= ceil(A / D) * D and ceil(A / D) * D <= B:
  count -= max(B - ceil(A / D) * D, 0) // D + 1

C_D_lcm = lcm(C, D)

if A <= ceil(A / C_D_lcm) * C_D_lcm and ceil(A / C_D_lcm) * C_D_lcm <= B:
  count += max(B - ceil(A / C_D_lcm) * C_D_lcm, 0) // C_D_lcm + 1

print(count)

Submission status: Wrong Answer

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

n = b-a+1

ax = n % c
ay = n % d

countx = 0
county = 0
countz = 0

if (a+ax) % c <= ax:
  countx = 1
if (a+ay) % d <= ay:
  county = 1
if a+max(c, d) % (c*d) <= (n % max(c, d)):
  countz = 1

# print(n - ((n//c + n//d)) +countx+county + (n//(c*d)))
print(n - ((n//c + n//d)+ countx+county-countz) + (n//(c*d)))

Submission status: Wrong Answer

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

if (c==1)|(d==1):
  print(0)
  sys.exit()

ac = a//c
bc = b//c
ad = a//d
bd = b//d

if a%c ==0:
  ac+=1
if b%c ==0:
  bc+=1
if a%d ==0:
  ad+=1
if b%d ==0:
  bd+=1
  
o = max(c, d)
p = min(c, d)

gcd_ = gcd(o,p)
lcm = int(c*d/gcd_)

acd = a//lcm
bcd = b//lcm

print(int(b-a+1-(bc-ac+bd-ad-(bcd-acd))))

Submission status: Wrong Answer

Solution code:
from math import ceil

def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while a % b > 0: a, b = b, a % b
    return b

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

divc = ceil((B + 1)/C) - ceil(A / C)
divd = ceil((B + 1)/D) - ceil(A / D)
dive = ceil((B + 1)/E) - ceil(A / E)

print(B + 1 - A - (divc + divd - dive))

Submission status: Wrong Answer

Solution code:
import math
import fractions
a,b,c,d=list(map(int,input().split()))
s=math.floor(b/c)-math.ceil(a/c)+1
e=math.floor(b/d)-math.ceil(a/d)+1
count=b-a-s-e+1
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
    #return math.floor((x * y) /fractions.gcd(x, y))


com=lcm(c, d)
tmp=math.floor(b/com)-math.ceil(a/com)-1
print(count+tmp)

Submission status: Wrong Answer

Solution code:
A,B,C,D=map(int,input().split())
lC = [_%C for _ in range(A,B+1)]
lD = [_%D for _ in range(A,B+1)]
ans = 0
for _ in range(len(lC)-1):
    if lC[_] !=0 or lD[_] !=0:
        ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
#C
from math import ceil, floor
from fractions import gcd
A,B,C,D = map(int, input().split())
X = int(C * D / gcd(C, D))

def retLR(A, B, num):
    #return A <= l*num < r*num <= B, length
    l = ceil(A/num)
    r = floor(B/num)
    if r*num < A and B < l*num :
        length = 0
    elif (r*num < A and l*num <= B) or (A <= r*num and B < l*num) : 
        length = 1
    else:
        length = r - l + 1
    return length

a1 = retLR(A,B,C)
a2 = retLR(A,B,D)
a3 = retLR(A,B,X)

subst = a1 + a2 - a3
ans = (B - A + 1) - subst
print(ans)

Submission status: Wrong Answer

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

e = int(b // c)
f = int(a // c)
if f == a / c:
    g = int(e - f + 1)
else:
    g = int(e - f)

h = int(b // d)
i = int(a // d)
if i == a / d:
    j = int(h - i + 1)
else:
    j = int(h - i)

import fractions

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

k = int(b // cd)
l = int(a // cd)
if l == a / cd:
    m = int(k - l + 1)
else:
    m = int(k - l)

ans = int(b-a) + 1 - int(g + j - m)
print(int(ans))

Submission status: Wrong Answer

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

import math

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

E = C*D/gcd(C, D)
interval = B-A+1

n1 = math.ceil(A/C)
n2 = math.ceil(A/D)
n3 = math.ceil(A/E)

m1 = math.floor(B/C)
m2 = math.floor(B/D)
m3 = math.floor(B/E)

cnt_c = m1-n1+1
cnt_d = m2-n2+1
cnt_cd = m3-n3+1

print(interval-(cnt_c+cnt_d-cnt_cd))

Submission status: Wrong Answer

Solution code:
import fractions

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

G = C*D // fractions.gcd(C,D)

Conly = 1 + ( ( ( B - (B%C) ) - ( A + C - (A%C) ) ) // C )
Donly = 1 + ( ( ( B - (B%D) ) - ( A + D - (A%D) ) ) // D )
CandD = 1 + ( ( ( B - (B%G) ) - ( A + G - (A%G) ) ) // G )

if C == 1 or D == 1:
  result = 0
else:
  result = B - A + 1 - ( Conly + Donly - CandD )
  
if A == C or A == D:
  result -= 1

print(result)

Submission status: Wrong Answer

Solution code:
import math
import fractions
a,b,c,d=list(map(int,input().split()))
s=math.floor(b/c)-math.ceil(a/c)+1
e=math.floor(b/d)-math.ceil(a/d)+1
count=b-a-s-e+1
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
    #return math.floor((x * y) /fractions.gcd(x, y))


com=lcm(c, d)
tmp=math.floor(b/com)-math.ceil(a/com)+1
print(count+tmp+1)

Submission status: Wrong Answer

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

c=math.floor(B/C)
cc=math.ceil(A/C)

d=math.floor(B/D)
dd=math.ceil(A/D)
CD=(C*D)//fractions.gcd(C,D)
cd=math.floor(B/(CD))
cdcd=math.ceil(A/(CD))
if CD>B:
    cd=0
    cdcd=1

print((B-A+1)-((c-cc+1)+(d-dd+1)-(cd-cdcd+1)))

Submission status: Wrong Answer

Solution code:
import fractions
a = list(map(int, input().split()))
if a[0]==a[1]:
    if a[0]%a[2]==0 or a[0]%a[3]==0:
        print(0)
    else:
        print(1)
else:
    n = a[1]-a[0]+1
    wac = int(a[1]/a[2])-int(a[0]/a[2])

    if a[0]%a[2]==0:
        wac+=1
    wad= int(a[1]/a[3])-int(a[0]/a[3])

    if a[0]%a[3]==0:
        wad+=1
        
    lcm = int(a[2]*a[3]/fractions.gcd(a[2], a[3]))
    cd = int(a[1]/lcm)-int(a[0]/lcm)

    if a[0]%lcm==0:
        cd +=1

    print(n-wac-wad+cd)

Submission status: Wrong Answer

Solution code:
import math

A, B, C, D = list(map(int, input().split(' ')))
print(math.ceil(B/C)+math.ceil(B/C)-math.ceil(A/C)-math.ceil(D/C)-math.ceil(B/(C*D))+math.ceil(B/(C*D)))

Submission status: Wrong Answer

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

s=c*d//fractions.gcd(c, d)
bk=0
ak=0
ans=0
bc=b//c
ac=a//c    
bd=b//d
ad=a//d
bsa=b//s
asa=a//s
bk=b-bc-bd+bsa
ak=a-ac-ad+asa
ans=bk-ak
if a%c!=0 or a%d!=0 or a%s!=0:
    ans+=1
else:
    ans=ans
print(ans)

Submission status: Wrong Answer

Solution code:
A,B,C,D=map(int, input().split())
N=B-A+1-((B//C-A//C+B//D-A//D)-(B//(C*D)-A//(C*D)))
if (A%C==0 or A%D==0):
    N-=1
    if (B%C==0 or B%D==0):
        N-=1
print(N)

Submission status: Wrong Answer

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

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

lcm = lcm(C, D)

llcm = A // lcm
rlcm= math.floor(B / lcm)

lc = A // C
rc = math.floor(B / C)

ld = A // D
rd = math.floor(B / D)


nlcm = rlcm-llcm+1
nc = rc - lc + 1
nd = rd - ld + 1

print(B-A+1-nc-nd+nlcm)

Submission status: Wrong Answer

Solution code:
import math
import fractions

A, B, C, D = map(int, input().split())
N = B - A + 1  #整数列の長さ

if C == D:
    tmp = 0   #最後にNから引く
    mod = A % C
    tmp += math.ceil((N-mod)/C)
    print(N-tmp)
elif max(C,D)%min(C,D) == 0:
    tmp = 0   #最後にNから引く
    mod = A % min(C, D)
    tmp += math.ceil((N-mod)/min(C, D))
    print(N-tmp)
else:
    tmp = 0   #最後にNから引く
    mod = A % C
    tmp += math.ceil((N-mod)/C)
    mod = A % D
    tmp += math.ceil((N-mod)/D)
    mod = A % (D*C)
    tmp -= math.ceil((N-mod)/(D*C))
    print(N-tmp)

Submission status: Wrong Answer

Solution code:
A, B, C, D = map(int, input().split())
a = [A//C, A//D, A//(C*D), B//C, B//D, B//(C*D)]
ans = (B-A + 1) - (a[3] - a[0]) - (a[4] - a[1]) 
if (A%C == 0) or (A%D == 0):
  ans -= 1
c = 0
for i in range(1,B//min(C,D)+1):

  if ((i*min(C,D))%max(C,D)==0) & A<= i*(min(C,D)) <= B:
    c += 1

  
print(ans+c)

Submission status: Wrong Answer

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

all_num = b - a + 1
c_mul =  round((b / c)) - round((a / c))
d_mul = round((b / d)) - round((a / d))

cd_mul = round((b / (c*d))) - round((a / (c*d)))

print(all_num - c_mul - d_mul + cd_mul)

Submission status: Wrong Answer

Solution code:
A, B, C, D = map(int, input().split())
p = 0
cntC = int(B / C) - int(A / C)
cntD = int(B / D) - int(A / D)

#a,bの最大公約数
def gcd(C, D):
    while D:
        C, D = D, C % D
    return C

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


CD = lcm(C, D)

cntCD = int(B / CD) - int(A / CD)

# print(cntC, cntD, cntCD, CD)
calc = (B-A+1) - (cntC + cntD - cntCD)

if A % C == 0:
    if B % D == 0:
        calc -= 1

print(calc)

Submission status: Wrong Answer

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

def solve(A: int, B: int, C: int, D: int):
    ci = int(B/C) - int((A-1)/C)
    di = int(B/D) - int((A-1)/D)
    l = C*D / fractions.gcd(C,D)
    xi = int(B/l) - int((A-1)/l)

    # print(ci,di,xi)
    print(B-A+1-ci-di+xi)

    return


# Generated by 1.1.4 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: Wrong Answer

Solution code:
import math
import fractions

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

# A以上B以下の数でXの倍数の個数
def countX(X) :
    if X > B :
        return 0
    minX = max(math.ceil(A / X),0)
    maxX = max(math.floor(B / X),0)
    return maxX - minX + 1

if C == 1 or D == 1 :
    print(0)
elif min(C,D) > B :
    print(A - B + 1)
elif max(C,D) % min(C,D) == 0 :
    Z = min(C,D)
    ans = B - A + 1 - countX(Z)
    print(ans + 1)
elif A % C == 0 and A % D == 0:
    ans = B - A + 1

    # 最小公倍数
    Z = int(C * D / fractions.gcd(C,D))

    ans -= countX(C)
    ans -= countX(D)
    ans += countX(Z)

    print(ans + 1)
else :
    ans = B - A + 1

    # 最小公倍数
    Z = int(C * D / fractions.gcd(C,D))

    ans -= countX(C)
    ans -= countX(D)
    ans += countX(Z)

    print(ans)

Submission status: Wrong Answer

Solution code:
import fractions

ABCD = list(map(int, input().split()))

A = ABCD[0]
B = ABCD[1]
C = ABCD[2]
D = ABCD[3]


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

AB = B-A
count = 0

firstC = int(A/C)
if A%C == 0 and firstC > 0:
    firstC -= 1
firstD = int(A/D)

if A%D == 0 and firstD > 0:
    firstD -= 1

firstCD = int(A/lcm(C,D))
if A%lcm(C,D) ==0 and firstCD > 0:
    firstCD -= 1

endC = int(B/C)
endD = int(B/D)
endCD = int(B/lcm(C,D))

thereC = endC - firstC
thereD = endD - firstD
thereCD = endCD - firstCD

NoP = thereC+thereD - thereCD
#
# print(thereC)
# print(thereD)
# print(thereCD)
# print(NoP)

print(B-A+1-NoP)

Submission status: Wrong Answer

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


if B<C:
    if B<D:
        print(int(B-A+1))
    else:
        ist=(A-A%D)/D
        ien=(B-B%D)/D
        print(int(B-A+1-ien+ist-1))
elif B<D:
    ist=(A-A%C)/C
    ien=(B-B%C)/C
    print(int(B-A+1-ien+ist-1))
else:
    ist,ien,jst,jen,kst,ken=1,1,1,1,1,0
    ist=(A-A%C)/C
    ien=(B-B%C)/C
    jst=(A-A%D)/D
    jen=(B-B%D)/D
    ist=(A-A%(C*D))/(C*D)
    ien=(B-B%(C*D))/(C*D)
    print(int(B-A+1-ien+ist-1-jen+jst-1+ken-kst+1))

Submission status: Wrong Answer

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

A,B,C,D=map(int,input().split())
calC=(B//C)-(A//C)
calD=(B//D)-(A//D)
calCD=(C*D)//gcd(C,D)

aaa=B-A+1
print(aaa-calC-calD-calCD)

Submission status: Wrong Answer

Solution code:
A , B , C , D = map(int,input().split())
lst = []
list= [i for i in range(A,B)]
for i in range(len(list)):
    if i % C !=0 and i % D != 0:
        lst.append(i)
print(len(lst))

Submission status: Wrong Answer

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

x = c
y = d
num = c * d
if x > y:
    tmp = x
    x = y
    y = tmp

tmp = y % x
while tmp != 0:
    y = x
    x = tmp
    tmp = y % x

num = num / x

cnt = math.floor(b / c) - math.ceil(a / c) + 1
cnt = cnt + math.floor(b / d) - math.ceil(a / d) + 1
cnt = cnt - (math.floor(b / num) - math.ceil(a / num) + 1)

print(b - a + 1 - cnt)

Submission status: Wrong Answer

Solution code:
import fractions

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

G = C*D // fractions.gcd(C,D)

Conly = 1 + ( ( ( B - (B%C) ) - ( A + C - (A%C) ) ) // C )
Donly = 1 + ( ( ( B - (B%D) ) - ( A + D - (A%D) ) ) // D )
CandD = 1 + ( ( ( B - (B%G) ) - ( A + G - (A%G) ) ) // G )

if C == 1 or D == 1:
  result = 0
else:
  result = B - A + 1 - ( Conly + Donly - CandD )
  
print(Conly)

print(Donly)
print(CandD)
  
if A == C or A == D:
  result -= 1

print(result)

Submission status: Wrong Answer

Solution code:
import fractions

A, B, C, D = (int(i) for i in input().split())

if A == B and A != C and A != D:
    print(1)
    exit()

if C == D:
    duplicate = C
elif C > D and C % D == 0:
    duplicate = C
elif D > C and D % C == 0:
    duplicate = D
else:
    duplicate = C * D // fractions.gcd(C, D)

cCnt = (B - A) // C
dCnt = (B - A) // D
duplicateCnt = (B - A) // duplicate

print(B - A - cCnt - dCnt + duplicateCnt)

Submission status: Wrong Answer

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

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 = [int(i) for i in input().split()]
C = 0
D = 0
E = 0
'''
for i in range(int(a / max(c,d,lcm(c,d))),b-a):
    print(i)
    if c * i >= a and c * i <= b and C == 0:
        C = c * i
    if d * i >= a and d * i <= b and D == 0:
        D = d * i
    if lcm(c,d) * i >= a and lcm(c,d) * i <= b and E == 0:
        E = lcm(c,d) * i
    if C != 0 and D != 0 and E != 0:
        break
'''
'''
for i in range(int(a / lcm(c,d)) - 1,int(b / lcm(c,d))+2):
    if lcm(c,d) * i >= a and lcm(c,d) * i <= b and E == 0:
        E = lcm(c,d) * i
        break

for i in range(int(a / c) - 1,int(b / c)+2):
    if c * i >= a and c * i <= b and C == 0:
        C = c * i
        break

for i in range(int(a / d) - 1,int(b / d)+2):
    if d * i >= a and d * i <= b and D == 0:
        D = d * i
        break
'''
#print(lcm(c,d))
#print('')
#print(C,D,E)
'''
c2 = int(b / c) - int(C / c)+1
d2 = int(b / d) - int(D / d)+1
e2 = int(b / lcm(c,d)) - int(E / lcm(c,d))+1
'''
'''
if int(b / c) == b/c:
	print('c')
	c2 += 1
if int(b / d) == b/d:
	print('d')
	d2 += 1
if int(b / lcm(c,d)) == b/lcm(c,d):
	print('e')
	e2 += 1
'''
c2 = int(b / c) - int(a / c)
d2 = int(b / d) - int(a / d)
e2 = int(b / lcm(c,d)) - int(a / lcm(c,d))
if a % c == 0:
	#print('c')
	c2 += 1
if a % d == 0:
	#print('d')
	d2 += 1
if a % lcm(c,d) == 0:
	#print('e')
	e2 += 1
'''
if E == 0:
	e2 = 0
if C == 0:
	c2 = 0
if D == 0:
	d2 = 0
'''

#print(b-a+1,c2,d2,e2)
'''
c1 = list(range(C,b+1,c))
d1 = list(range(D,b+1,d))
e1 = list(range(E,b+1,lcm(c,d)))
print(b-a+1,len(c1),len(d1),len(e1))

print(c1,d1,e1)

print(b-a+1-len(c1)-len(d1)+len(e1))
'''
print(int(b-a+1 - c2 - d2 + e2))

Submission status: Wrong Answer

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=input().split()
a=int(a)
b=int(b)
c=int(c)
d=int(d)
x=a+a%c
if c>a:
    x=c
y=a+a%d
if d>a:
    y=d
e=lcm(c,d)
z=a+a%e
if e>a:
    z=e
x2=(b-x)//c+1
if x>b:
    x2=0
y2=(b-y)//d+1
if y>b:
    y2=0
z2=(b-z)//e+1
if z>b:
    z2=0
if c>b and d>b:
    print(b-a+1)
elif d>b:
    print(b-a+1-x2)
elif c>b:
    print(b-a+1-y2)
elif e>b:
    print(b-a-x2-y2+1)
else:
    print(b-a+1-x2-y2+z2)

Submission status: Wrong Answer

Solution code:
import fractions

a, b, c, d = (float(x) for x in input().split())
def kansu(x,y,z):
    sta = x
    end = y
    if sta%z != 0:
        sta = sta+(z-(sta%z))
    if end%z != 0:
        end = end-(end%z)
    ans = ((end-sta)/z) +1
    return ans
a1 = kansu(a,b,c)
a2 = kansu(a,b,d)
lcm = c*d/(fractions.gcd(int(c),int(d)))
a3 = kansu(a,b,lcm)
print(int(b-a-a1-a2+a3+1))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import math

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

A, B, C, D = map(int, input().split())
subst = abs(A - B)
num1 = subst // C + 1
num2 = subst // D + 1
num3 = subst // (C * D // gcd(C, D)) + 1

if A % gcd(C, D) == 0 and B % gcd(C, D) == 0:
    print(subst - num1 - num2 + num3)
else:
    print(subst - num1 - num2 + num3 + 1)

Submission status: Wrong Answer

Solution code:
a,b,c,d=map(int,input().split())
c = sum(x%c!=0 or x%d!=0 for x in range(a,b+1))
print(b+1-a+1-c)

Submission status: Wrong Answer

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


number =str(input())
numbers = number.split(" ")
A = int(numbers[0])
B = int(numbers[1])
C = int(numbers[2])
D = int(numbers[3])

number = B - A + 1

answer_C = number // C

answer_D = number // D

answer_E = number // lcm(C,D)
final_answer = number - answer_C - answer_D + answer_E
print(int(final_answer))

Submission status: Wrong Answer

Solution code:
import math
import fractions

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

# A以上B以下の数でXの倍数の個数
def countX(X) :
    if X > B :
        return 0
    minX = max(math.ceil(A / X),0)
    maxX = max(math.floor(B / X),0)
    return maxX - minX + 1

if C == 1 or D == 1 :
    print(0)
elif min(C,D) > B :
    print(A - B + 1)
elif max(C,D) % min(C,D) == 0 :
    Z = min(C,D)
    ans = B - A + 1 - countX(Z)
    print(ans + 1)
elif A % C == 0 and A % D == 0:
    ans = B - A + 1

    # 最小公倍数
    Z = int(C * D / fractions.gcd(C,D))

    ans -= countX(C)
    ans -= countX(D)
    ans += countX(Z)

    print(ans - 1)
else :
    ans = B - A + 1

    # 最小公倍数
    Z = int(C * D / fractions.gcd(C,D))

    ans -= countX(C)
    ans -= countX(D)
    ans += countX(Z)

    print(ans)

Submission status: Wrong Answer

Solution code:
import math


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


A, B, C, D = map(int, input().split())  # A以上B以下の整数のうち、CでもDでも割り切れないものの個数

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

# B以下の全ての数でCで割れるものの個数hoge
hogeBC = math.floor(B / C)
# A-1以下の全ての数でCで割れるものの個数fuga
fugaA1C = math.floor((A-1) / C)

# B以下の全ての数でDで割れるものの個数
piyoBD = math.floor(B / D)
# A-1以下の全ての数でDで割れるものの個数
toriA1D = math.floor((A-1) / D)

# B以下の全ての数でCとDの最小公倍数で割れるものの個数
yoshiBCD = math.floor(B / (CD_lcm))
# A-1以下の全ての数でCとDの最小公倍数で割れるものの個数
takaA1CD = math.floor((A-1) / (CD_lcm))

# A以上B以下の整数でCで割り切れるものの個数
Ccan = hogeBC-fugaA1C
# A以上B以下の整数でDで割り切れるものの個数
Dcan = piyoBD - toriA1D
# A以上B以下の整数でCでもDでも割り切れるものの個数
CDcan = yoshiBCD - takaA1CD


# A以上B以下でCでもDでも割り切れるものの個数
count = Ccan + Dcan - CDcan
print(B-(A-1) - count)

Submission status: Wrong Answer

Solution code:
numbers = input().split(' ')

a = int(numbers[0])	
b = int(numbers[1])
c = int(numbers[2])
d = int(numbers[3])

e = c * d
i = int(a / c) + 1
j = int(a / d) + 1
k = int(a / e) + 1
count = 0
while c * i < b:
	i += 1
	count += 1
while d * j < b:
	j += 1
	count += 1
while e * k < b:
	k += 1
	count -= 1
print(b - a - count + 1)

Submission status: Wrong Answer

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

def cl(a,b):
	if a%b==0:
		return int(a/b)
	else:
		return int(a/b)+1

def ma(a,b):
	if a>b:
		return a
	else:
		return b
		
a,b,c,d = map(int,input().split())
e = (c*d)/g(c,d)
t1 = ma(0,int(b/c)-cl(a,c)+1)
t2 = ma(0,int(b/d)-cl(a,d)+1)
t3 = ma(0,int(b/e)-cl(a,e)+1)
ans = t1+t2-t3
ans = b-a+1-ans
print(ans)

Submission status: Wrong Answer

Solution code:
def koyaku(m, n):
    temp = 0
    while(m % n != 0):
        temp = n
        n = m % n
        m = temp
    return n

def koubai(m, n):
    return m * n / koyaku(m, n)

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

if C > D:
    kou = koubai(C, D)
else:
    kou = koubai(D, C)

a = int(A / C)
b = int(B / C)

ab = b - a

a2 = int(A / D)
b2 = int(B / D)

ab2 = b2 - a2

total = int(B / int(kou))
partial = int(A / int(kou))
print((B - A) - ((ab + ab2) - (total - partial)))

Submission status: Wrong Answer

Solution code:
import math
from fractions import gcd


def read():
    A, B, C, D = list(map(int, input().strip().split()))
    return A, B, C, D


def solve(A, B, C, D):
    nC = B // C - math.ceil(A / C) + 1
    nD = B // D - math.ceil(A / D) + 1
    lcm = C * D // gcd(C, D)
    nCD = B // lcm - math.ceil(A / lcm) + 1
    return (B - A + 1) - (nC + nD - nCD)


if __name__ == '__main__':
    inputs = read()
    print("%d" % solve(*inputs))