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())
under_b = b // c + b // d - b // (c * d // fractions.gcd(c, d))
under_a = (a - 1) // c + (a - 1) // d - (a - 1) // (c * d // fractions.gcd(c, d))
print(b - (a - 1) - (under_b - under_a))

Submission status: Accepted

Solution code:
from fractions import gcd

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

sumB = B - B // C - B // D + B // lcm_CD
sumA = (A - 1) - (A - 1) // C - (A - 1) // D + (A - 1) // lcm_CD

print(sumB - sumA)

Submission status: Accepted

Solution code:
import fractions
A, B, C, D = map(int, input().split())
cnt = 0
ac = (A-1) // C
bc = B // C
ad = (A-1) // D
bd = B // D
gcd_b = B // (C*D//fractions.gcd(C, D))
gcd_a = (A-1) // (C*D//fractions.gcd(C, D))
total = B - A + 1
a = bc - ac
b = bd - ad
gcdx = gcd_b - gcd_a

print(total-a-b+gcdx)

Submission status: Accepted

Solution code:
import math
from decimal import Decimal
#from decimal import Decimal
a, b, c, d = map(Decimal, input().split())

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

def count(a, c, d):
    tmp_a= a - math.floor(a/c) - math.floor(a/d)
    tmp_a += math.floor(a/((c*d)/gcd(c, d)))
    return tmp_a

print(int(count(b,c,d))-int(count(a-1, c, d)))

Submission status: Accepted

Solution code:
# from math import factorial,sqrt
# from itertools import permutations as permus
from fractions import gcd
# from collections import deque,Counter
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

# import numpy as np
# import scipy as scp

# 入力の受け取り
a,b,c,d = map(int,input().split())

def lcm(x:int, y:int)->int:
    """
    return Least common multiple (最小公倍数)
    """
    return (x * y) // gcd(x, y)

e = lcm(c,d)

if c == 1 or d ==1:
    ans = 0
else:
    ans = b-a+1
    minus1 = b//c - (a-1)//c
    minus2 = b//d - (a-1)//d
    plus = b//e - (a-1)//e
    ans = ans -minus1 -minus2 +plus

print(ans)
# print("{:.10f}".format(ans))
# print(*ans)   # unpackして出力。間にスペースが入る
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a,b,c,d = map(int,input().split())
all_val = b-a+1
import fractions
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
if a%c == 0:
  c_val = b//c-a//c+1
else:
  c_val = b//c-a//c
if a%d == 0:
  d_val = b//d-a//d+1
else:
  d_val = b//d-a//d
if a%lcm(c,d) == 0:
  lcm_val = b//lcm(d,c)-a//lcm(d,c)+1
else:
  lcm_val = b//lcm(d,c)-a//lcm(d,c)
print(all_val-c_val-d_val+lcm_val)

Submission status: Accepted

Solution code:

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

ac=(A-1)//C
ad=(A-1)//D
bc=B//C
bd=B//D
ae=(A-1)//E
be=B//E

s= bc-ac
t= bd-ad
u= be-ae

print(B-A+1 - s -t +u)

Submission status: Accepted

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

Submission status: Accepted

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

ac=(A-1)//C
ad=(A-1)//D
bc=B//C
bd=B//D
ae=(A-1)//E
be=B//E

s= bc-ac
t= bd-ad
u= be-ae

