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

def gcd(x, y):
    if y > x:
        x, y = y, x
    if y == 1:
        return 1
    elif y == 0:
        return x
    else:
        return gcd(y, x-y*(x//y))

def num_divide(p, q, r):
    if p % r == 0:
        rmin = p
    else:
        rmin = p + (r-p%r)
    rmax = q - q%r
    return (rmax-rmin)//r + 1

print(b - a + 1 - (num_divide(a, b, c) + num_divide(a, b, d) - num_divide(a, b, c * d // gcd(c, d))))

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    a, b, c, d = list(map(int, input().rstrip('\n').split()))
    import fractions
    lcm = c * d // fractions.gcd(c, d)
    print((b - a + 1) - ((b // c - (a - 1) // c) + (b // d - (a - 1) // d) - (b // lcm - (a - 1) // lcm)))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from fractions import gcd


def solve(string):
    a, b, c, d = map(int, string.split())
    lcm = c * d // gcd(c, d)
    return str(b - a + 1 - (b // c - (a - 1) // c + b // d - (a - 1) // d - b // lcm +
                            (a - 1) // lcm))


if __name__ == '__main__':
    print(solve(input()))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from fractions import gcd

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


def lcb(x, y):
    return int((x * y) / gcd(x, y))


l = lcb(C, D)

A -= 1
AC = A // C
AD = A // D
ACD = A // l
x = A - AC - AD + ACD
# print(x)

BC = B // C
BD = B // D
BCD = B // l
y = B - BC - BD + BCD
# print(y)

ans = y - x
print(ans)

Submission status: Accepted

Solution code:
import fractions

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

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

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

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


if A%E == 0:
    CandD_div  = B//E - A//E + 1
else:
    CandD_div  = B//E- A//E


ans = B - A + 1 - (C_div + D_div - CandD_div)
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 f(num, c, d):
    return num - (num // c + num // d - num // int(c * d / gcd(c, d)))

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

Submission status: Accepted

Solution code:
def gcd(x, y):
    while y >= 1:
        x, y = y, x%y
    return x
def lcm(x,y):
    return int((x*y)/gcd(x,y))

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

lcm_cd = lcm(c, d)
a -= 1
result = int(b - a)
result -= b//c - a//c
result -= b//d - a//d
result += b//lcm_cd -a//lcm_cd

print(result)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC131 C

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

def gcd(n,m):
    if m == 0:
        return n
    else:
        return gcd(m,n%m)
def lcm(n,m):
    return (n*m)//gcd(n,m)

a,b,c,d = LI()
i,j,k = (a-1)//c,(a-1)//d,(a-1)//lcm(c,d)
s,t,u = b//c,b//d,b//lcm(c,d)
x = b - s - t + u
y = (a-1) - i - j + k
print(x -y)

Submission status: Accepted

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

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

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

cd = lcm(c,d)
r = b-a+1

min_c = a//c + (1 if a%c!=0 else 0)
max_c = b//c
min_d = a//d + (1 if a%d!=0 else 0)
max_d = b//d
min_cd = a//cd + (1 if a%cd!=0 else 0)
max_cd = b//cd

c_num = max_c-min_c+1
d_num = max_d-min_d+1
cd_num = max_cd-min_cd+1

print(r-c_num-d_num+cd_num)

Submission status: Accepted

Solution code:
import fractions
def CntMultiples(a, b, n):
    return b//n - a//n + int(a%n==0)

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

Submission status: Accepted

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

def f(x, y, z):
    cnt = y // z - x // z
    if x % z == 0:
        cnt += 1
    return cnt

div_c_count = f(a, b, c)
div_d_count = f(a, b, d)

import fractions
lcm = c * d // fractions.gcd(c, d)
cm_count = f(a, b, lcm)

print(all_count - (div_c_count + div_d_count - cm_count))

Submission status: Accepted

Solution code:
from fractions import gcd


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


def counter(n, a):
    return n // a


a, b, c, d = map(int, input().split())
l = lcm(c, d)
print(b - a + 1 - (counter(b, c) - counter(a - 1, c) +
                   counter(b, d) - counter(a - 1, d) -
                   (counter(b, l) - counter(a - 1, l))))

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 calc(n):
    return n-n // C-n // D+n // (C*D//gcd(C, D))

print(calc(B) - calc(A-1))

Submission status: Accepted

Solution code:
def gcd(x, y):
    while True:
        x, y = y, x % y
        if y == 0:
            break
    return x


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


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

n_c = b // c - (a - 1) // c
n_d = b // d - (a - 1) // d

e = lcm(c, d)
n_e = b // e - (a - 1) // e

print(b - a + 1 - (n_c + n_d - n_e))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from fractions import gcd
a,b,c,d = map(int,input().split())
lcm = c*d//gcd(c,d)
ac = (a-1)//c
ad = (a-1)//d
la = (a-1)//lcm
a_ = a-(ac+ad-la)
bc = b//c
bd = b//d
lb = b//lcm
b_ = b-(bc+bd-lb)
ans = b_ - a_ + 1
print(ans)

Submission status: Accepted

Solution code:
import math
a,b,c,d = map(int, input().split())
 
#cとdの最大公約数を求める
first = [c,d%c]
first.sort()
if d%c==0:
  cd = c
elif first[0]==1:
  cd = 1
else:
  while first[0]>=2:
    first[1] = first[1]%first[0]
    first.sort()
    if first[0]==0:
      cd = first[1]
    elif first[0]==1:
      cd = 1
lcm = c*d//cd
 
#1.7＊10^17みたいなノリだから、intにするとまずいのでは
zen = b-a+1
if a%c==0:
  c_num = b//c - a//c + 1
else:
  c_num = b//c - a//c
  
if a%d==0:
  d_num = b//d - a//d + 1
else:
  d_num = b//d - a//d
  
if a%lcm==0:
  lcm_num = b//lcm - a//lcm + 1
else:
  lcm_num = b//lcm - a//lcm

ans = zen - c_num - d_num + lcm_num
print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd


def wari(x, c, d):
    nc = x // c
    nd = x // d
    ncd = x // ((c * d) // gcd(c, d))
    return x - nc - nd + ncd


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

nb = wari(B, C, D)
na = wari(A - 1, C, D)
print(nb - na)

Submission status: Accepted

Solution code:
import fractions
n1, n2, n3, n4 = [int(i) for i in input().split()]

n5 = (n3 * n4) // fractions.gcd(n3, n4)

n6 = (n2 // n3) - ((n1 - 1) // n3)
n7 = (n2 // n4) - ((n1 - 1) // n4)
n8 = (n2 // n5) - ((n1 - 1) // n5)

r1 = (n2 - n1  + 1)- n6 - n7 + n8 

print(r1)

Submission status: Accepted

Solution code:
#######################################
import numpy as np
import math
import copy
import re
import sys
#sys.exit(0)

inf = float("inf")
prime = 1

def check_print(check_flag, text, end="\n"):
  if check_flag:
    print(text, end=end)

def c2i(x):
    if str.isdecimal(x):
        return int(x)
    else:
        return x

def cout(*args):
  arglist = []
  for arg in args:
    if hasattr(arg, "__iter__"):
      arglist.extend(arg)
    else:
      arglist.append(arg)
      
  arglist = list(map(str, arglist))
  print(" ".join(arglist))

#or
#print(" ".join(list(map(str, lis))))
  
def count2(num):
  count=0
  while num%2==0:
    num/=2
    count+=1
  return count

def ketasum(num):
  nums = [int(s) for s in str(num)]
  return sum(nums)

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

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

def gcd3(lis): #最大公約数(3over)
  ans = lis[0]
  for li in lis:
    ans = gcd(ans,li)

def lcm3(lis): #最小公倍数(3over)
  ans = lis[0]
  for li in lis:
    ans = lcm(ans,li)

def divisor(n): #nの約数を全て求める
  i = 1
  table = []
  while i * i <= n:
    if n%i == 0:
      table.append(i)
      table.append(n//i)
    i += 1
  table = list(set(table))
  return table

def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(i)
    i += 1
  if n > 1:
    table.append(n)
  return table

def is_prime(n):
  for i in range(2, n + 1):
    if i * i > n:
      break
    if n % i == 0:
      return False
  return n != 1

#processing stock
"""
a = sorted(a)
a = sorted(a, reverse=True)

"""

#regex
"""
pattern = ""
result = re.search(pattern, s)

result = re.fullmatch(pattern, s)

result = re.finditer(pattern, s)
for res in result:
    print res.group()   # 1回目: ca      2回目: ca   
    print res.start()   # 1回目: 0       2回目: 6      
    print res.end()     # 1回目: 2       2回目: 8      
    print res.span()    # 1回目: (0, 2)  2回目: (6, 8)
"""


#math
"""
#切り捨て
math.floor()
# 切り上げ
math.ceil()
"""

#input stock
"""
n = int(input())

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

d=[]
for i in range(n):
  d[i]=int(input())

#2dim-list
a = [list(map(int, input().split())) for i in range(n)]

s = input()
ss =  [int(si) for si in s]
"""

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

def anti_d(num):
  return num - (num//c + num//d - num//lcm(c,d))

ret = anti_d(b) - anti_d(a-1)
print(int(ret))

Submission status: Accepted

Solution code:
#a,bの最大公約数
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())

c1=(b//c)-(-((-1*a)//c))+1
d1=(b//d)-(-((-1*a)//d))+1
cd=lcm(c, d)
#print(cd)
cd1=(b//cd)-(-((-1*a)//cd))+1

print(b-a-c1-d1+cd1+1)

Submission status: Accepted

Solution code:
import math
import copy
import fractions
import numpy as np

# ===FUNCTION===

def getInputNum():
    inputNum = int(input())
    return inputNum

def getInputListInt():
    outputoData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData

def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList

def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList

# ===CODE===

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


def LCM(x, y):
    return x // GCD(x, y) * y

num = getInputListInt()

a = num[0]
b = num[1]
c = num[2]
d = num[3]

c_modzero = b//c - (a-1)//c
d_modzero = b//d - (a-1)//d

cd = LCM(c, d)

cd_modzero = b//cd - (a-1)//cd

ans = b - a + 1 - (c_modzero + d_modzero - cd_modzero)

print(int(ans))

Submission status: Accepted

Solution code:
# import math
# import copy
# import fractions
# import numpy as np
#
# # ===FUNCTION===
#
# def getInputNum():
#     inputNum = int(input())
#     return inputNum
#
# def getInputListInt():
#     outputoData = []
#     inputData = input().split()
#     outputData = [int(n) for n in inputData]
#
#     return outputData
#
# def getSomeInputInt(n):
#     outputDataList = []
#     for i in range(n):
#         inputData = int(input())
#         outputDataList.append(inputData)
#
#     return outputDataList
#
# def getSomeInputListInt(n):
#     inputDataList = []
#     outputDataList = []
#     for i in range(n):
#         inputData = input().split()
#         inputDataList = [int(n) for n in inputData]
#         outputDataList.append(inputDataList)
#
#     return outputDataList
#
# # ===CODE===
#
# num = getInputListInt()
#
# a = num[0]
# b = num[1]
# c = num[2]
# d = num[3]
#
# c_modzero = math.floor(b/c) - math.floor((a-1)/c)
# d_modzero = math.floor(b/d) - math.floor((a-1)/d)
#
# cd = c / fractions.gcd(c, d) *d
#
# cd_modzero = math.floor(b/cd) - math.floor((a-1)/cd)
#
# ans = b - a + 1 - (c_modzero + d_modzero - cd_modzero)
#
# print(int(ans))

import copy



# input = sys.stdin.readline

# ~~~~~~~~~~~~~~~~~~~~~_(＾～＾ ｣ ∠)_~~~~~~~~~~~~~~~~~~~~~

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


def LCM(x, y):
    return x // GCD(x, y) * 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())
i, j = c, d
def gcd(i, j):
    return i if j == 0 else gcd(j, i%j)
lcm = int(c//gcd(c, d)*d)

def cont(x):
    cnt1 = x//c 
    cnt2 = x//d
    ans = x - cnt1 - cnt2 + x//lcm
    return int(ans)

print(cont(b) - cont(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)


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

E = lcm(C, D)

X = int(B//C)-int((A-1)//C)
Y = int(B//D)-int((A-1)//D)
Z = int(B//E)-int((A-1)//E)

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

Submission status: Accepted

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

    if b == 0:
        return a

    else:
        return gcd(b, a % b)

A, B, C, D = map(int, input().split())
lcm = C * D // gcd(C, D)
tmp = (B // C - (A - 1) // C) + (B // D - (A - 1) // D) - (B // lcm - (A - 1) // lcm)
output = B - A + 1 - tmp

print(output)

Submission status: Accepted

Solution code:
import fractions

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

cde = b // c - (a - 1) // c
dde = b // d - (a - 1)  // d
lde = b // lcm - (a - 1)  // lcm

print(b - a + 1 - cde - dde + lde)

Submission status: Accepted

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

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

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

lcm = c1 * d1 // gcd(d1, c1)

def count(x):
    return B // x - ((A - 1) // x)

s = count(c1)
t = count(d1)
u = count(lcm)
v = count(1)

print(v - s - t + u)

Submission status: Accepted

Solution code:
import fractions

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

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

A -= 1

lcm_cd = lcm(C, D)
a_by_c = A // C
a_by_d = A // D
b_by_c = B // C
b_by_d = B // D
a_by_cd = A // lcm_cd
b_by_cd = B // lcm_cd

toB = B - (b_by_c + b_by_d - b_by_cd)
toA = A - (a_by_c + a_by_d - a_by_cd)

print(toB - toA)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
from fractions import gcd

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

c1 = b // c - (a - 1) // c
d1 = b // d - (a - 1) // d
cd = (c * d) //gcd(c,d)
cd1 = b // cd - (a - 1) // cd



print((b - a + 1) - c1 - d1 + cd1)

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


# 初項s, 交差dのn個の数列の和
def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


# aとbの最大公約数
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


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


def count(X, C, D):
    ans = X
    ans -= X // C
    ans -= X // D

    num = X // lcm(C, D)
    ans += num

    return ans


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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

def lcm(C, D):
    return (C * D) // fractions.gcd(C, D)

def function(X):
    return X - X//C - X//D + X//lcm(C, D)

print(function(B) - function(A - 1))

Submission status: Accepted

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

Submission status: Accepted

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

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

b_num = b//c + b//d -b//lcm(c,d)
a_num = (a-1)//c + (a-1)//d - (a-1)//lcm(c,d)
print(target - (b_num - a_num))

Submission status: Accepted

Solution code:
def main():
    a, b, c, d = map(int, input().split())
    ans = b - a + 1
    ans -= b // c - (a - 1) // c
    ans -= b // d - (a - 1) // d
    lcmcd = lcm(c, d)
    ans += b // lcmcd - (a - 1) // lcmcd
    print(ans)

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


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

main()

Submission status: Accepted

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

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

def lcm(C,D):
  return C*D // fractions.gcd(C,D)
CD=lcm(C,D)

print(int(B-A+1-((B//C + B//D - B//CD) - ((A-1)//C + (A-1)//D - (A-1)//CD))))

Submission status: Accepted

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

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

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

b_c = b//c
b_d = b//d
b_e = b//e
a_c = (a-1)//c
a_d = (a-1)//d
a_e = (a-1)//e

print(b-a+1 - (b_c - a_c) - (b_d - a_d) + (b_e - a_e))

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
cd = int(c*d/gcd(c,d))
l = [c,d,cd]
count = 0
for i in range(3):
  if a%l[i] == 0:
    if i < 2:
      count += (1+len(range(a+1,b+1))//l[i])
    else:
      count -= (1+len(range(a+1,b+1))//l[i])
  else:
    e = a+l[i]-a%l[i]
    if e <= b:
      if i < 2:
        count += (1+len(range(e+1,b+1))//l[i])
      else:
        count -= (1+len(range(e+1,b+1))//l[i])
    
print(len(range(a,b+1))-count)

Submission status: Accepted

Solution code:
import fractions

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

Submission status: Accepted

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

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

def calc(x):
    return x - (x//c)-(x//d)+(x//e)

print(calc(b)-calc(a-1))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
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)

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

print(calc(b)-calc(a-1))

Submission status: Accepted

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

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

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

print(calc(b)-calc(a-1))

Submission status: Accepted

Solution code:
import fractions
A,B,C,D = map(int,input().split(' '))
def calc1(X):
  return X - X//C - X//D + X //int((C*D/fractions.gcd(C,D)))
print(calc1(B)-calc1(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 ans(x):
    return x - (x // C + x // D - x // (C * D // gcd(C, D)))

print(ans(B) - ans(A - 1))

Submission status: Accepted

Solution code:
def main():
  a, b, c, d = list(map(int, input().split(' ')))
  l = lcm(c, d) if c >= d else lcm(d, c)
  ans = (b - (b//c + b//d - b//l)) - ((a-1) - ((a-1)//c + (a-1)//d - (a-1)//l))
  print(ans)


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


def lcm(l, s):
  return l * s // gcd(l, s)


if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
from fractions import gcd

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

def f(a, b, n):
  return b // n - (a - 1) // n

acc = 0
acc += f(a, b, c)
acc += f(a, b, d)
acc -= f(a, b, c * d // gcd(c, d))
print(b - a + 1 - acc)

Submission status: Accepted

Solution code:
a,b,c,d = map(int,input().split())
def gcd(a,b):
    if b == 0:
        return a
    else:
        return gcd(b,a%b)
lc = c * d // (gcd(c,d))
total = b - a + 1
onlyc = (b//c) - (a-1)//c
onlyd = (b//d) - (a-1)//d
both = (b//lc) - (a-1)//lc

print(total - (onlyd + onlyc - both))

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())
count = b-a+1

div_c = b//c - (a-1)//c
div_d = b//d - (a-1)//d
cd = lcm(c, d)
div_cd = b//cd - (a-1)//cd

count -= div_c + div_d - div_cd
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def gcd(c, d):
  if c < d:
    tmp = d
    d = c
    c = tmp
  ret = c
  if d != 0:
    ret = gcd(d, c % d)
  return ret

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

def union_cardinality(a_cardi, b_cardi, ab_cardi):
  return a_cardi + b_cardi - ab_cardi

def count_cm(maxi, divi):
  return maxi // divi

def count_cm2(maxi, divi1, divi2):
  num1 = count_cm(maxi, divi1)
  num2 = count_cm(maxi, divi2)
  num12 = count_cm(maxi, lcm(divi1, divi2))
  return union_cardinality(num1, num2, num12)

def count_cm2_band(mini, maxi, divi1, divi2):
  return count_cm2(maxi, divi1, divi2) - count_cm2(mini - 1, divi1, divi2)

a, b, c, d = (int(x) for x in input().split())
print(b - a + 1 - count_cm2_band(a, b, c, d))

Submission status: Accepted

Solution code:
import math

#x>yとする
def gcd(x, y):
    rem = x%y
    if rem == 0:
        return y
    else:
        return gcd(y,rem)

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

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

Submission status: Accepted

Solution code:
import fractions

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

#a以上b以下の数のなかで、cかdで割り切れるものの数を求めて、合計から除く
ans=b-a+1
c_m=b//c-(a-1)//c
d_m=b//d-(a-1)//d
cd=c*d//fractions.gcd(c,d)
cd_m=b//cd-(a-1)//cd
print(ans-c_m-d_m+cd_m)

Submission status: Accepted

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

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

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

# cとd両方で割り切れる数を求める
cd = (c * d) // fractions.gcd(c, d)
cd_div = (b // cd) - ((a-1) // cd)
#print(cd_div)

div =  c_div  + d_div - cd_div

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

Submission status: Accepted

Solution code:
def main():
    A, B, C, D = map(int, input().split())
    if C <= D: p = gcd(C, D)
    else: p = gcd(D, C)
    lcm = C * D // p
    dA = (A-1) - (A-1) // C - (A-1) // D + (A-1) // lcm
    dB = B - B // C - B // D + B // lcm
    return dB - dA

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

if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
import fractions

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

# CとDの最小公倍数算出
bi = C*D//fractions.gcd(D,C)

A-=1
Bbi =B//bi
Abi =A//bi
sa = Bbi -Abi
# print(B-A-sa)

Bc = B//C
Bd = B//D

Ac = A//C
Ad = A//D

print(int( B-A - ((Bc-Ac) + (Bd-Ad) - sa)  ))

Submission status: Accepted

Solution code:
import fractions
A,B,C,D = list(map(int, input().split()))
a=(A-1)//C
b=B//C
c=(A-1)//D
d=B//D
g = C*D//fractions.gcd(C,D)
e=(A-1)//(g)
f=B//(g)

print(B-A+1 - (b-a) - (d-c) + (f-e))

Submission status: Accepted

Solution code:
# import math
import fractions


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


def lcm(x, y):
    # DeprecationWarning: fractions.gcd() is deprecated. Use math.gcd() instead.
    return x * y // fractions.gcd(x, y)


a1 = a - 1
div_a = (a1 // c) + (a1 // d) - (a1 // lcm(c, d))
div_b = (b // c) + (b // d) - (b // lcm(c, d))

print((b - a + 1) - (div_b - div_a))

Submission status: Accepted

Solution code:
from fractions import gcd 

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

total = B - A + 1

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

Cdiv = B // C - (A-1) // C
Ddiv = B // D - (A-1) // D
lcgdiv = B // (lcg) - (A-1) // (lcg)

print(total - Cdiv - Ddiv + lcgdiv)

Submission status: Accepted

Solution code:
a, b, c, d = list(map(int, input().split()))
def euqlid(x,y):
    if x < y :
        temp = x
        x = y
        y = temp
    while x % y != 0:
        temp = x
        x = y
        y = temp % y
    return y
def searchCount(p,q,k):
    min = 0
    max = 0
    if p % k == 0:
        min = p // k
    else:
        min = p//k + 1
    max = q//k
    return max - min + 1
lcm = euqlid(c,d)
gcd = int((c * d) / lcm)
print((b - a + 1) - searchCount(a,b,c) - searchCount(a,b,d) + searchCount(a,b,gcd))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import fractions


def solve():
    a, b, c, d = map(int, input().split())
    lcm = c * d // fractions.gcd(c, d)
    div_c = b // c - (a - 1) // c
    div_d = b // d - (a - 1) // d
    div_cd = b // lcm - (a - 1) // lcm
    return (b - a + 1) - div_c - div_d + div_cd


if __name__ == "__main__":
    print(solve())

Submission status: Accepted

Solution code:
import math
import fractions

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

a_c = (a - 1) // c
b_c = b // c
a_d = (a - 1) // d
b_d = b // d

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

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

Submission status: Accepted

Solution code:
#coding utf-8
A,B,C,D=map(int,input().split())

def gcd(m,n):
    x = max(m,n)
    y = min(m,n)
    if x%y == 0:
        return y
    else:
        return gcd(x%y,y)

if A%C==0:
  ac=A//C-1
else:
  ac=A//C
bc=B//C

if A%D==0:
  ad=A//D-1
else:
  ad=A//D
bd=B//D

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

if A%CD==0:
  acd=A//CD-1
else:
  acd=A//CD
bcd=B//CD

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

Submission status: Accepted

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


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




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



c_d = lcm(c, d)




ans = b - a + 1 - (b // d - (a - 1) // d) -\
    (b // c - (a - 1) // c) +\
    (b // c_d - (a - 1) // c_d)



print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# Anti-Division

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


# 最大公約数
def gcd(C, D):
    while D != 0:
        C, D = D, C % D
    return C


w = B // C
x = B // D
y = (A - 1) // C
z = (A - 1) // D
a = B // (C * D // gcd(C, D))
b = (A - 1) // ((C * D) // gcd(C, D))

ans = (B - A + 1) - ((w - y) + (x - z) - (a - b))
print(ans)

Submission status: Accepted

Solution code:
import fractions

entrada = list(map(int,input().split(" ")))

rangoMenor = entrada[0]
rangoMayor = entrada[1]
divisor1 = entrada[2]
divisor2= entrada[3]

multiplo = divisor2*divisor1// fractions.gcd(divisor1,divisor2)
cantidad = rangoMayor-rangoMenor
repetidos = rangoMayor//multiplo - rangoMenor//multiplo

if rangoMenor%divisor1==0:
	divisibles1 = rangoMayor//divisor1 +1 - rangoMenor//divisor1
else:
	divisibles1 = rangoMayor // divisor1 - rangoMenor//divisor1

divisibles2 = rangoMayor//divisor2 - rangoMenor//divisor2

resultado = cantidad -(divisibles1+divisibles2) +repetidos + 1
print(resultado)

Submission status: Accepted

Solution code:
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)

def calc(n, c, d):
    lcm_num = n // lcm(c, d)
    c_num = n // c
    d_num = n // d
    return n - c_num - d_num + lcm_num

ret = calc(B, C, D) - calc(A-1, C, D)
print(ret)

Submission status: Accepted

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

total_num = B - A + 1

if A % C == 0:
    can_C_divided = B // C - A // C + 1
else:
    can_C_divided = B // C - A // C
if A % D == 0:
    can_D_divided = B // D - A // D + 1
else:
    can_D_divided = B // D - A // D
lcm = C * D // fractions.gcd(C, D)
if A % lcm == 0:
    can_CD_divided = B // lcm - A // lcm + 1
else:
    can_CD_divided = B // lcm - A // lcm

print(total_num - can_C_divided - can_D_divided + can_CD_divided)

Submission status: Accepted

Solution code:
import fractions

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

a -= 1

# a~bの数でcで割り切れる数の個数は
divC = b//c - a//c
# a~bの数でdで割り切れる数の個数は
divD = b//d - a//d
# a~bの数でc,dの最小公倍数で割り切れる数の個数は
l = (c*d) // fractions.gcd(c,d)
divU= b//l - a//l
divSum = divC+divD-divU


print(b-a - divSum)