Problem:


Score : 300 points

Problem Statement

You are given four integers A, B, C, and D. Find the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Constraints

1\leq A\leq B\leq 10^{18}

1\leq C,D\leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Sample Input 1

4 9 2 3

Sample Output 1

2

5 and 7 satisfy the condition.

Sample Input 2

10 40 6 8

Sample Output 2

23

Sample Input 3

314159265358979323 846264338327950288 419716939 937510582

Sample Output 3

532105071133627368



Sample input:
4 9 2 3


Sample output:
2

Submission status: Accepted

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

Submission status: Accepted

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

import fractions

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


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

print(f(B, C, D) - f(A-1, C, D))

Submission status: Accepted

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

def gcd(a,b):
	#a = bk + r
	if b == 0: return a
	return gcd(b, a % b)

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

# how many multiples of c among integers between a and b inclusive
def f(a,b,c):
	if a == 0:
		return b/c + 1
	return b/c  - (a-1)/c 

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import fractions

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

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

gcd_cd = lcm(c,d)

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

a_numbers = a_c_numbers + a_d_numbers

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

if a % gcd_cd == 0:
    gcd_cd_numbers = b//gcd_cd - a//gcd_cd + 1
else:
    gcd_cd_numbers = b//gcd_cd - a//gcd_cd

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from fractions import *
from math import *

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

a, b, c, d = map(int, input().split())
def f(x):
    xc = x // c
    xd = x // d
    cd = x // lcm(c, d)
    return x - xc - xd + cd

print(f(b) - f(a-1))

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)


C_num = B // C - A // C

D_num = B // D - A // D

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


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

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import fractions
import sys
from functools import reduce
import math

input = sys.stdin.readline

