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:
import fractions

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

def custom_round(number, ndigits=0):
    if type(number) == int:
        return number
    d_point = len(str(number).split('.')[1])
    if ndigits >= d_point:
        return number
    c = (10 ** d_point) * 2
    return round((number * c + 1) / c, ndigits)

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

x =int((b/c)-(a/c))
y =int((b/d)-(a/d))
n =lcm(c,d)
xy=int((b/n)-(a/n))

print(x)
print(y)
print(xy)
ans = (b-a+1) - (x+y-xy)
print(int(ans)-1)

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

Submission status: Wrong Answer

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

def count_multiple(n):
    return (B // n) - ((A - 1) // n)

def count_not_multiple(n):
    return (B - A + 1) - count_multiple(n)

cnm = count_not_multiple
print(cnm(C) + cnm(D) - cnm(C * D))

Submission status: Wrong Answer

Solution code:
#coding:utf-8
a, b, c, d = map(int, input().split())

# cで割り切れる個数を求める
c_div = abs((b // c) - (a // c))

# dで割り切れる個数を求める
d_div = abs((b // d) - (a // d))

# cとd両方で割り切れる数を求める
cd = c * d
cd_div = abs((b // cd) - (a // cd))

div =  c_div + d_div - cd_div

print((b-a+1)-div)

Submission status: Wrong Answer

Solution code:
def gcd(x, y):
	if (x > y) :
		x , y = y , x
	r = x % y
	while(r != 0) :
		x = y;
		y = r;
		r = x % y;
	
	return y
def lcm(x, y):
    return (x * y) // gcd(x, y)

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

s = B - A + 1
if (A % C == 0) :
	aKc = int(A / C)
else :
	aWc = A / C
	aIc = int(aWc)
	aKc = aIc + 1
bKc = int(B / C)

if (A % D == 0) :
	aKd = int(A / D)
else :
	aWd = A / D
	aId = int(aWd)
	aKd = aId + 1
bKd = int(B / D)

lcm = lcm(C, D)
if (A % lcm == 0) :
	aKcd = int(A / lcm)
else :
	aWcd = A / lcm
	aIcd = int(aWcd)
	aKcd = aIcd + 1
bKcd = int(B / lcm)

print("{}".format(s - (bKc - aKc + 1 + bKd - aKd + 1 - (bKcd - aKcd + 1))))

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)

c_div_a = (a-1) // c
c_div_all = b // c

d_div_a = (a-1) // d
d_div_all = b // d

cd_div_a = (a-1) // int( lcm(c, d) )
cd_div_all = b // int( lcm(c, d) )

div_a = c_div_a + d_div_a - cd_div_a
div_all = c_div_all + d_div_all - cd_div_all

print( (b - (a-1)) - (div_all - div_a) )

Submission status: Wrong Answer

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

import fractions
import math



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

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

Submission status: Wrong Answer

Solution code:
import math
def gcd(a,b): 
    if a==0: 
        return b 
    return gcd(b%a,a)
def lcm(a,b):
  return a*b/gcd(a,b)
a,b,c,d=map(int,input().split())
div_c=math.ceil(((b-a)+1)/c)
div_d=math.ceil(((b-a)+1)/d)
tot_div=math.ceil(((b-a)+1)/lcm(c,d))
#print(div_c)
#print(div_d)
#print(tot_div)
print(((b-a)+1)-(div_c+div_d-tot_div))

Submission status: Wrong Answer

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

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= gcd(C, D)
l=C*D/X
amari1=A%C
if amari1==0:
    a=A
else:
    a=A+C-amari1
amari2=B%C
b=B-amari2
kazu1=((b-a)/C+1)
 
 
amari3=A%D
if amari3==0:
    a=A
else:
    a=A+D-amari3
amari4=B%D
b=B-amari4
kazu2=((b-a)/D+1)
 
amari5=A%l
if amari5==0:
    c=A
else:
    c=A+l-amari5
amari6=B%l
d=B-amari6
kazu3=((d-c)/l+1)
 
print(int(B-A+1-kazu1-kazu2+kazu3))

Submission status: Wrong Answer

Solution code:
import fractions

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

e=b-a
f=a-1

g = int(b/c) + int(b/d) - int(b/((c * d) // fractions.gcd(c, d)))
h = int(f/c) + int(f/d) - int(f/((c * d) // fractions.gcd(c, d)))
print(e+1 - g + h)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

result = 0

result += (B-A+1) - (B//C - A//C) - (B//D - A//D)
l = lcm(C, D)
result += (B//l - A//l)

print("{}".format(result))

Submission status: Wrong Answer

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

def c(begin, end, num):
    r = end - begin + 1
    if r < num:
        if begin < num < end:
            return 1
        else:
            return 0
    if num == r:
        return 1
    if num < r:
        begin = ((begin // num) + 1) * num
        r = end - begin + 1
        ans =  1 + (r // num)
        return ans

A, B, C, D = [int(x) for x in input().split(' ')]

C, D = (C, D) if C < D else (D, C)
CD = (C * D) // gcd(C, D)

count = 0

if C == D:
    c_count = c(A, B, C)
    count = c_count
elif D == CD:
    c_count = c(A, B, C)
    d_count = c(A, B, D)
    count = c_count + d_count
else:
    c_count = c(A, B, C)
    d_count = c(A, B, D)
    cd_count = c(A, B, CD)
    count = c_count + d_count - cd_count

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

Submission status: Wrong Answer

Solution code:
A, B, C, D = [int(i) for i in input().split()]

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


countC = B // C - A // C
if A % C == 0:
    countC += 1
countD = B // D - A // D
if A % D == 0:
    countD += 1
s = gcd(C, D)
countCD = B // C // (D // s) - A // C // (D // s)
if A / C / (D // s) == 1:
    countCD += 1

res = (B - A + 1 - countC - countD + countCD)
if res < 0:
    res = 0
print(res)

Submission status: Wrong Answer

Solution code:
import numpy as np
import math

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

a, b, c, d = [int(i) for i in input().split()]
#
#num = np.arange(b - a + 1) + a
#
#if b < c and b < d:
#    print('0')
#    exit(0)
#
#m = min(c, d)
#div = max(c, d)
#tmp = []
#div = num[0] // div + div
#index = 0
#while True:
#    if n[index] % div != 0:
#        if index + div >
#        tmp += range(n[index], n[index])
#        index += 1
#    else:
#        index += div
#
#tmp = np.array(tmp)
#print(tmp[tmp % m == 0].shape[0])


size = b - a + 1
cd = int(c * d // gcd(c, d))
c_num_a = a // c
d_num_a = a // d
cd_num_a = a // cd
c_num_b = b // c
d_num_b = b // d
cd_num_b = b // cd

c_num = c_num_b - c_num_a
d_num = d_num_b - d_num_a
cd_num = cd_num_b - cd_num_a

#if a % c == 0:
#    c_num += 1
#if a % d == 0:
#    d_num += 1

print(size - c_num - d_num + cd_num)

Submission status: Wrong Answer

Solution code:

import fractions

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


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

if A%C==0:
    nC_initial=A//C
else:
    nC_initial=A//C+1
    
if A%D==0:
    nD_initial=A//D
else:
    nD_initial=A//D+1

    
if A%E==0:
    nE_initial=A//E
else:
    nE_initial=A//E+1
    
nC_final=B//C
nD_final=B//D
nE_final=B//E
if nC_final<nC_initial:
    nC_final=nC_initial
if nD_final<nD_initial:
    nD_final=nD_initial
if nE_final<nE_initial:
    nE_final=nE_initial
nc=(nC_final-nC_initial+1)
nd=(nD_final-nD_initial+1)
ne=(nE_final-nE_initial+1)
if B<C:
    nc=0
if B<D:
    nd=0
if B<E:
    nE=0
#print(ans,nC_initial,nC_final,nD_initial,nD_final, nE_initial,nE_final)
print(ans-nc-nd+ne)

Submission status: Wrong Answer

Solution code:
def gcd(a,b):
    while b!=0:
        a,b=b,a%b
    return a
a,b,c,d=list(map(int,input().split(" ")))
g=gcd(c,d)
lcm=c*d/g
Ncd=int(b/(lcm))-int((a-1)/(lcm))
Nc=int(b/c)-int((a-1)/c)
Nd=int(b/d)-int((a-1)/d)
print(b-a+1-(Nc+Nd-Ncd))
print(Nc,Nd,Ncd)

Submission status: Wrong Answer

Solution code:

import fractions

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


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

if A%C==0:
    nC_initial=A//C
else:
    nC_initial=A//C+1
    
if A%D==0:
    nD_initial=A//D
else:
    nD_initial=A//D+1

    
if A%E==0:
    nE_initial=A//E
else:
    nE_initial=A//E+1
    
nC_final=B//C
nD_final=B//D
nE_final=B//E
if nC_final<nC_initial:
    nC_final=nC_initial
if nD_final<nD_initial:
    nD_final=nD_initial
if nE_final<nE_initial:
    nE_final=nE_initial
nc=(nC_final-nC_initial+1)
nd=(nD_final-nD_initial+1)
ne=(nE_final-nE_initial+1)
if B<C:
    nc=0
if B<D:
    nd=0
if B<E:
    nE=0
print(ans,nC_initial,nC_final,nD_initial,nD_final, nE_initial,nE_final)
print(ans-nc-nd+ne)

Submission status: Wrong Answer

Solution code:
import numpy as np
import math

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

a, b, c, d = [int(i) for i in input().split()]
#
#num = np.arange(b - a + 1) + a
#
#if b < c and b < d:
#    print('0')
#    exit(0)
#
#m = min(c, d)
#div = max(c, d)
#tmp = []
#div = num[0] // div + div
#index = 0
#while True:
#    if n[index] % div != 0:
#        if index + div >
#        tmp += range(n[index], n[index])
#        index += 1
#    else:
#        index += div
#
#tmp = np.array(tmp)
#print(tmp[tmp % m == 0].shape[0])


size = b - a + 1
cd = int(c * d // gcd(c, d))
c_num_a = a // c
d_num_a = a // d
cd_num_a = a // cd
c_num_b = b // c
d_num_b = b // d
cd_num_b = b // cd

c_num = c_num_b - c_num_a
d_num = d_num_b - d_num_a
cd_num = cd_num_b - cd_num_a

if a % c == 0:
    c_num += 1
if a % d == 0:
    d_num += 1

print(size - c_num - d_num + cd_num)

Submission status: Wrong Answer

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

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

a, b, c, d = map(int, input().split())
b1 = b // c
a1 = a // c
b2 = b // d
a2 = a // d
a3 = a // lcm(c,d)
b3 = b // lcm(c,d)
#print(a1, a2, a3)
#print(b1, b2, b3)
a_ans = a1 + a2 - a3
b_ans = b1 + b2 - b3
diff = b - a + 1
ans = diff - (b_ans - a_ans)
if (lcm(c,d) == d*c):
    ans -= 1
print(ans)

Submission status: Wrong Answer

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

A = b // c - (a-1) // c
B = b // d - (a-1) // c
AB = b // lcm(c,d) - (a-1) // lcm(c,d)

print(U-A-B+AB)

Submission status: Wrong Answer

Solution code:
a, b, c, d = [int(l) for l in input().split()]
res = 0
for i in range(a+1, b+1):
  if i % c > 0 and i % d > 0:
    res += 1
print(res)

Submission status: Wrong Answer

Solution code:
A, B, C, D = [int(x) for x in input().split(' ')]

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

B = B + 1
a = B // C
b = B // D
c = B // (C*D // gcd(C,D))
d = A // C
e = A // D
f = A // (C*D // gcd(C,D))
print(B - (a + b - c) - (A - (d + e - f)))

Submission status: Wrong Answer

Solution code:
import fractions
A,B,C,D = map(int, input().split())
x = abs(A-B)//C + 1
y = abs(A-B)//D + 1
if (B-A)/((C*D)//fractions.gcd(C,D)) < 1:
    z = 0
else:
    z = abs(A-B)//((C*D)//fractions.gcd(C,D)) + 1
print(B-A+1-x-y+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)

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

Submission status: Wrong Answer

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

if A%C == 0:
    cnt_c = A//C - 1
else:
    cnt_c = A//C

if A%D == 0:
    cnt_d = A//D - 1
else:
    cnt_d = A//D

if A%(C*D) == 0:
    cnt_cd = A//(C*D) - 1
else:
    cnt_cd = A//(C*D)

num = (B//C)+(B//D)-(B//(C*D)) - (cnt_c + cnt_d - cnt_cd)
print(B-A+1-num)

Submission status: Wrong Answer

Solution code:
import math
import fractions

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

if C > D :
    C , D = D , C

ans = 0

if A < C :
    ans += C - A
    A = C

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

    return maxX - minX + 1

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

ans += B - A + 1 - countX(C) - countX(D) + countX(Z)

print(ans)

Submission status: Wrong Answer

Solution code:
l,m, s,t= map(int,input().split())
p=0
for i in range(l,m+1):
  if  l%s!=0 and l%t!=0:
    p+=1
print(p)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

from fractions import gcd
from math import ceil,floor

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


ans = B - A - floor(B/C) + ceil(A/C) - floor(B/D) + ceil(A/D) + floor(B/(C * D / gcd(C,D))) - ceil(A/(C * D / gcd(C,D)))

print(ans)

Submission status: Wrong Answer

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

if a%c==0:
    cc=int(b/c)-int(a/c)
else:
    cc = int(b / c) - (int(a / c)+1)


if a%d==0:
    dd = int(b / d) - int(a / d)
else:
    dd=int(b/d)-int((a/d)+1)

i=0
f=[c]
x=10000000
k=[c,d]

while x!=0:
    x=max(k)%min(k)

    if x!=0:
        f.append(x)
    k.sort()
    k[1]=x
#Wprint(min(f))

if (int(b / (c*d/min(f))))!= (int(a / (c*d/min(f)))):
    if a%d==0:
        cd = int(b / (c*d/min(f)))- int(a / (c*d/min(f)))
    else:
        cd=int(b/(c*d/min(f)))-(int(a/(c*d/min(f)))+1)
else:
    cd=0


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

Submission status: Wrong Answer

Solution code:
import fractions

def div(a,b,c):
    val=b//c-a//c
    return val

A,B,C,D=map(int,input().split())
numC=div(A,B,C)
numD=div(A,B,D)
lcm=int(C*D/(fractions.gcd(C,D)))
numCD=div(A,B,lcm)
ans=B-A+1-numC-numD+numCD
print(ans)

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)

lcm = lcm(C, D)
# A
Ax = A // lcm
Ay = A // C
Az = A // D
AA = Ay + Az -Ax
a = A - AA
# B
Bx = B // lcm
By = B // C
Bz = B // D
BB = By + Bz -Bx
b = B - BB

print(b - a)

Submission status: Wrong Answer

Solution code:
import fractions
A, B, C, D = map(int, input().split())
diff = B - A + 1
count_c = int(B/C) - int((A-1)/C)
count_d = int(B/D) - int((A-1)/D)
cd = int(C*D / fractions.gcd(C,D))
count_cd = int(B/cd) - int((A-1)/cd)
print(diff - count_c - count_d + count_cd)

Submission status: Wrong Answer

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

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

A, B, C, D = list(map(int, input().split()))
lcm = lcm(max(C, D), min(C, D))
print((B - A + 1) - (B // C - A // C) - (B // D - A // D) + (B // lcm - A // lcm))

Submission status: Wrong Answer

Solution code:
#!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3

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

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

x = int(c * d / gcd(c, d))
c1d = int((a - 1) / c)
c2d = int((b + 0) / c)
d1d = int((a - 1) / d)
d2d = int((b + 0) / d)
x1d = int((a - 1) / x)
x2d = int((b + 0) / x)

print(b + 1 - a - (c2d - c1d + d2d - d1d - (x2d - x1d)))

Submission status: Wrong Answer

Solution code:
from fractions import gcd

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

def f(x, c, d):
    return x-int(x/c)-int(x/d)+int(x/lcm(c,d))

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

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())
m = int(c*d/gcd(c,d))
if c == 1 or d == 1:
  print(0)
  exit()
if a%c < b%c:
  xc = (b-a+1)//c
elif a%c > b%c and b%c == 0 and a%c == 1:
  xc = (b-a+1)//c
elif a%c > b%c:
  xc = (b-a+1)//c +1
elif a%c == b%c and a%c == 0:
  xc = (b-a+1)//c +1
else:
  xc  = (b-a+1)//c

if a%d < b%d:
  xd = (b-a+1)//d
elif a%d > b%d and b%d == 0 and a%d == 1:
  xd = (b-a+1)//d
elif a%d > b%d:
  xd = (b-a+1)//d +1
elif a%d == b%d and a%d == 0:
  xd = (b-a+1)//d +1
else:
  xd  = (b-a+1)//d

if a%m < b%m:
  xm = (b-a+1)//m
elif a%m > b%m and b%m == 0 and a%m == 1:
  xm = (b-a+1)//m
elif a%m > b%m:
  xm = (b-a+1)//m +1
elif a%m == b%m and a%m == 0:
  xm = (b-a+1)//m +1
else:
  xm  = (b-a+1)//m

print(b-a+1-xc-xd+xm)

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)

a,b,c,d = list(map(int,input().split(" ")))
e = lcm(c,d)
p=a//c
o=b//c
i=a//d
u=b//d
y=a//e
t=b//e
C = max([0,o-p])
D = max([0,u-i])
E = max([0,t-y])
print( (b-a+1)-(C+D-E) )

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)),int(b / lcm(c,d))+1):
    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),int(b / c)+1):
    if c * i >= a and c * i <= b and C == 0:
        C = c * i
        break

for i in range(int(a / d),int(b / d)+1):
    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 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:
import math

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

_c = c
_d = d
while _d % _c != 0:
	_d, _c = _c, _d % _c
cd = (c * d) // _c

_a = int(a/c) + int(a/d) - int(a/cd);
_b = int(b/c) + int(b/d) - int(b/cd);
ans = (b - a) - (_b - _a);

if (not(a%c == 0 or a%d ==0)) : ans +=1

print(ans)

Submission status: Wrong Answer

Solution code:
A, B, C, D =  (int(j) for j in input().split()) 
    
n = B - A + 1
CD = C * D

if A == B:
  print(int(not( A%C==0 or A%D==0 )))
elif C != D:
  print( n- ( (B//C - (A-1)//C) + (B//D - (A-1)//D) - (B//CD - (A-1)//CD) ) )
else:
  print( n- ( (B//C - (A-1)//C) ) )

Submission status: Wrong Answer

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

e = b // c #c de wareru
f = a // c
if f == a / c:
    g = e - f + 1
else:
    g = e - f

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

import fractions

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

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

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import random
input = sys.stdin.readline

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

def main():

	#S = int(input())
	#S = list(input())
	A,B,C,D = map(int, input().split(" "))
	#L = list(map(int, input().split(" ")))

	E = lcm(C, D)
	if A == B:
		if A % C == 0 or A % D == 0:
			print(0)
			sys.exit()
		else:
			print(1)
			sys.exit()

	if C == D:
		tmp_C2 = B // C
		tmp_C2ama = B % C

		tmp_C1 = A // C
		tmp_C1ama = A % C

		tmp = (tmp_C2-tmp_C1)

		tmp2 = 0
		if tmp_C1ama == 0:
			tmp2 -=1

		ans = B-A-tmp+1+tmp2
		print(ans)
		sys.exit()

	tmp_C2 = B // C
	tmp_C2ama = B % C
	tmp_D2 = B // D
	tmp_D2ama = B % D
	tmp_C1 = A // C
	tmp_C1ama = A % C
	tmp_D1 = A // D
	tmp_D1ama = A % D

	tmp_E2 = B // E
	tmp_E2ama = B % E
	tmp_E1 = A // E
	tmp_E1ama = A % E

	tmp = (tmp_C2+tmp_D2)-(tmp_C1+tmp_D1)

	tmp2 = 0
	if tmp_C1ama == 0:
		tmp2 -=1
	if tmp_D1ama == 0:
		tmp2 -=1

	ans = B-A-tmp+1+(tmp_E2-tmp_E1)+tmp2

	print(ans)

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

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

if __name__ == "__main__":
	main()

Submission status: Wrong Answer

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



A,B,C,D = map(int,input().split())
# print(math.floor(A/C),math.floor(B/C))
# print(math.floor(A/D),math.floor(B/D))



Cwari = math.floor(B/C) - math.floor(A/C)
if A%C == 0:
    Cwari += 1
# print("Cwari:",Cwari)

Dwari = math.floor(B/D) - math.floor(A/D)
if A%D == 0:
    Cwari += 1
# print("Dwari:",Dwari)

CD = lcm(C, D)
# print("CD:",CD)

CDwari = math.floor(B/CD) - math.floor(A/CD)
# print("CDwari:",CDwari)
if A%CD == 0:
    Cwari += 1


print(B-A+1-(Cwari+Dwari-CDwari))

Submission status: Wrong Answer

Solution code:
import fractions


A, B, C, D = map(int, input().split())
tmp = fractions.gcd(C, D)
lcd = C * D // tmp
acnt1 = int((A - 1) / C)
acnt2 = int((A - 1) / D)
acnt3 = int(B / C)
acnt4 = int(B / D)
acnt5 = int((A - 1) / lcd)
acnt6 = int(B / lcd)
ans = B - A + 1
atmp = acnt3 + acnt4 - acnt5 - acnt6 - acnt1 - acnt2
ans -= atmp

print(ans)

Submission status: Wrong Answer

Solution code:
import math
import fractions

def main():

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

    count_C,count_D,count_CD = 0,0,0
    CD = C * D / fractions.gcd(C,D)
    #print("CD = ", CD)

    for i in range(A,B+1):
        if i%C == 0:
            count_C += (B-i)//C + 1
            break
        else:
            continue

    #print("count_C = ", count_C)

    for j in range(A,B+1):
        if j%D == 0:
            count_D += (B-j)//D + 1
            break
        else:
            continue

    #print("count_D = ", count_D)

    for k in range(A,B+1):
        if k%CD == 0:
            count_CD += (B-k)//CD + 1
            break
        else:
            continue

    #print("count_CD = ", count_CD)

    print(int(B-A+1 - (count_C + count_D - count_CD)))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

base = b-a+1

subb_c = int(b/c)
if a%c == 0:
    subm_c = int(a/c)
else:
    subm_c = int(a/c) + 1
c_count = subb_c - subm_c + 1

subb_d = int(b/d)
if a%d == 0:
    subm_d = int(a/d)
else:
    subm_d = int(a/d) + 1
d_count = subb_d - subm_d + 1

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

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

mil = lcm(c, d)
subb_mil = int(b/mil)
if a%mil == 0:
    subm_mil = int(a/mil)
else:
    subm_mil = int(a/mil) + 1
mil_count = subb_mil - subm_mil + 1

base = base - c_count - d_count + mil_count

print(base)

Submission status: Wrong Answer

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

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)
l = gcd(c,d)
if a == b:
  if a % c == 0 or a % d == 0:
    print(0)
    exit()
  else:
    print(1)
    exit()
if l == 1:
  x =c * d
  
  print(int( b-a+1 - (b//c - (a-1)//c + b//d - (a-1) // d) + (b//x - (a-1)//x)))
  
  exit()
else:
  x = (c * d/l)
  
  print(int( b-a+1 - (b//c - (a-1)//c + b//d - (a-1) // d) + (b//x - (a-1)//x)))

Submission status: Wrong Answer

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 getAns(a, b, c):
  if a % c == 0:
    start = a
  else:
    start = a+ c - (a % c)
  if b % c == 0:
    end = b
  else:
    end = b - c + (b % c)
  num_c = max((end - start) // c + 1, 0)
  return num_c

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

# cについて
num_c =getAns(a,b,c)
#print(num_c)

# dについて
num_d =getAns(a,b,d)
#print(num_d)

# cとdの公倍数について
cd = c * d // gcd(c,d)
num_cd =getAns(a,b,cd)
#print(num_cd)

print(b - a + 1 - (num_c + num_d - num_cd))

Submission status: Wrong Answer

Solution code:
line = input().strip()
A,B,C,D = line.split(" ")
A = int(A)
B = int(B)
C = int(C)
D = int(D)
nums = range(C,D+1)

result = 0

while(A <= B):
    for n in nums:
        if (A % n) == 0: result += 1

    A += 1

print(result)

Submission status: Wrong Answer

Solution code:
import fractions
a, b, c, d = map(int, input().split())
e = c*d//int(fractions.gcd(c,d))
s = b-a+1
cb = b//c
db = b//d
eb = b//e
ca = a//c
da = a//d
ea = a//e
print(int(s - (cb - ca + db -da - eb + ea)))

Submission status: Wrong Answer

Solution code:
A,B,C,D = map(int,input().split())
# list_num = [i+A for i in range(B-A+1)]
# ans = 0
# for i in list_num:
#     if (i % C == 0 or i % D == 0):
#         continue
#     else:
#         ans += 1
# print(ans)
import math
num_C = math.floor(B/C) - math.ceil(A/C) + 1
num_D = math.floor(B/D) - math.ceil(A/D) + 1

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

lcm_C_D = lcm(C, D)

num_C_D = math.floor(B/lcm_C_D) - math.ceil(A/lcm_C_D) + 1

ans = (B - A +1) - (num_C + num_D - num_C_D)
print(ans)

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())
m = int(c*d/gcd(c,d))
if a%c < b%c:
  xc = (b-a+1)//c
elif a%c > b%c and b%c == 0 and a%c == 1:
  xc = (b-a+1)//c
elif a%c > b%c:
  xc = (b-a+1)//c +1
elif a%c == b%c and a%c == 0:
  xc = (b-a+1)//c +1
else:
  xc  = (b-a+1)//c

if a%d < b%d:
  xd = (b-a+1)//d
elif a%d > b%d and b%d == 0 and a%d == 1:
  xd = (b-a+1)//d
elif a%d > b%d:
  xd = (b-a+1)//d +1
elif a%d == b%d and a%d == 0:
  xd = (b-a+1)//d +1
else:
  xd  = (b-a+1)//d

if a%m < b%m:
  xm = (b-a+1)//m
elif a%m > b%m and b%m == 0 and a%m == 1:
  xm = (b-a+1)//m
elif a%m > b%m:
  xm = (b-a+1)//m +1
elif a%m == b%m and a%m == 0:
  xm = (b-a+1)//m +1
else:
  xm  = (b-a+1)//m

print(b-a+1-xc-xd+xm)

Submission status: Wrong Answer

Solution code:
import fractions

def antidiv(x,l,c,d):
    if x == l or x < 0:
        return l - (l // c) - (l // d) + 1
    else:
        return x - (x // c) - (x // d) 

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

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


aq = a // l
ar = a % l
bq = b // l
br = b % l
#print(br)

qq = bq - aq - 1
r1 = antidiv(l,l,c,d) - antidiv(ar-1,l,c,d)
r2 = antidiv(br,l,c,d)

if br != 0:
    ans = qq * antidiv(l,l,c,d) + r1 + r2
else:
    ans = (qq+1) * antidiv(l,l,c,d) + r1 + r2

print(ans)

Submission status: Wrong Answer

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


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

def count(st, n):
    return (b - st + 1) - int((b - st + 1) / n)


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


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


sum = 0
cc = 0
cflag = True
dflag = True
cdflag = True
for i in range(a, b + 1):
    if i % c == 0 and cflag:
        sum += count(i, c)
        cc += 1
        cflag = False
    if i % d == 0 and dflag:
        if dflag:
            sum += count(i, d)
            cc += 1
            dflag = False
        if i % c == 0 and cdflag:
            l = lcm(c, d)
            sum -= count(i, l)
            cc += 1
            cdflag = False
    if cc == 3:
        break
print(sum)

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)),int(b / lcm(c,d))+1):
    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),int(b / c)+1):
    if c * i >= a and c * i <= b and C == 0:
        C = c * i
        break

for i in range(int(a / d),int(b / d)+1):
    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 E == 0:
	e2 = 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 dfs(k):
    if k==-1:return 0
    if a==1:
        return int(b/k)
    else:
        return int(b/k)-int((a-1)/k)
def sp(k):
    for i in range(2,int((k**0.5)+1)):
        if k%i==0:
            return False
    return True
        
            
a,b,c,d=map(int,input().split())
e=c*d
ans=b-a+1-dfs(c)-dfs(d)
lprime=[]
for x in range(2,min(c,d)+1):
    if sp(x):
        lprime.append(x)

for j in lprime:
    while c%j==0 and d%j==0:
        c=c/j
        d=d/j
        e=e/j
ans+=dfs(e)
print(ans)

Submission status: Wrong Answer

Solution code:
A,B,C,D = map(int,input().split())
gcd = 0
if C>D:
  big = C
  small = D
else:
  big = C
  small = D
GCDa = big
while GCDa != 0:
  GCD = GCDa
  tmp = small
  small = big%small
  big = tmp
  GCDa = small
if C == D:
  gcd = C
if GCD == 1:
  ans = B-A+1-((B//C-(A-1)//C)+(B//D-(A-1)//D)-int(int(B/C)//D-(int(A-1)/B)//D))
else:
  gcd = C*D/GCD
  ans = B-A+1-((B//C-(A-1)//C)+(B//D-(A-1)//D)-int(int(B//gcd)-int((A-1)//gcd)))
print(ans)