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):
    if A==B:
        output=A
    n=max(A,B)
    m=min(A,B)
    r=100
    while r>0:
        r=n%m
        n=m
        m=r
    return n

def div(A,B,C):
    n1=A%C
    if n1==0:
        ans1=A//C
    else:
        ans1=A//C+1
    n2=B%C
    if n2==0:
        ans2=B//C
    else:
        ans2=(B-B%C)//C
    d=ans2-ans1+1
    if d>=1:
        ans3=d
    else:
        ans3=0
    return ans3

A,B,C,D=map(int,input().split())
num1=div(A,B,C)
num2=div(A,B,D)
n=gcd(C,D)
lcm=C*D//n
num3=div(A,B,lcm)
lastanswer=(B-A+1)-(num1+num2-num3)
print(lastanswer)

Submission status: Accepted

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


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

rng=b-a+1
x=b//c-(a-1)//c
y=b//d-(a-1)//d
z=b//e-(a-1)//e
print(rng-x-y+z)

Submission status: Accepted

Solution code:
from math import ceil
from fractions import gcd


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


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

A = max(b // c - ceil(a / c) + 1, 0)
B = max(b // d - ceil(a / d) + 1, 0)
C = max(b // lcm(c, d) - ceil(a / lcm(c, d)) + 1, 0)
print(max(b-a+1-(A+B-C), 0))

Submission status: Accepted

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

ce=b//c
if a<=ce*c and ce*c<=b:
    cs=1+((a-1)//c)
else:
    cs=ce+1

de=b//d
if a<=de*d and de*d<=b:
    ds=1+((a-1)//d)
else:
    ds=de+1

gg=(c*d)//gcd(c,d)
cde=b//gg
if a<=cde*gg and cde*gg<=b:
    cds=1+((a-1)//gg)
else:
    cds=cde+1

#print(cs,ce,ds,de,cds,cde)
print(int(b-a-ce-de+cs+ds+cde-cds))

Submission status: Accepted

Solution code:
from sys import stdin
import fractions


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


def main():
    A, B, C, D = [int(x) for x in stdin.readline().rstrip().split()]

    C_D_lcm = lcm(C, D)

    z = B//C_D_lcm - A//C_D_lcm

    if A % C == 0:
        x = (B//C)+1 - (A//C)
    else:
        x = (B//C) - A//C

    y = B//D - A//D

    print((B-A+1)-(x+y-z))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#2パターンWA

def gcd(x,y):
    while y != 0:
        x,y = y,x%y
    return x
def lcm(x,y):
    return x*y//gcd(x,y)

a, b, c, d = (int(i) for i in input().split())

V = b - a + 1
ans = 0
if V != 1:
    if a%c != 0:
        c_cnt = len(range(a+(c - a%c),b+1,c))
    else:
        c_cnt = len(range(a,b+1,c))

    if a%d != 0:
        d_cnt = len(range(a+(d - a%d),b+1,d))
    else:
        d_cnt = len(range(a,b+1,d))

    cd_lcm = lcm(c,d)
    if a%(cd_lcm) != 0:
        cd_cnt = len(range(a+(cd_lcm - a%(cd_lcm)),b+1,cd_lcm))
    else:
        cd_cnt = len(range(a,b+1,cd_lcm))

    ans = V - (c_cnt + d_cnt - cd_cnt)
else:
    if a%c != 0 and a%d != 0:
        ans = 1
print(ans)

Submission status: Accepted

Solution code:
import fractions

def resolve():
    a,b,c,d = map(int,input().split())
 
    dividable_by_c = b // c - (a-1) // c
    dividable_by_d = b // d - (a-1) // d
    lcm = c * d // fractions.gcd(c,d)
    dividable_by_c_and_d = b // lcm - (a-1) // lcm
 
    result = (b - a + 1) - dividable_by_c - dividable_by_d + dividable_by_c_and_d
    print(result)
resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:

from fractions import gcd

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

a, b, c, d = map(int, input().split())
# a以上なのでaを含む
a = a-1

# b以下の条件を満たす数
nb = b - b//c - b//d + b//lcm(c, d)
na = a - a//c - a//d + a//lcm(c, d)

print(nb - na)

Submission status: Accepted

Solution code:
import fractions
a,b,c,d = (int(i) for i in raw_input().split())

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

def get_num(n, c, d):
    num_of_c = n // c
    num_of_d = n // d
    num_of_cd = n // lcm(c,d)
    ret = n - num_of_c - num_of_d + num_of_cd
    return ret

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

Submission status: Accepted

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

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

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

def calc(ab, c, d):
    return ab - (ab//c + ab//d - ab//lcm(c,d))
  

num_a = calc(a, c, d)
num_b = calc(b, c, d)

print(num_b - num_a)

Submission status: Accepted

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

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
 
X = B//C - (A-1)//C
Y = B//D - (A-1)//D
CD = C*D // gcd(C, D)
XY = B//CD - (A-1)//CD
 
print(B-A+1-(X+Y-XY))

Submission status: Accepted

Solution code:
import fractions

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

x = fractions.gcd(C, D)
CD = x * (C // x) * (D // x)

Cm = B // C - (A - 1) // C
Dm = B // D - (A - 1) // D
CDm = B // CD - (A - 1) // CD

print(B + 1 - A - (Cm + Dm - CDm))

Submission status: Accepted

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()))

#AまででCでもDでも割り切れない数の個数
A_C = A // C
A_D = A // D
A_CD = A //(C*D // gcd(C,D))
countA = A - A_C - A_D + A_CD
#print(countA,A_C,A_D,A_CD)

#BまででCでもDでも割り切れない数の個数
B_C = B // C
B_D = B // D
B_CD = B //(C*D // gcd(C,D))
countB = B - B_C - B_D + B_CD
#print(countB,B_C,B_D,B_CD)

if A % C != 0 and A % D != 0 and A % C != 0 and A % D != 0:
    print(countB - countA + 1)
else:
    print(countB - countA)

Submission status: Accepted

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

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

def solve(a, b, c, d):
    div = ((b//c) - ((a-1)//c)) + ((b//d) - ((a-1)//d)) - ((b//lcm(c,d)) -
            ((a-1)//lcm(c,d))) 

    return (b - a + 1) - div

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

Submission status: Accepted

Solution code:
def main():
    tmp = list(map(int, input().split(" ")))
    a = tmp[0]
    b = tmp[1]
    c = tmp[2]
    d = tmp[3]

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


def gcd(c, d):
    big = max(c, d)
    small = min(c, d)
    if small == 0:
        return int(big)
    r = big % small
    return gcd(small, r)


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


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


main()

Submission status: Accepted

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

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

a,b,c,d=[int(x) for x in input().split()]

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

if A%C == 0:
    start1 = A // C
else:
    start1 = A // C
    start1 += 1


if A%D == 0:
    start2 = A // D
else:
    start2 = A // D
    start2 += 1


tmp = lcm(C, D)


if A%(C*D) == 0:
    start3 = A // tmp
else:
    start3 = A // tmp
    start3 += 1

end1 = B // C
end2 = B // D
end3 = B // tmp


res = (B - A + 1) - (end1 - start1 + 1) - (end2 - start2 + 1) + (end3 - start3 + 1)


print(res)

Submission status: Accepted

Solution code:
from math import factorial, ceil, floor # 階乗/切り上げ/切り捨て
from operator import itemgetter as ig
from collections import defaultdict as dd
# 定数
INF = float("inf")
MOD = int(1e9 + 7)
# 最大公約数 / 最小公倍数
def gcd(v1, v2):
    if v2 == 0:
        return v1
    return gcd(v2, v1 % v2)
def lcm(v1, v2):
    return (v1 // gcd(v1, v2)) * v2

# エントリーポイント
def main():
    A, B, C, D = map(int, input().split())
    def solve(v1, v2, d):
        return B // d - (A - 1) // d
    print((B - A + 1) - (solve(A, B, C) + solve(A, B, D) - solve(A, B, lcm(C, D))))

main()

Submission status: Accepted

Solution code:
import fractions
num = [int(i) for i in input().split()]
lower_limit = num[0]
upper_limit = num[1]
c = num[2]
d = num[3]

def count_multi(x):
  count = upper_limit//x- (lower_limit-1)//x                          
  return count

cd_gcd = fractions.gcd(c, d)

multi_c_d = count_multi(c) + count_multi(d) - count_multi(c*d//cd_gcd)
print(upper_limit - lower_limit + 1 - multi_c_d)

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

X = B//C - (A-1)//C
Y = B//D - (A-1)//D
CD = C*D // gcd(C, D)
XY = B//CD - (A-1)//CD

print(B-A+1-(X+Y-XY))

Submission status: Accepted

Solution code:
from fractions import gcd
 
a, b, c, d = map(int, input().split())
a = a - 1
 
def main():
    cd = c * d // gcd(c, d)
    print(b - a - f(c) - f(d) + f(cd))
 
def f(x):
    return b // x - a // x
 
main()

Submission status: Accepted

Solution code:
from fractions import gcd

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

def main():
    cd = c * d // gcd(c, d)
    print(b - a - f(c) - f(d) + f(cd))

def f(x):
    return b // x - a // x

main()

Submission status: Accepted

Solution code:
import fractions

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

# cとdの最大公約数
g = fractions.gcd(c, d)

# cとdの最小公倍数
l = c * d // g

# cとdで割り切れないb以下の数の個数
# (全体) - (cの倍数の個数) - (dの倍数の個数) + (cの倍数かつdの倍数の個数)
x = b - b // c - b // d + b // l

# cとdで割り切れない(a-1)以下の数の個数
y = (a - 1) - (a - 1) // c - (a - 1) // d + (a - 1) // l
    
print(x - y)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10000000)

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


def div(n,divn):
    #1からnまでの中で、divnで割り切れる数の数を示す
    return n//divn

def divor(n,div1,div2):
    #1からnまでの中で、div1,div2のどちらかで割り切れる数の数を示す
    return div(n,div1)+div(n,div2)-div(n,lcm(div1,div2))
def nondivor(n,div1,div2):
    #１からnまでの中で、div1,div2のどちらでも割り切れない数の数を示す
    return n-divor(n,div1,div2)


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


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import fractions

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


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


def ans(x):
    can_div_c_count = x // c
    can_div_d_count = x // d
    can_div_lcm_count = x // lcm(c, d)
    return x - (can_div_c_count + can_div_d_count - can_div_lcm_count)


print(ans(b)-ans(a-1))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import fractions
a,b,c,d = map(int, input().split())
e = a-1
less_than_e = e - (e//c) - (e//d) + (e//(c*d//fractions.gcd(c,d)))
less_than_b = b - (b//c) - (b//d) + (b//(c*d//fractions.gcd(c,d)))
if less_than_e < 0:
    less_than_e = 0
print(less_than_b - less_than_e)

Submission status: Accepted

Solution code:
import math

def gcd(x,y):
  M=max(x,y)
  m=min(x,y)
  while(M%m!=0):
      r=M%m
      M=m
      m=r
  return m



def f(x,y):
  z=int(x//y)
  return z


a,b,c,d=map(int,input().split())
g=gcd(c,d)
p=a-f(a,c)-f(a,d)+f(a,int(c*d/g))
q=b-f(b,c)-f(b,d)+f(b,int(c*d/g))
if(a%c!=0 and a%d!=0):
  print(q-p+1)
else:
  print(q-p)

Submission status: Accepted

Solution code:
# ABC 131 anti division

a, b, c, d = [int(el) for el in input().split(' ')]

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

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

def g(x, y):
    return(x // y + 1)

def f(x, y, z):
    return(x+1 - g(x, y) - g(x, z) + g(x, lcm(y, z)))

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

Submission status: Accepted

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

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

GcdCD = gcd(C,D)
LcmCD = (C*D)//GcdCD

s1 = B- ((B//C)+(B//D)-(B//LcmCD))
s2 = (A-1)-(((A-1)//C)+((A-1)//D)-((A-1)//LcmCD))
if s2 < 0:
    s2 = 0
print(s1-s2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#import math

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


def count_multiple(A, B, x):
    """B と　A-1 までのxの倍数の数をかぞえる
    引けばA,B範囲内の倍数の数"""
    min_value = (A - 1) // x
    max_value = B // x
    
    return max_value - min_value


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

# lcm = C * D // math.gcd(C, D)
lcm = C * D // gcd(C, D)
cnt = 0
for i, x in enumerate([C, D, lcm]):
    if i < 2:
        cnt += count_multiple(A, B, x)
    else:
        cnt -= count_multiple(A, B, x)

total = B - (A - 1)
ans = total - cnt
print(ans)

Submission status: Accepted

Solution code:
#28 ABC131C
a,b,c,d=map(int,input().split())
a-=1

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

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

Submission status: Accepted

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


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

b_cnt1 = b // c

b_cnt2 = b // d

b_cnt3 = b // lcm(c, d)

a_cnt1 = a // c

a_cnt2 = a // d

a_cnt3 = a // lcm(c, d)

print(b - (b_cnt1 + b_cnt2 - b_cnt3) - (a - (a_cnt1 + a_cnt2 - a_cnt3)))

Submission status: Accepted

Solution code:
import fractions
def n_div(k):
  return B // k - (A-1) // k
A, B, C, D = map(int, input().split())
CD = C * D // fractions.gcd(C,D)
ans = B - A + 1 - n_div(C) - n_div(D) + n_div(CD)
print(ans)

Submission status: Accepted

Solution code:
import fractions

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

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

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

Submission status: Accepted

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

def lcm(n, h):
    return (n * h) // fractions.gcd(n, h)

def slove(x, y, z):
	return (x//y) + (x//z) - (x//lcm(y,z))

print(w -(slove(b,c,d) - slove(a-1,c,d)) +1 )

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from fractions import gcd
def lcm(a, b):
    return a*b//gcd(a, b)

def count(a, b, x):
    start = a + (a-a%x)%x
    end = b - b%x
    return max((end-start)//x + 1, 0)

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

Submission status: Accepted

Solution code:
a,b,c,d=map(int,input().split())
import fractions as frc
gc=int(c*d/frc.gcd(c,d))

c_n=b//c-(a-1)//c
d_n=b//d-(a-1)//d
gc_n=b//gc-(a-1)//gc

res=b-(a-1)-(c_n+d_n-gc_n)

print(int(res))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
A, B, C, D = map(int, input().split())
E = A - 1
def func(a,b):
    if a%b == 0:
        return b
    return func(b,a%b)

c = min(C,D)
d = max(C,D)
G = func(d,c)
L = (C*D) // G

x = B - ((B//C)+(B//D)-(B//L))
y = E - ((E//C)+(E//D)-(E//L))
print(x-y)

Submission status: Accepted

Solution code:
def gcd(n, m):
    x = max(n, m)
    y = min(n, m)

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

    return x


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

    # count of factors of c which are less than or equal to b
    fclb = b // c
    # count of factors of d which are less than or equal to b
    fdlb = b // d

    # least common multiple
    lcm = c * d // gcd(c, d)  # greatest common divisor

    duplicated_b = b // lcm
    count_b = b - fclb - fdlb + duplicated_b

    fcla = (a - 1) // c
    fdla = (a - 1) // d
    duplicated_a = (a - 1) // lcm
    count_a = (a - 1) - fcla - fdla + duplicated_a

    score = count_b - count_a
    print(score)


main()

Submission status: Accepted

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

def gcd(v, w):
  while w:
    v, w = w, v % w
  return v

def calc(l, m, n):
  return l//m + l//n - l//lcm(m,n)

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

tmp = b-a+1
aa = calc((a-1), c, d)
bb = calc(b, c, d)
print(tmp - (bb - aa))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
A, B, C, D = map(int, input().split())
ans = 0
Am = 0
Bm = 0
df = 0
t = 0

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

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

t = B - A + 1
Am = (A - 1) // C + (A - 1) // D - (A - 1)//lcm(C, D)
Bm = B // C + B // D - B // lcm(C, D)
ans = t - (Bm - Am)

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
A, B, C, D = map(int, input().split())
ans = 0
Am = 0
Bm = 0
df = 0
t = 0

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

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

t = B - A + 1
Am = B // C - (A - 1) // C
Bm = B // D - (A - 1) // D
df = B // lcm(C, D) - (A - 1)//lcm(C, D)
ans = t - (Am + Bm) + df
print(ans)

Submission status: Accepted

Solution code:
A,B,C,D=map(int,input().split())
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
def lcm(x, y):
    return (x * y) // gcd(x, y)
  
d=B//D-(A-1)//D
c=B//C-(A-1)//C
tmp=lcm(C,D)
cd=B//(tmp)-(A-1)//(tmp)
print(B-A+1-c-d+cd)

Submission status: Accepted

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


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


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

s = b - a + 1
x = b // c - (a - 1) // c
y = b // d - (a - 1) // d
z = b // lcm(c, d) - (a - 1) // lcm(c, d)

print(s - (x + y) + z)

Submission status: Accepted

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

Submission status: Accepted

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

def diff(i, j, k):
	return i - (i//j) - (i//k) + (i//lcm(j,k))

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

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

Submission status: Accepted

Solution code:
def gcd(x, y): 
  
   while(y): 
       x, y = y, x % y 
  
   return x 
[a,b,c,d]=[int(x) for x in input().split()]
k=(c*d)//gcd(c,d)
count=(b-a+1)-((b//c-(a-1)//c)+(b//d-(a-1)//d)-(b//k-(a-1)//k))
print(count)

Submission status: Accepted

Solution code:
a,b,c,d=map(int,input().split())
B=b
A=a-1
def gcd(x,y):
    while y!=0:
        x,y=y,x%y
    return x
def mm(x,y):
    return int(x*y/gcd(x,y))
m_B=B-B//c-B//d+B//mm(c,d)
m_A=A-A//c-A//d+A//mm(c,d)
print(m_B-m_A)

Submission status: Accepted

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

def lcm(x, y):  #最小公倍数
    return (x * y) // fractions.gcd(x, y)

def cnt(n,x,y):
    return n-n//x-n//y+n//lcm(x,y)
  
print(cnt(b,c,d)-cnt(a-1,c,d))

Submission status: Accepted

Solution code:
import fractions

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

a-=1

buf_1=a-(a//c+a//d)+a//((c*d)//fractions.gcd(c,d))
buf_2=b-(b//c+b//d)+b//((c*d)//fractions.gcd(c,d))

print(buf_2-buf_1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
import fractions

def count_antidivision(n, x, y):
    """
    returns count of numbers from 1 to n which are not dividable by x nor y.
    """
    return n - n // x - n // y + n // (x * y // fractions.gcd(x, y))    

def main(A, B, C, D):
    return count_antidivision(B, C, D) - count_antidivision(A-1, C, D)

if __name__ == '__main__':
    A, B, C, D = list(map(int, input().split()))
    print(main(A, B, C, D))

Submission status: Accepted

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

Submission status: Accepted

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

A -= 1
tmp1 = A - A//C - A//D + A//((C*D)//fractions.gcd(C,D))
tmp2 = B - B//C - B//D + B//((C*D)//fractions.gcd(C,D))
answer = tmp2-tmp1

print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

#b以下からa以下を引く
#c and dで割り切れない→U - (cで割り切れる数+dで割り切れる数-cd最小公倍数で割り切れる数)
def cnt_num(cri):
  l = cri // c
  m = cri // d
  cd = c * d // gcd(c,d)
  lm = cri // cd
  return cri - (l + m - lm)
  
ans = cnt_num(b) - cnt_num(a-1)
print(ans)

Submission status: Accepted

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


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


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


def calc(t, x, y, z):
    return t // x + t // y - t // z


g = lcm(C, D)
bcnt = calc(B, C, D, g)
acnt = calc(A - 1, C, D, g)
print(B - bcnt - (A - 1 - acnt))

Submission status: Accepted

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


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


def calc(l, r, x):
    tmp = r // x - l // x
    if l % x == 0:
        tmp += 1
    return tmp


ccnt = calc(A, B, C)
dcnt = calc(A, B, D)
g = gcd(C, D)
cdcnt = calc(A, B, (C // g) * (D // g) * g)
print(B - A + 1 - (ccnt + dcnt - cdcnt))

Submission status: Accepted

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

Submission status: Accepted

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

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

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

b_num = B//C + B//D - B//lcm(C, D)
a_num = (A-1)//C + (A-1)//D - (A-1)//lcm(C, D)

print((B-A+1) -(b_num - a_num))

Submission status: Accepted

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


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

cc=b//c-(a-1)//c
dd=b//d-(a-1)//d
cd=b//lcm-(a-1)//lcm

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

Submission status: Accepted

Solution code:
import fractions

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

cc=b//c-(a-1)//c
dd=b//d-(a-1)//d
ee=b//(c * d // fractions.gcd(c, d))-(a-1)//(c * d // fractions.gcd(c, d))
    
print((b-a+1)-(cc+dd-ee))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def g_c_d(x, y):
    if y == 0:
        return x
    else:
        return g_c_d(y, x % y)


def l_c_d(x, y):
    return x * y // g_c_d(x, y)


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

Submission status: Accepted

Solution code:
import fractions

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

c_min = (a-1) // c
c_max = b // c
c_count = c_max - c_min

d_min = (a-1) // d
d_max = b // d
d_count = d_max - d_min

cd_min = (a-1) // (c * d // fractions.gcd(c, d))
cd_max = b // (c * d // fractions.gcd(c, d))
cd_count = cd_max - cd_min

print(b - (a-1) - c_count - d_count + cd_count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import fractions
l, r, p1, p2 = list(map(int, input().split()))
lcm = p1*p2 // fractions.gcd(p1, p2)
ans = (r-l+1) - (r//p1-(l-1)//p1) - (r//p2-(l-1)//p2) + (r//lcm)-(l-1)//lcm
print(ans)

Submission status: Accepted

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

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

E = C * D // gcd(C, D)
ac = (A-1) // C
ad = (A-1) // D
acd = (A-1) // E
bc = B // C
bd = B // D
bcd = B // E

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

Submission status: Accepted

Solution code:
from fractions import gcd

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

lcmcd = c * d / gcd(c, d)
lcmcd = int(lcmcd)

numb = b - (b//c) - (b//d) + (b//lcmcd)
numa = a-1 - ((a-1)//c) - ((a-1)//d) + ((a-1)//lcmcd)

print(numb-numa)

Submission status: Accepted

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

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

def nr_divide(a: int, b: int, x: int):
    count = b//x - a//x + 1
    if a % x != 0:
        count -= 1
    return count

def solve(A: int, B: int, C: int, D: int):
    E = my_lcm(C, D)
    N = B - A + 1
    nrC = nr_divide(A, B, C)
    nrD = nr_divide(A, B, D)
    nrE = nr_divide(A, B, E)
    count = N - nrC - nrD + nrE
    print(count)
    return

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)
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

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

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

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

res_b = b - b // c - b // d + b // cd
a -= 1
res_a = a - a // c - a // d + a // cd

print(res_b - res_a)

Submission status: Accepted

Solution code:
import fractions

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

a_c = (a - 1) // c
a_d = (a - 1) // d
a_cd = (a - 1) // (c * d // fractions.gcd(c, d))

b_c = b // c
b_d = b // d
b_cd = b // (c * d // fractions.gcd(c, d))

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

Submission status: Accepted

Solution code:
import math

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

A,B,C,D=map(int,input().split()[:4])

total=B-A+1

C_div=B//C-(A-1)//C
D_div=B//D-(A-1)//D

CD=lcm(C,D)

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

print(total-C_div-D_div+CD_div)

Submission status: Accepted

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

nc = (b//c) - ((a-1)//c)
# print(nc)

nd = (b//d) - ((a-1)//d)
# print(nd)

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

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

ncd = (b//lcm(c,d)) - ((a-1)//lcm(c,d))
# print(ncd)

u = b - a + 1
# print(u)

ans = u - nc - nd + ncd

print(ans)

Submission status: Accepted

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

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

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

x=B//C-(A-1)//C
y=B//D-(A-1)//D
s=lcm(C, D)
z=B//s-(A-1)//s

print(int(B-A+1-x-y+z))

Submission status: Accepted

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

C, D = min(C,D), max(C,D)

dividable_C = B//C - (A-1)//C
dividable_D = B//D - (A-1)//D

x = C*D
r = D%C
while r!=0:
    D = C
    C = r
    r = D%C
saisho_ko_baisu = x//C

dividable_CD = (B//saisho_ko_baisu) - ((A-1)//saisho_ko_baisu)

print(B - A + 1 - (dividable_C + dividable_D - dividable_CD))

Submission status: Accepted

Solution code:
import fractions

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

c_n = (b // c) - ((a - 1) // c)
d_n = (b // d) - ((a - 1) // d)

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

ans = (b - a + 1) - (c_n + d_n - lcm_n)
print(ans)

Submission status: Accepted

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

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

def cal(n):
  if a%n ==0 and b%n ==0:
    return b//n - a//n + 1
  else:
    return b//n - a//n
    
cd = lcm(c, d)
ans = (b-a+1) - (b//c - (a-1)//c) - (b//d - (a-1)//d) + (b//cd - (a-1)//cd)
print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd

lcm = lambda x, y: x*y//gcd(x, y)
calc = lambda mod, a, b: (b//mod-a//mod) if a%mod != 0 else (b//mod-a//mod+1) 

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

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