def main():
    a, b, c, d = input_list()
    l = lcm_base(c, d)
    a -= 1
    ans_b = b - (b//c + b//d) + b//l
    ans_a = a - (a//c + a//d) + a//l
    print(ans_b - ans_a)


def divide_list(l, args):
    result = []
    pre = 0
    for i, a in enumerate(args):
        if i != 0:
            pre = args[i-1]
        result.append(l[pre:a])
        if i == len(args) - 1:
            result.append(l[a:])
    return result

def divide_two(n):
    c = 0
    while True:
        if c >= 2:
            break
        if n % 2 != 0:
            break
        n //= 2
        c += 1
    return c

def get_camulative(l):
    import itertools
    # 累積和
    return [0] + list(itertools.accumulate(l))

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


def input_list():
    return list(map(int, input().split()))


def input_list_str():
    return list(map(str, input().split()))


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


def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


def gcd(*numbers):
    return reduce(fractions.gcd, numbers)


def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from fractions import gcd
import math

def lcm(x, y):
    return (x * y) // gcd(x, y)
a,b,c,d = map(int,input().split())
all = b - a + 1
if c == 1 or d == 1:
    print(0)
    exit()

cd = lcm(c, d)

cs = b//c - math.ceil(a/c) + 1
ds = b//d - math.ceil(a/d) + 1
cds = b//cd - math.ceil(a/cd) + 1

print(all - cs - ds + cds)

Submission status: Accepted

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

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

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

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

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

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
from copy import deepcopy
from decimal import Decimal
import fractions
alf = list("abcdefghijklmnopqrstuvwxyz")
ALF = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
#import numpy as np
INF = float("inf")
#d = defaultdict(int)
#d = defaultdict(list)
#N = int(input())
#A = list(map(int,input().split()))
#S = list(input())[:-1]
#S.remove("\n")
#N,M = map(int,input().split())
#S,T = map(str,input().split())
#A = [int(input()) for _ in range(N)]
#S = [list(input())[:-1] for _ in range(N)]
#A = [list(map(int,input().split())) for _ in range(N)]

a,b,c,d=map(int,input().split())
e=fractions.gcd(c,d)
f=c*d//e
ans1=a-1
ans1-=(a-1)//c
ans1-=(a-1)//d
ans1+=(a-1)//f
ans2=b
ans2-=b//c
ans2-=b//d
ans2+=b//f
print(ans2-ans1)

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit


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


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


def main():
    input = stdin.buffer.readline
    a, b, c, d = map(int, input().split())
    l = lcm(c, d)
    ans = 0
    ans += (l - (l // c + l // d - 1)) * (b // l - (a - 1)//l)
    tmp_b = b % l
    ans += tmp_b - (tmp_b // c + tmp_b // d)
    tmp_a = (a - 1) % l
    ans -= tmp_a - (tmp_a // c + tmp_a // d)
    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Accepted

Solution code:
import fractions

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

#lis = list(range(a,b+1))

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

if a % c == 0:
    csyou = (b//c) - (a//c) +1
else:
    csyou = (b//c) - (a//c)
    
if a % d == 0:
    dsyou = (b//d) - (a//d) +1
else:
    dsyou = (b//d) - (a//d)
    
if a % lcm == 0:
    lcmsyou = (b//lcm) - (a//lcm)+1
else:
    lcmsyou = (b//lcm) - (a//lcm)

print((b-a+1)-(csyou+dsyou-lcmsyou))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

e = c * d // fra.gcd(c,d)
x = b//c + b//d - (a-1)//c - (a-1)//d
y = b//e - (a-1)//e

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

Submission status: Accepted

Solution code:
import math
import fractions

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

count = 0
if A % C == 0:
    div_C = B // C - A // C + 1
else:
    div_C = B // C - A // C

if A % D == 0:
    div_D = B // D - A // D + 1
else:
    div_D = B // D - A // D

if A % lcm == 0:    
    div_C_D = B // lcm - A // lcm + 1
else:
    div_C_D = B // lcm - A // lcm

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

Submission status: Accepted

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

print(B - A)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
# 最小公倍数(mathは3.5以降) fractions
# 参考　https://note.nkmk.me/python-gcd-lcm/

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y) # 「//」はフロートにせずにintにする
def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)

# 初期入力
from functools import reduce
import fractions #(2020-0405  fractions→math)
import sys
input = sys.stdin.readline
A,B,C,D = (int(x) for x in input().split())

c_multi_B =B // C #Cの倍数
d_multi_B =B // D #Dの倍数
least_common_multi =lcm(C,D)
cd_common_multi_B =B //least_common_multi

c_multi_A =(A -1) // C #Cの倍数
d_multi_A =(A -1) // D #Dの倍数
least_common_multi =lcm(C,D)
cd_common_multi_A =(A -1)//least_common_multi

#A以上、B以下
all =B -A +1

ans =all - (c_multi_B -(c_multi_A )) -(d_multi_B -(d_multi_A )) + cd_common_multi_B -cd_common_multi_A
print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd

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

c = (B // C) - ((A-1) // C)
d = (B // D) - ((A-1) // D)

CD = C * D // gcd(C, D)
cd = (B // (CD)) - ((A-1) // (CD))

print((B - A + 1) - (c + d - cd))

Submission status: Accepted

Solution code:
import fractions

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

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

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

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

Submission status: Accepted

Solution code:
import fractions

B,A,C,D = map(int,input().split())
B = B - 1
c = A//C - B//C
d = A//D - B//D
CD = C*D//fractions.gcd(C,D)
cd = A//CD - B//CD

print(A-B-c-d+cd)

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)

cd_lcm = lcm(C, D)

# Cで割れるものを探す。
C_onetoB = B//C
C_onetoA = (A-1)//C
C_div = C_onetoB - C_onetoA

# Dで割れるものを探す。
D_onetoB = B//D
D_onetoA = (A-1)//D
D_div = D_onetoB - D_onetoA

#CDの最小公倍数で割れるものを探す。
cd_lcm_onetoB = B//cd_lcm
cd_lcm_onetoA = (A-1)//cd_lcm
cd_lcm_div = cd_lcm_onetoB - cd_lcm_onetoA

print(B-A+1 - C_div - D_div + cd_lcm_div)

Submission status: Accepted

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





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


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

Submission status: Accepted

Solution code:
def G(x,y):
    if(x<y):
        x,y=y,x
    if(y==0):
        return x
    return G(y,x%y)


a,b,c,d=map(int,input().split())
ans=0
# print(a,b,c,d,b//c,a//c)
ans+=(b//c-a//c+(a%c==0))
# print(ans)
ans+=(b//d-a//d+(a%d==0))
# print(ans)
lcm=c*d//G(c,d)
ans-=(b//lcm-a//lcm+(a%lcm==0))
print(b-a+1-ans)

Submission status: Accepted

Solution code:
from fractions import gcd

def main():
    A, B, C, D = map(int, input().split())
    nC = B//C - (A-1)//C
    nD = B//D - (A-1)//D
    CD = (C*D)//gcd(C, D)
    nCD = B//CD - (A-1)//CD
    cnt = nC + nD - nCD
    ans = B - A + 1 - cnt
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

from fractions import gcd
m=(c*d)//gcd(c,d)

A=b//c-(a-1)//c # a-bのうち、cの倍数のもの
B=b//d-(a-1)//d # a-bのうち、dの倍数のもの
C=b//m-(a-1)//m # a-bのうち、cとdの最小公倍数の倍数のもの
ans=(b-a+1)-(A+B-C)
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)

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

a, b, c, d = map(int, input().split())
divc = b // c - a // c
if a % c == 0:
    divc += 1
divd = b // d - a // d
if a % d == 0:
    divd += 1
    
lcm = c * d // gcd(c, d)
divlcm = b // lcm - a // lcm
if a % lcm == 0:
    divlcm += 1


div_or = divc + divd - divlcm
print(int(b - a + 1 - div_or))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
line = input().split()
import fractions
lcm = (int(line[2])*int(line[3]) //fractions.gcd(int(line[2]),int(line[3])))
 
pos_c = int(line[1])//int(line[2]) 
pos_d = int(line[1])//int(line[3])
pos_lcm = int(line[1])//lcm
 
pos_c1 = (int(line[0])-1)//int(line[2])
pos_d1 = (int(line[0])-1)//int(line[3])
pos_lcm1 = (int(line[0])-1)//lcm
 
b = int(line[1]) - pos_c - pos_d + pos_lcm
a = int(line[0]) - 1 - pos_c1 - pos_d1 + pos_lcm1
 
print(b-a)

Submission status: Accepted

Solution code:
line = input().split()
import fractions
lcm = (int(line[2])*int(line[3]) //fractions.gcd(int(line[2]),int(line[3])))

pos_c = int(int(line[1])//int(line[2])) 
pos_d = int(int(line[1])//int(line[3])) 
pos_lcm = int(int(line[1])//lcm)

pos_c1 = int((int(line[0])-1)//int(line[2])) 
pos_d1 = int((int(line[0])-1)//int(line[3])) 
pos_lcm1 = int((int(line[0])-1)//lcm)

b = int(line[1]) - pos_c - pos_d + pos_lcm
a = int(line[0]) - 1 - pos_c1 - pos_d1 + pos_lcm1

print(b-a)

Submission status: Accepted

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

from fractions import gcd
m=(c*d)//gcd(c,d)

A=b//c-(a-1)//c # a-bのうち、cの倍数のもの
B=b//d-(a-1)//d # a-bのうち、dの倍数のもの
C=b//m-(a-1)//m # a-bのうち、cとdの最小公倍数の倍数のもの
ans=(b-a+1)-(A+B-C)
print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd


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

Submission status: Accepted

Solution code:
def gcd(a,b):
  if b!=0:
    return gcd(b, a%b)
  else:
    return a
  
def lcm(a,b):
  return a//gcd(a,b)*b
  
def f(x, c, d):
  a = x
  a -= x//c
  a -= x//d
  a += x//lcm(c,d)
  return a
  
A,B,C,D = list(map(int, input().split()))

out = f(B, C, D) - f(A-1, C, D)
print(out)

Submission status: Accepted

Solution code:
import fractions
# input
left, right, fizz, buzz = map(int, input().split())

# define
nums = right - left + 1
fizzbuzz = fizz * buzz // fractions.gcd(fizz, buzz)

# main
if left % fizz == 0:
  fizz_nums = right // fizz - left // fizz + 1
else:
  fizz_nums = right // fizz - (left // fizz + 1) + 1

if left % buzz == 0:
  buzz_nums = right // buzz - left // buzz + 1
else:
  buzz_nums = right // buzz - (left // buzz + 1) + 1
  
if left % fizzbuzz == 0:
  fizzbuzz_nums = right // fizzbuzz - left // fizzbuzz + 1
else:
  fizzbuzz_nums = right // fizzbuzz - (left // fizzbuzz + 1) + 1
  
# output 
print(nums - (fizz_nums + buzz_nums - fizzbuzz_nums))
  
# debug
"""
debug = int(input())
if debug == (nums - (fizz_nums + buzz_nums - fizzbuzz_nums)):
  print("AC")
else:
  print("WA")
"""

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

S = B - A + 1

canC = B // C - (A - 1) // C
canD = B // D - (A - 1) // D
canCD = B // LCM(C, D) - (A - 1) // LCM(C, D)

print(S - (canC + canD) + canCD)

Submission status: Accepted

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

def solve(a,b,c,d):
    x,y = c,d
    def gcd(x,y):
        while y != 0:
            z = y
            y = x % y
            x = z
        return x

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

    mult_b = b // c
    mult_b += b // d
    duplicates = b // lcm
    mult_b -= duplicates

    mult_a = (a-1) // c
    mult_a += (a-1) // d
    duplicates_a = (a-1) // lcm
    mult_a -= duplicates_a

    result = b - (a-1) - (mult_b - mult_a)
    print(result)

solve(a,b,c,d)

Submission status: Accepted

Solution code:
import fractions

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

U = B - A  + 1
c = (B // C) + (-A // C) + 1
d = (B // D) + (-A // D) + 1
x = C * D // fractions.gcd(C , D)
e = (B // x) + (-A // x) + 1
print(U - c - d + e)

Submission status: Accepted

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

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

cnt = 0
cnt += ((B // C) - ((A-1) // C))
cnt += ((B // D) - ((A-1) // D))

tmp = ((B//lcm(C,D)) - ((A-1)//lcm(C,D)))

ans = B-A+1-cnt+tmp
print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd

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

A,B,C,D = list(map(int,input().split()))
C1 = B//C
C2 = (A-1)//C
C3 = B//D
C4 = (A-1)//D
C5 = B//lcm(C,D)
C6 = (A-1)//lcm(C,D)
print((B-(A-1))-((C1-C2)+(C3-C4)-(C5-C6)))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import fractions
a, b, c, d = map(int, input().split())
l = c * d // fractions.gcd(c, d)
n = b - a + 1
print(n - (((b//c) - (a//c) + (a%c == 0)) + ((b//d) - (a//d) + (a%d == 0)) - ((b//l) - (a//l) + (a%l == 0))))

Submission status: Accepted

Solution code:
from fractions import gcd

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

A,B,C,D = list(map(int,input().split()))
C1 = B//C
C2 = (A-1)//C
C3 = B//D
C4 = (A-1)//D
C5 = B//lcm(C,D)
C6 = (A-1)//lcm(C,D)
print((B-(A-1))-((C1-C2)+(C3-C4)-(C5-C6)))

Submission status: Accepted

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

while True:
  if X > Y:
    t = Y
    Y = X % Y
    X = t
  else:
    Y = Y % X
  if Y == 0:
    break;

a = (A - 1) - (A - 1)//C - (A - 1)//D + (A - 1)//(C*D//X)
b = B - B//C - B//D + B//(C*D//X)

print(b - a)

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())
print((B-(B//C)-(B//D)+(B//min(B+1,lcm(C, D))))-((A-1)-((A-1)//C)-((A-1)//D)+((A-1)//min(A,lcm(C, D)))))

Submission status: Accepted

Solution code:
from fractions import gcd
A, B, C, D = map(int, input().split())
A-=1
lcm=C*D//gcd(C,D)
#B以下の個数
b = B-(B//C)-(B//D)+B//lcm
#A以下の個数
a = A-(A//C)-(A//D)+A//lcm
print(b-a)

Submission status: Accepted

Solution code:
import fractions

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

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

divisible_c = (b // c) - (a // c)
divisible_d = (b // d) - (a // d)
divisible_cd = (b // lcm) - (a // lcm)

print((b - a) - (divisible_c + divisible_d - divisible_cd))

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

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


def f(x, c, d):
    num_c = x // c
    num_d = x // d
    num_c_d = x // lcm(c,d)

    return x - num_c - num_d + num_c_d

print( f(B, C, D) - f(A-1, C, D) )

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)

n_C = B//C - (A-1)//C # Cで割り切れる数の数
n_D = B//D - (A-1)//D
CandD = lcm(C,D)
n_CandD = B//CandD - (A-1)//CandD

# CでもDでも割けれない数 = 全体 - CでもDでも割り切れる数
ans = B-A+1 - (n_C + n_D - n_CandD)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from fractions import gcd
a, b, c, d = map(int, input().split())
l = c // gcd(c, d) * d
def f(x): # # of num lt or eq to x undivisable by c nor d
    return x - x // c - x // d + x // l
print(f(b) - f(a - 1))

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

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

cc = b // c - mod(a,c)+1
dd = b // d - mod(a,d)+1
cd = b // lcm(c,d) - mod(a,lcm(c,d))+1
print(b-a+1-cc-dd+cd)

Submission status: Accepted

Solution code:
a,b,c,d=map(int,input().split())
from fractions import gcd # ~3.4
import math # 3.5~
# lcm(a, b) = a * b / gcd(a, b)
cd=int(c*d/gcd(c,d))
e=(b-a+1)
f=(b//c-a//c)
if c*(a//c)==a:
  f+=1
g=(b//d-a//d)
if d*(a//d)==a:
  g+=1
h=(b//cd-a//cd)
if cd*(a//cd)==a:
  h+=1
#print(e,f,g,h)
print(e-f-g+h)

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 amari(x,y):
    return x//y if x%y==0 else x//y+1

cnum=b//c-amari(a,c)+1
dnum=b//d-amari(a,d)+1
cdnum=b//lcm(c,d)-amari(a,lcm(c,d))+1

print(b-a+1-cnum-dnum+cdnum)

Submission status: Accepted

Solution code:
import numpy as np
import functools
import math
import collections
import scipy
import fractions
import itertools
 
def solve():
  a, b, c, d = map(int, input().split())
  a -= 1
  lc = (c*d)//fractions.gcd(c,d)
  ans = (b-a)-(b//c-a//c)-(b//d-a//d)+(b//lc-a//lc)
  print(ans)
  return 0
 
if __name__ == "__main__":
  solve()

Submission status: Accepted

Solution code:
import fractions

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

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

Submission status: Accepted

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

if A%C!=0 and A%D!=0:
    print((B-A+1)-((B-(A//C+1)*C)//C+1+(B-(A//D+1)*D)//D+1-(((B-(A//L+1)*L)//L)+1)))
elif A%C==0 and A%D!=0:
    print((B-A+1)-(((B-(A//C)*C)//C+1)+((B-(A//D+1)*D)//D+1)-((B-(A//L+1)*L)//L+1)))
elif A%C!=0 and A%D==0:
    print((B-A+1)-((B-(A//C+1)*C)//C+1+(B-(A//D)*D)//D+1-(B-(A//L+1)*L)//L-1))
elif A%C==0 and A%D==0 and A%L==0:
    print((B-A+1)-((B-(A//C)*C)//C+1+(B-(A//D)*D)//D+1-(B-(A//L)*L)//L-1))
elif A%C==0 and A%D==0 and A%L!=0:
    print((B-A+1)-((B-(A//C)*C)//C+1+(B-(A//D)*D)//D+1-(B-(A//L+1)*L)//L-1))

Submission status: Accepted

Solution code:
from fractions import gcd

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
try:
    from typing import List
except ImportError:
    pass


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


def solve(A: int, B: int, C: int, D: int):
    cdl = C // gcd(C, D) * D
    A -= 1
    print(
        (B - A)
        - (B // C - A // C) - (B // D - A // D)
        + (B // cdl - A // cdl)
    )


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    D = int(next(tokens))  # type: int
    solve(A, B, C, D)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

def count_divisor(a, b, c):
    return b//c - a//c + (a%c == 0)
    
a, b, c, d = map(int, input().split())
ans = b - a + 1
lcm = int(c * d / gcd(c, d))
ans -= count_divisor(a, b, c)
ans -= count_divisor(a, b, d)
ans += count_divisor(a, b, lcm)
print(ans)

Submission status: Accepted

Solution code:

def gcd(c, d):
    if c < d:
        return gcd(d, c)
    
    while d != 0:
        s = c % d
        c = d
        d = s
        
    return c
    
def lcm(c, d):
    return c * d // gcd(c, d)

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


c = B // C - A // C if A % C == 0 else B // C - (A // C + 1)
d = B // D - A // D if A % D == 0 else B // D - (A // D + 1)

LCM = lcm(C, D)

LCM = B // LCM - A // LCM if A % LCM == 0 else B // LCM - (A // LCM + 1)

c += 1
d += 1
LCM += 1

#print(c, d, LCM)

print(B - A + 1 - c - d + LCM)

Submission status: Accepted

Solution code:
import fractions

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

Submission status: Accepted

Solution code:

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n): return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
# from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx
# from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop
from fractions import gcd
# from math import gcd
def lcm(x, y):
    return (x * y) // gcd(x, y)
a,b,c,d=n1()
a1=b//c
a2=(a-1)//c
b1=b//d
b2=(a-1)//d
l=lcm(c,d)
c1=b//l
c2=(a-1)//l
ans=a1-a2+b1-b2-c1+c2
print(b-a+1-ans)

Submission status: Accepted

Solution code:
A,B,C,D=map(int, input().split())
import fractions
def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数

if (B-(A+(C-A%C)%C))<0:
    cs=0
else:
    cs=(B-(A+(C-A%C)%C))//C+1
if (B-(A+(D-A%D)%D))<0:
    ds=0
else:
    ds=(B-(A+(D-A%D)%D))//D+1
E=lcm(C,D)
if (B-(A+((E)-A%(E))%(E)))<0:
    cds=0
else:
    cds=(B-(A+((E)-A%(E))%(E)))//(E)+1

#print(cs, ds, cds)
print(B-A+1-(cs+ds-cds))

Submission status: Accepted

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

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

e=lcm(c,d)

i_max=b//c
j_max=b//d
k_max=b//e

i_min=a//c
if a%c==0:
    i_min-=1
j_min=a//d
if a%d==0:
    j_min-=1
k_min=a//e
if a%e==0:
    k_min-=1

i=i_max-i_min
j=j_max-j_min
k=k_max-k_min

print(b-a+1-(i+j-k))