print(B-A+1 - s -t +u)

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 = [int(_) for _ in 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 = b - a + 1
ans_ca = a//c
ans_cb = b//c
ans_c = ans_cb - ans_ca
if a%c== 0:
  ans_c += 1
  
ans_da = a//d
ans_db = b//d
ans_d = ans_db - ans_da
if a%d == 0:
  ans_d += 1

x = c
y = d
for i in range(b):
  n = y%x
  if n == 0:
    break
  y = x
  x = n
z = int(c*d/x)
ans_cda = a//z
ans_cdb = b//z
ans_cd = ans_cdb - ans_cda
if a%z== 0:
  ans_cd += 1
  
print(e-(ans_c+ans_d-ans_cd))

Submission status: Accepted

Solution code:
import sys
from bisect import *
from heapq import *
from collections import *
from itertools import *
from functools import *
from math import *
from fractions import *

sys.setrecursionlimit(100000000)
input = lambda: sys.stdin.readline().rstrip()

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

Submission status: Accepted

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

import fractions

f=fractions.gcd(c,d)           #最大公約数
f2=c*d//f                          #最小公倍数

# a未満でcかdで割れるかず
a-=1
x = a//c
y = a//d
z = a//f2
ac = x+y-z

# b以下でcかdで割れるかず
x = b//c
y = b//d
z = b//f2
bc = x+y-z

print(b-a-(bc-ac))

Submission status: Accepted

Solution code:
import sys
from heapq import heappush, heappop
from fractions import gcd
#input = sys.stdin.readline

def main():
    a,b,c,d=map(int,input().split())
    dc=int(d*c/gcd(d,c))
    print(b-a+1-(b//c-(a-1)//c)-(b//d-(a-1)//d)+(b//(dc)-(a-1)//(dc)))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import fractions

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

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

a -= 1

tmp_b = b - (b//c + b//d - b//c_d)
tmp_a = a - (a//c + a//d - a//c_d)

print(tmp_b-tmp_a)

Submission status: Accepted

Solution code:
a,b,c,d = map(int, input().split())
from functools import reduce
import fractions
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

def solve(tmp,c,d):
    num=lcm(c,d)
    ans = tmp//c + tmp//d - tmp//num
    return tmp-ans

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

Submission status: Accepted

Solution code:
# import bisect
# import copy
import fractions
#import math


# import numpy as np
# from collections import Counter, deque
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product

def resolve():
    A, B, C, D = map(int, input().split())
    A = A - 1
    cntA = A - A // C - A // D + A // (C * D // fractions.gcd(C, D))
    cntB = B - B // C - B // D + B // (C * D // fractions.gcd(C, D))
    print(cntB - cntA)


resolve()

Submission status: Accepted

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

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

c_min = a + (c - a % c) if (a % c) != 0 else a
c_max = b - b % c
d_min = a + (d - a % d) if (a % d) != 0 else a
d_max = b - b % d
e_min = a + (e - a % e) if (a % e) != 0 else a
e_max = b - b % e

n_c = (c_max - c_min) // c + 1 if c_min <= c_max else 0
n_d = (d_max - d_min) // d + 1 if d_min <= d_max else 0
n_e = (e_max - e_min) // e + 1 if e_min <= e_max else 0

ans = b - a + 1 - n_c - n_d + n_e

print(ans)

Submission status: Accepted

Solution code:
import fractions

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


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


nA = (A - 1) // C + (A - 1) // D - (A - 1) // lcm(C, D)
nA = A - 1 - nA
nB = B // C + B // D - B // lcm(C, D)
nB = B - nB
print(nB - nA)

Submission status: Accepted

Solution code:
# ABC131 C
from fractions import gcd
a,b,c,d=map(int,input().split())
lcm=c*d//gcd(c,d)
c_n=b//c-(a-1)//c
d_n=b//d-(a-1)//d
lcm_n=b//lcm-(a-1)//lcm
print(int(b-a+1-c_n-d_n+lcm_n))

Submission status: Accepted

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

B_C = B // C
B_D = B // D
B_CD = B // ((C * D) // fractions.gcd(C, D))

B_count = B - B_C - B_D + B_CD

A_C = (A - 1) // C
A_D = (A - 1) // D
A_CD = (A - 1) // ((C * D) // fractions.gcd(C, D))

A_count = (A - 1) - A_C - A_D + A_CD

print(B_count - A_count)

Submission status: Accepted

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

def cnt_val(n):
    cdiv = n // c
    ddiv = n // d
    ldiv = n // lcm
    return n - cdiv - ddiv + ldiv

print(cnt_val(b) - cnt_val(a-1))

Submission status: Accepted

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

import fractions

def calc(x,y,z):
    div_y = x//y
    div_z = x//z
    div_yz = x//( y*z//fractions.gcd(y,z) )
    return( x - div_y - div_z + div_yz  )

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

Submission status: Accepted

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

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

cd = lcm(c, d)

b_c = b//c
b_d = b//d
b_cd = b//cd
bb = b - (b_c + b_d - b_cd)

a_c = (a-1)//c
a_d = (a-1)//d
a_cd = (a-1)//cd
aa = (a-1) - (a_c + a_d - a_cd)

print(bb-aa)

Submission status: Accepted

Solution code:
import fractions

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

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

C_count = B//C - (A-1)//C
D_count = B//D - (A-1)//D
CD_count = B//CD - (A-1)//CD

result = total - C_count - D_count + CD_count

print(result)

Submission status: Accepted

Solution code:
from fractions import gcd

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

def solve():
    A,B,C,D = map(int, input().split())
    c = B//C - (A-1)//C
    d = B//D - (A-1)//D
    r = lcm(C,D)
    e = B//r - (A-1)//r
    ans = (B-A+1) - (c+d-e)
    return ans

print(solve())

Submission status: Accepted

Solution code:
def gcd(p, q):
    if p < q:
        p, q = q, p
    while q > 0:
        p, q = q, p%q
    return p

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

Submission status: Accepted

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


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


def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)


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

def calc_kukan_num(mod):
    start = A if A % mod == 0 else A + mod - (A+mod) % mod
    return (B - start) // mod + 1
# print(B-A-1, calc_kukan_num(C), calc_kukan_num(D), calc_kukan_num(C*D))
print(B-A+1-calc_kukan_num(C)-calc_kukan_num(D)+calc_kukan_num(lcm(C, D)))

Submission status: Accepted

Solution code:
from fractions import gcd

a, b, c, d = [int(i) for i in input().split()]
a-=1
ans = b - a
ans -= b // c - a//c
ans -= b // d - a // d
e = c * d // gcd(c, d)
ans += b // e - a // e
print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd

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

# A以上B以下の整数のうち、CでもDでも割り切れないものの個数を出力せよ。

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

by_C = B // C - (A - 1) // C
by_D = B // D - (A - 1) // D
by_LCM = B // LCM - (A - 1) // LCM
    
print(B - A + 1 - by_C - by_D + by_LCM)

Submission status: Accepted

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

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

e = int(c*d/gcd(c,d))
cde = [c,d,e]

cde1 = []
for i in cde:
    a0 = 0 if a%i != 0 else 1
    a1 = b//i - a//i + a0
    cde1.append(a1)

x,y,z = cde1
print((b-a+1) - x - y + z )

Submission status: Accepted

Solution code:
import sys
from fractions import gcd

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

A, B, C, D = lr()

def F(x):
    cnt_C = x // C
    cnt_D = x // D
    cnt_CD = x // (C*D//gcd(C, D))
    return x - cnt_C - cnt_D + cnt_CD

print(F(B) - F(A-1))

Submission status: Accepted

Solution code:
from fractions import gcd
A, B, C, D = map(int, input().split())
result = B+1 - A
def lcm(a, b):
    return (a * b) // gcd(a, b)
def wareru(min_num, max_num, waru):
    return (max_num)//waru - (min_num-1)//waru
result -= wareru(A, B, C) + wareru(A, B, D) - wareru(A, B, lcm(C,D))
print(result)

Submission status: Accepted

Solution code:
from fractions import gcd
A, B, C, D = [int(i) for i in input().split()]
result = B+1 - A
def lcm(a, b):
    return (a * b) // gcd(a, b)
def wareru(min_num, max_num, waru):
    return (max_num)//waru - (min_num-1)//waru
result -= wareru(A, B, C) + wareru(A, B, D) - wareru(A, B, lcm(C,D))
print(result)

Submission status: Accepted

Solution code:
import fractions
A, B, C, D = [int(i) for i in input().split()]
result = B+1 - A
def lcm(a, b):
    return (a * b) // fractions.gcd(a, b)
def wareru(min_num, max_num, waru):
    return (max_num)//waru - (min_num-1)//waru
result -= wareru(A, B, C) + wareru(A, B, D) - wareru(A, B, lcm(C,D))
print(result)

Submission status: Accepted

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

bc=b//c
bd=b//d
bcd=b//(cd)
b_cnt=b-(bc+bd-bcd)

ac=(a-1)//c
ad=(a-1)//d
acd=(a-1)//(cd)
a_cnt=a-1-(ac+ad-acd)

print(b_cnt-a_cnt)

Submission status: Accepted

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

# 最大公約数＜ユークリッドの互除法＞
def gcd(a,b):
  while b != 0:
    a, b = b, a % b
  return a
# 最小公倍数
def lcm(a,b):
    return a // gcd(a,b) * b


a_f = a // c + a // d - a // lcm(c,d)
b_f = b // c + b // d - b // lcm(c,d)

print((b-b_f) - (a-a_f))

Submission status: Accepted

Solution code:
from fractions import gcd

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

def lcm(c,d):
  return c*d//gcd(c,d)
a -= 1
lcm_ = lcm(c,d)
print(b-a - (b//c + b//d - a//c - a//d - (b//lcm_ -a//lcm_)))

Submission status: Accepted

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

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

A, B, C, D = lr()
cnt_divided_C = B // C - (A-1) // C
cnt_divided_D = B // D - (A-1) // D
x = C * D // gcd(C, D)
cnt_divided_CD = B // x - (A-1) // x
answer = B - (A-1) - (cnt_divided_C + cnt_divided_D - cnt_divided_CD)
print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from fractions import gcd

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

ok1=b-a+1
a-=1
ng1=b//c-a//c
ng2=b//d-a//d
lcm=(c*d)//gcd(c,d)
ok2=b//lcm-a//lcm 

print(ok1-ng1-ng2+ok2)

Submission status: Accepted

Solution code:
from fractions import gcd

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

ok1=b-a+1
ng1=b//c-a//c if a%c!=0 else b//c-a//c+1
ng2=b//d-a//d if a%d!=0 else b//d-a//d+1
lcm=(c*d)//gcd(c,d)
ok2=b//lcm-a//lcm if a%lcm!=0 else b//lcm-a//lcm+1

print(ok1-ng1-ng2+ok2)

Submission status: Accepted

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

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

def anti_division(n, c, d):
    return n - (n // c) - (n // d) + (n // lcm(c, d))

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
def linput():
    return list(map(int, input().split()))

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

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

def main():
    # N = int(input())
    A,B,C,D = linput()
    # vA = linput()
    # S = input()
    # mX = [linput() for _ in [0,]*N]
    U = B-A+1
    E = lcm(C, D)
    tC = (B//C - A//C) + (A%C==0)
    tD = (B//D - A//D) + (A%D==0)
    tE = (B//E - A//E) + (A%E==0)

    # res = 0
    # res = -(-N//M)

    res = U - tC - tD + tE
    print(res)

main()

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)
ans=b//d + b//c - (a-1)//c - (a-1)//d - b//lcm(c,d) + (a-1)//lcm(c,d)
ans=b-a+1-ans
print(ans)

Submission status: Accepted

Solution code:
# import math
from fractions import gcd

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

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

x1 = b//c - (a-1)//c
x2 = b//d - (a-1)//d
x3 = b//cd_lcm - (a-1)//cd_lcm

print((b-(a-1))-x1-x2+x3)

Submission status: Accepted

Solution code:
import fractions

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

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

Submission status: Accepted

Solution code:
from fractions import gcd

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

cnt = B//C -(A - 1)//C + B//D - (A - 1)//D - ((B//((C * D) // gcd(C, D))) - (A - 1)//((C * D) // gcd(C, D)))
cnt1 = B - (A - 1) -cnt

print(cnt1)

Submission status: Accepted

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


x = b // c
y = b // d


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


z = b // e

x2 = (a-1) // c
y2 = (a-1) // d


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


z2 = (a-1) // e2


print((b - (x + y - z)) - (a - 1 - (x2 + y2 - z2)))

Submission status: Accepted

Solution code:
from fractions import gcd

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

def solve(n):
  res = n - (n // c) - (n // d) + (n // lcm)
  return res

print(int(solve(b) - solve(a-1)))

Submission status: Accepted

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

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

div = (c+d-j)
print(W-div)

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())
l = lcm(C, D)
div_C = B // C - (A - 1) // C
div_D = B // D - (A - 1) // D
div_lcm = B // l - (A - 1) // l
div = div_C + div_D - div_lcm
ans = (B - A + 1) - div
print(ans)

Submission status: Accepted

Solution code:
def cnt(A, B, X):
    A1 = -(-A // X)
    B1 = B // X
    return B1 - A1 + 1

def gcd(A, B):
    mul = A * B
    if A < B:
        A, B = B, A
    while B:
        A, B = B, A % B
    return mul // A 

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
from fractions import gcd

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

a, b, c, d = (int(x) for x in stdin.readline().rstrip().split())

lcm = lcm(c, d)

total = b - (a - 1)
mc = b // c - (a - 1) // c
md = b // d - (a - 1) // d
mlcm = b // lcm - (a - 1) // lcm

print(total - (mc + md - mlcm))

Submission status: Accepted

Solution code:
import sys
import os
import fractions

MOD = 10 ** 9 + 7


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


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    A, B, C, D = list(map(int, sys.stdin.readline().split()))
    E = B - A + 1

    ret = 0
    ret += B // C
    ret += B // D
    ret -= (A - 1) // C
    ret -= (A - 1) // D
    ret -= B // lcm(C, D)
    ret += (A-1) // lcm(C, D)
    print(E - ret)


if __name__ == '__main__':
    main()

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):
    return x - x // C - x // D + x // lcm(C,D)

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

Submission status: Accepted

Solution code:
import fractions

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

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())
ans = b - a + 1
ans -= b // c - (a - 1) // c
ans -= b // d - (a - 1) // d
ans += b // lcm(c, d) - (a - 1) // lcm(c, d)
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)
#def count(n):
 # c = (B-A)//n
  #if A%n > B%n or A%n == 0:
   # c += 1
  #return c

#c = B-A+1-count(C)-count(D)+count(lcm)
def count2(A):
  c = A - A//C - A//D + A//lcm
  return c

c = count2(B)-count2(A-1)
print(int(c))

Submission status: Accepted

Solution code:
from fractions import gcd
A,B,C,D=map(int,input().split())
 
rc=(B//C)-((A-1)//C)#割り切れる数
rd=(B//D)-((A-1)//D)#割り切れる数
 
rcd=(B//(C*D//gcd(C,D)))-((A-1)//(C*D//gcd(C,D)))
print(int((B-A+1)-(rc+rd-rcd)))

Submission status: Accepted

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

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

c_num = B//C - (A-1)//C
d_num = B//D - (A-1)//D

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

res = (B-A + 1) - (c_num + d_num - cd_num)

print(res)

Submission status: Accepted

Solution code:
import fractions
a,b,c,d = map(int, input().split())
print(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))))

Submission status: Accepted

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

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

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

print(k(b)-k(a-1))

Submission status: Accepted

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

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


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

e=lcm(c,d) #c,dの最小公倍数

def kosu(num):
    z=num
    nc=num//c
    nd=num//d
    ne=num//e
    return z-nc-nd+ne

ans=kosu(b)-kosu(a-1)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

def fun(x,c,d):
    return x - div(x,c) - div(x,d) + div(x,c*d//gcd(c,d))

def div(a,b):
    return a // b

print(fun(a[1],a[2],a[3])-fun(a[0]-1,a[2],a[3]))

Submission status: Accepted

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

lcm = c*d//fractions.gcd(c,d)
ansa = a - a//c - a//d + a//lcm
ansb = b - b//c - b//d + b//lcm

print(ansb-ansa)

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)

ABmulC = B//C - (A-1)//C
ABmulD = B//D - (A-1)//D
LCM = lcm(C,D)
ABmulLCM = B//LCM - (A-1)//LCM

ans = B - A + 1
ans -= ABmulC + ABmulD
ans += ABmulLCM
print(ans)

Submission status: Accepted

Solution code:
#from math import gcd
from fractions import gcd
def lcm(s,t):
    return s*t//gcd(s,t)
def not_div(b,c,d):
    return b-(b//c + b//d - b//lcm(c,d))
a,b,c,d = map(int,input().split())
print(not_div(b,c,d)-not_div(a-1,c,d))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import fractions

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

# (B - A + 1) - n(C) - n(D) + n(C∩D)

def count_divisible_numbers(x):
  a = (A-1) // x
  b = B // x
  return b - a

n_C = count_divisible_numbers(C)
n_D = count_divisible_numbers(D)
CD_lcm = C*D // fractions.gcd(C, D)
n_CD = count_divisible_numbers(CD_lcm)

res = (B - A + 1) - n_C - n_D + n_CD
print(res)

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
sys.setrecursionlimit(10 ** 7)

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

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

def f(n, m, l):
  a = n // l
  if a * l < n:
    a += 1
  b = m // l
  return b - a + 1

def main():
    a, b, c, d = na()
    x = b - a + 1
    print(x - (f(a, b, c) + f(a, b, d) - f(a, b, lcm(c, d))))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

E = lcm(C, D)
ans = (B-A+1) - (B//C - (A-1)//C) - (B//D - (A-1)//D) + (B//E - (A-1)//E)

print(ans)

Submission status: Accepted

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

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

def multiple_counting(range, a, b):
    return (range -range//a -range//b +range//lcm(a,b))

def main():
    print(multiple_counting(b, c, d) - multiple_counting(a-1, c, d))

def test():
    from fractions import gcd
    A, B, C, D = map(int, input().split())
    l = C * D // gcd(C, D)
    x = (A - 1) - (A - 1) // C - (A - 1) // D + (A - 1) // l
    y = B - B // C - B // D + B // l
    print(y - x)
main()

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())
E = lcm(C, D)

def mulc(t) :
    return (B//t - (A-1)//t)

print((B-A+1) - mulc(C) - mulc(D) + mulc(E))

Submission status: Accepted

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

sub_c = (A - 1) // C
all_c = (B) // C
bic = all_c - sub_c


sub_d = (A - 1) // D
all_d = (B) // D
bid = all_d - sub_d

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

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

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

sub_all = bid + bic - bitmp


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

Submission status: Accepted

Solution code:
# 最大公約数
def gcd(m, n):
    r = m % n
    return gcd(n, r) if r else n

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

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

def f(n,C=C,D=D):
    return n - (n//C + n//D - n//lcm(C,D))
print(f(B)-f(A-1))

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)


ans1=b
a-=1
ans2=0
ans1-=b//c
ans1-=b//d

ans1+=b//(lcm(c,d))
#print(ans1)

ans2+=a//c
ans2+=a//d
ans2-=a//(lcm(c,d))
print(ans1-(a-ans2))

Submission status: Accepted

Solution code:
# gcd: 最大公約数
def gcd(x,y):
  while y:
    x, y = y, x%y
  return x
# lcm: 最小公約数
def lcm(x,y):
  return x*y//gcd(x,y)
a,b,c,d=map(int,input().split())
a-=1
e=lcm(c,d)
tc=b//c-a//c
td=b//d-a//d
te=b//e-a//e
print(b-a-tc-td+te)

Submission status: Accepted

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

Submission status: Accepted

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

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

def count(b, c, d):
    return b - b//c - b//d + b//(c * d // gcd(c, d))

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

Submission status: Accepted

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

def cnt_num(n, ok):
    cnt = 0
    # print(n, cnt)
    cnt += n // c
    # print(n, cnt)
    cnt += n // d
    # print(n, cnt)
    cnt -= n // ((c*d) // gcd(c, d))
    # print(n, cnt)
    if ((n % c == 0 or n % d == 0) or n % ((c*d) // gcd(c, d)) == 0) and ok:
        cnt -= 1
        
    return cnt

ans = cnt_num(b, False) - cnt_num(a, True)
# print(cnt_num(b, 0))
# print(cnt_num(a, 1))
ans = b - a - ans + 1
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)

all_count = b - a + 1
c_count = b // c - (a-1) // c
d_count = b // d - (a-1) // d
cd_count = b // lcm - (a-1) // lcm

print(all_count - c_count - d_count + cd_count)

Submission status: Accepted

Solution code:
from fractions import gcd

"""

+ その範囲の6の倍数の数
  これはBまでの倍数の数からAまでの倍数の数を引く
+ その範囲の8の倍数の数
- その範囲の48の倍数の数
で答えが求まる

"""

def v_count(n, start, end):
  a = (start - 1) // n
  b = end // n
  return b - a

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

all_count = B - A + 1

c_count = v_count(C, A, B)
d_count = v_count(D, A, B)
min_cd = (C * D) // gcd(C, D)
cd_count = v_count(min_cd, A, B)

print(all_count - c_count - d_count + cd_count)

Submission status: Accepted

Solution code:

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

def calc_lcd(x, y):
    if x < y:
        temp = x
        x = y
        y = temp
    
    r = x % y
    z = x * y
    while r != 0:
        x = y
        y = r
        r = x % y
    
    return z // y

lcd = calc_lcd(c, d)
a -= 1
#print(b // d)
#print("b:{0} b / c:{1} b / d:{2} b / lcd:{3} a:{4} a / c:{5} a / d:{6} a / lcd:{7}".format(b, b // c, b // d, b // lcd, a, a // c, a // d, a // lcd))
print(b - b // c - b // d + b // lcd - (a - a // c - a // d + a // lcd))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


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


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


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

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

    print(B - A + 1 - (b - a))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from fractions import gcd
A,B,C,D=map(int,input().split())
A=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:
#131_C
from fractions import gcd
a,b,c,d=map(int,input().split())
def not_c_d(x):
    return x-(x//c+x//d-x//(c*d//gcd(c,d)))
print(not_c_d(b)-not_c_d(a-1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from fractions import gcd
A,B,C,D = map(int,input().split())
def f(n):
    x = n//C
    y = n//D
    z = n//(C*D//gcd(C,D))
    return n - x - y + z

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