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

cc = B // C - (A-1) // C
dd = B // D - (A-1) // D

import math
l = C * D // math.gcd(C, D)
cd = B // l - (A-1) // l

print(B - A + 1 - (cc + dd - cd))

Submission status: Accepted

Solution code:
import math
 
def lcm(a, b):
    return (a*b)//math.gcd(a, b)
 
a, b, c, d = map(int, input().split())
e = lcm(c, d)
 
total = b - (a - 1)
cc = b//c - (a-1)//c
dd = b//d - (a-1)//d
ee = b//e - (a-1)//e
 
ans = total - cc - dd + ee
print(ans)

Submission status: Accepted

Solution code:
import math

def Div(a,b,c):
  #a<=x<=bのうちcで割り切れるものの数を返す
  l = ((a-1)//c+1)*c
  r = (b//c)*c
  return (r-l)//c+1

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

print(B-A+1-(Div(A,B,C)+Div(A,B,D)-Div(A,B,C*D//math.gcd(C,D))))

Submission status: Accepted

Solution code:
import math

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

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

total = b - (a - 1)
cc = b//c - (a-1)//c
dd = b//d - (a-1)//d
ee = b//e - (a-1)//e

ans = total - cc - dd + ee
print(ans)

Submission status: Accepted

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

#Cについて
divByC = 0
r = A % C
if r != 0:
    d = A + (C - r)
else:
    d = A
if d <= B:
    divByC = (B - d + 1) // C
    if (B - d + 1) % C != 0:
        divByC += 1
# print(r, d, divByC)

#Dについて
divByD = 0
r = A % D
if r != 0:
    d = A + (D-r)
else:
    d = A
if d <= B:
    divByD = (B - d + 1) // D
    if (B-d+1) % D != 0:
        divByD += 1
# print(r, d, divByD)

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

divByBoth = 0
r = A % lcm
if r != 0:
    d = A + (lcm-r)
else:
    d = A
if d <= B:
    divByBoth = (B - d + 1) // lcm
    if (B - d + 1) % lcm != 0:
        divByBoth += 1
# print(r, d, divByBoth)

print((B - A + 1) - (divByC + divByD - divByBoth))

Submission status: Accepted

Solution code:
# import sys
# sys.setrecursionlimit(10 ** 6)
# import bisect
# from collections import deque

def lcm(a, b):
    """最小公倍数"""
    from math import gcd
    return (a * b) // gcd(a, b)


# from decorator import stop_watch
#
#
# @stop_watch
def solve(A, B, C, D):
    A -= 1
    non_div_A = A - (A // C) - (A // D) + (A // lcm(C, D))
    non_div_B = B - (B // C) - (B // D) + (B // lcm(C, D))
    print(non_div_B - non_div_A)


if __name__ == '__main__':
    # S = input()
    # N = int(input())
    A, B, C, D = map(int, input().split())
    # As = [int(i) for i in input().split()]
    # Bs = [int(i) for i in input().split()]
    solve(A, B, C, D)

Submission status: Accepted

Solution code:
import math

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

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

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

B_cnt = B//C + B//D - B//CD
A_cnt = (A-1)//C + (A-1)//D - (A-1)//CD

print((B-A+1) - (B_cnt - A_cnt))

Submission status: Accepted

Solution code:
'''
Created on 2020/08/28

@author: harurun
'''

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

def main():
  import sys
  pin=sys.stdin.readline
  pout=sys.stdout.write
  perr=sys.stderr.write
  
  A,B,C,D=map(int,pin().split())
  M=B//C+B//D-B//lcm(C,D)
  m=(A-1)//C+(A-1)//D-(A-1)//lcm(C,D)
  t=M-m 
  print(B-A+1-t)
  return 
main()

Submission status: Accepted

Solution code:
import math

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

ac = (a-1) // c
ad = (a-1) // d
bc = b // c
bd = b // d
cd_lcm =  (c * d) // math.gcd(c, d)
acd_lcm = (a-1) // cd_lcm
bcd_lcm = b // cd_lcm

l = b - a + 1

print(l - (bc - ac) - (bd - ad) + (bcd_lcm - acd_lcm))

Submission status: Accepted

Solution code:
import math

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

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

ac=a//c
ad=a//d
acd=a//lcm
bc=b//c
bd=b//d
bcd=b//lcm

A=a-ac-ad+acd
B=b-bc-bd+bcd

if a%c==0 or a%d==0:
  print(B-A)
else:
  print(B-A+1)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readlines

import math

def main():

    lines = input()
    a, b, c, d = list(map(int, lines[0].split()))

    def gcd(a, b):

        r = a % b

        if r == 0:
            return int(b)
        
        return gcd(b, r)

    g = gcd(c, d)
    gcm = int(c * d / g)

    c_in = int(b // c - (a-1)//c)
    d_in = int(b // d - (a-1)//d)
    cd_in = int(b // gcm - (a-1)//gcm)

    ans = int((b-a+1) - (c_in + d_in - cd_in))
    print(ans)

main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readlines

import math

def main():

    lines = input()
    a, b, c, d = list(map(int, lines[0].split()))

    def gcd(a, b):

        r = a % b

        if r == 0:
            return int(b)
        
        return gcd(b, r)

    g = gcd(c, d)
    gcm = int(c * d // g)

    c_in = int(b // c - (a-1)//c)
    d_in = int(b // d - (a-1)//d)
    cd_in = int(b // gcm - (a-1)//gcm)

    ans = int((b-a+1) - (c_in + d_in - cd_in))
    print(ans)

main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque, Counter, OrderedDict
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from heapq import heappush, heappop, heapify

import itertools
import math, fractions
import sys, copy

def L(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline().rstrip())
def SL(): return list(sys.stdin.readline().rstrip())
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def LIR1(n): return [LI1() for _ in range(n)]
def SR(n): return [SL() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
def LR(n): return [L() for _ in range(n)]

def perm(n, r): return math.factorial(n) // math.factorial(r)
def comb(n, r): return math.factorial(n) // (math.factorial(r) * math.factorial(n-r))
def lcm(a, b): return a * b // math.gcd(a, b)

def make_list(n, *args, default=0): return [make_list(*args, default=default) for _ in range(n)] if len(args) > 0 else [default for _ in range(n)]


sys.setrecursionlimit(1000000)
dire, dire8 = [[1, 0], [0, 1], [-1, 0], [0, -1]], [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
alphabets, ALPHABETS = "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
MOD, INF = 1000000007, float("inf")

def main():
    A, B, C, D = LI()

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from math import gcd
a,b,c,d=map(int,input().split())
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 math
A,B,C,D = map(int,input().split())

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

i = B//C
j = B//D
k = (A-1)//C
l = (A-1)//E
n = (A-1)//D
m = B//E

res = B-(A-1)-(i-k)-(j-n)+(m-l)
print(res)

Submission status: Accepted

Solution code:
import math
a,b,c,d = map(int,input().split())
min_c = (a-1)//c
max_c = b//c
min_d = (a-1)//d
max_d = b//d
tmp = (c * d) // math.gcd(c, d)
min_cd = (a-1)//tmp
max_cd = b//tmp

#print(a,b,c,d)
#print(min_c,max_c)
#print(min_d,max_d)
#print(min_cd,max_cd)
print(b-(a-1)-(max_c-min_c)-(max_d-min_d)+(max_cd-min_cd))

Submission status: Accepted

Solution code:
def calc(a, b):
    x = max(a, b)
    y = min(a, b)
    while True:
        c = x % y
        if c == 0:
            break
        x = y
        y = c
    return y

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

E = int(C * D / calc(C, D))
l = int(B - B // C - B // D + B // E)
m = int((A-1) - (A-1) // C - (A-1) // D + (A-1) // E)

print(l-m)

Submission status: Accepted

Solution code:
from math import gcd 

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

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

G = gcd(C,D)
L = int(C*D/G)

Bl = B//L

RangeB = B-(Bc+Bd-Bl)

Ac = (A-1) // C
Ad = (A-1) // D
Al = (A-1) // L

RangeA = (A-1)-(Ac+Ad-Al)

print(RangeB-RangeA)

Submission status: Accepted

Solution code:
import math

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


C_multiple = B // C - A // C
D_multiple = B // D - A // D 

if A % C == 0:
    C_multiple += 1
if A % D == 0:
    D_multiple += 1
        

CD_gcd = C * D // math.gcd(C,D) 
CD_multiple = B // CD_gcd - A// CD_gcd 
if A % CD_gcd == 0:
    CD_multiple += 1



ans = ( B - A + 1 ) - (C_multiple + D_multiple - CD_multiple)
print(int(ans))

Submission status: Accepted

Solution code:
import math
a,b,c,d=[int(x) for x in input().split()]

def div(n,p):
  return n//p

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

ans=b-a+1
ans-=(div(b,c)-div(a-1,c))
ans-=(div(b,d)-div(a-1,d))
e=lcm(c,d)
ans+=(div(b,e)-div(a-1,e))
print(ans)

Submission status: Accepted

Solution code:
import sys
import math

A,B,C,D = map(int, input().split())
def lcm(x, y):
    return (x * y) // math.gcd(x, y)


maru1 = B // C
maru2 = (A-1) // C

maru3 = B // D
maru4 = (A-1) // D

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

ans_tmp = ((maru1-maru2)+(maru3-maru4)) - (maru5-maru6)
ans = (B-A+1) - ans_tmp

print(ans)

Submission status: Accepted

Solution code:
import math

def delete_head_zeros(n):
	n = str(n)
	l = len(n)
	if "." in n:
		l = n.find(".")
	head_zeros = 0
	for i in range(l - 1):
		if n[i] == "0":
			head_zeros += 1
		else:
			break

	return n[head_zeros:]

# compare a, b
# a, b: int or string
def bigint_compare(a, b):
	a = delete_head_zeros(a)
	b = delete_head_zeros(b)
	if len(a) > len(b):
		return 1
	elif len(a) < len(b):
		return -1
	else:
		if a > b:
			return 1
		elif a < b:
			return -1
		else:
			return 0

# calculate a + b
# a, b: int or string
def bigint_plus(a, b):
	a = str(a)
	b = str(b)

	d = max([len(a), len(b)])
	a = '0' * (d - len(a)) + a
	b = '0' * (d - len(b)) + b

	ans = ""
	carry = 0
	for i in range(d):
		s = int(a[-i-1]) + int(b[-i-1]) + carry
		carry = s // 10
		ans = str(s % 10) + ans
	else:
		if carry:
			ans = str(carry) + ans

	return ans

# calculate a - b
# a, b: int or string
def bigint_minus(a, b): 
	a = str(a)
	b = str(b)
	M = []
	m = []
	sign = ""

	if len(a) > len(b) or (len(a) == len(b) and a >= b):
		[M, m] = [a, b]
	else:
		[M, m] = [b, a]
		sign = "-"
	m = '0' * (len(M) - len(m)) + m

	ans = ""
	borrow = 0
	for i in range(len(M)):
		s = int(M[-i-1]) - int(m[-i-1]) - borrow
		if s < 0:
			borrow = 1
			s += 10
		else:
			borrow = 0
		ans = str(s) + ans

	return sign + delete_head_zeros(ans)

# calculate a * b
# a, b: int or string
def bigint_multiply(a, b):
	a = str(a)
	b = str(b)

	md = []
	carry = 0
	for j in range(len(b)):
		mj = ""
		for i in range(len(a)):
			m = int(a[-i-1]) * int(b[-j-1]) + carry
			carry = m // 10
			mj = str(m % 10) + mj
		else:
			if carry:
				mj = str(carry) + mj
		md.append(mj)

	ans = 0
	for k in range(len(md)):
		ans = bigint_plus(md[k] + "0" * k, ans)

	return ans


# calculate a / b to d digits after decimal point
# a, b, d: int or string
def bigint_divide(a, b, d = 0):
	a = str(a)
	b = str(b)
	d = int(d)
	if d < 0:
		d = 0

	ans = ""
	r = ""
	for i in range(len(a) + d):
		q = 0
		if i < len(a):
			r += a[i]
		elif i == len(a):
			ans += "."
			r += "0"
		else:
			r += "0"

		if bigint_compare(r, b) == -1:
			ans += str(q)
		else:
			while bigint_compare(r, b) >= 0:
				r = bigint_minus(r, b)
				q += 1
			ans += str(q)

	return delete_head_zeros(ans)

A, B, C, D = [n for n in input().split(" ")]
lcm = bigint_divide(bigint_multiply(C, D), math.gcd(int(C), int(D)))
right = B
left = bigint_minus(A, 1)
allcnt = bigint_minus(right, left)
def mulN(x):
  return bigint_minus(bigint_divide(right, x), bigint_divide(left, x))

print(bigint_minus(bigint_plus(allcnt, mulN(lcm)), bigint_plus(mulN(C), mulN(D))))

Submission status: Accepted

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip.split())
def main():
    from functools import reduce
    def lcm_base(n1,n2):
        return (n1*n2)//math.gcd(n1,n2)
    def lcm(*numbers):
        return reduce(lcm_base, numbers)
    a,b,c,d = LI()
    cd = lcm(c,d)
    lst = [c,d,cd]
    ans_lst = []
    for ex in lst:
        before = a if a % ex == 0 else a+ex-(a%ex)
        after = b - (b%ex)
        # print(ex,before,after)
        if before > after:
            ans_lst.append(0)
        else:
            bnum = before//ex
            anum = after//ex
            num = anum - bnum +1
            ans_lst.append(num)
            # print(anum,bnum,num)
    ans =(b-a +1) - (ans_lst[0] + ans_lst[1] - ans_lst[2])
    # print(ans_lst)
    print(ans)

main()

Submission status: Accepted

Solution code:
def gcd(a,b):
	return max(a,b) if min(a,b)==0 else gcd(min(a,b),max(a,b)%min(a,b))
a,b,c,d=map(int,input().split())
l=c*d//gcd(c,d)
cc=b//c-(a-1)//c
dd=b//d-(a-1)//d
ll=b//l-(a-1)//l
print(b-a+1-(cc+dd-ll))

Submission status: Accepted

Solution code:
import math

def main():
    A, B, C, D = map(int, input().split())
    lcm = C * D // math.gcd(C, D)
    C_div = B // C - (A - 1) // C
    D_div = B // D - (A - 1) // D
    C_D_div = B // lcm - (A - 1) //lcm
    print(B - A + 1 - C_div - D_div + C_D_div)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

A, B, C, D = MAP()

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

E = B - (B//C + B//D - B//(CD_lcm))
F = A - (A//C + A//D - A//(CD_lcm))

ans = E - F

if A % C != 0 and A % D != 0:
    ans += 1

print(ans)

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect, bisect_left, bisect_right

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

A, B, C, D = MAP()

def many_lcm(l):
    tmp = l[0]
    for i in range(1, len(l)):
        tmp = tmp * l[i]//gcd(tmp, l[i])
    return tmp



# B % C
# B - 
total = B - (A - 1)
# print(total)
A = A - 1
lc = many_lcm([C, D])
# print(lc)

a = A - (A // C + A // D - A // (lc))
b = B - (B // C + B // D - B // (lc))
print(b - a)


# (A - 1) % C
# B % C
# (A - 1) % D
# B % D

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    A, B, C, D = MAP()

    X = B // C - (A - 1) // C
    Y = B // D - (A - 1) // D
    g = (C * D) // gcd(C, D)
    Z = B // g - (A - 1) // g

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


if __name__ == '__main__':
    main()

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)

lcm_=lcm(C,D)
answer=B-A+1-(B//C-A//C)-(B//D-A//D)+(B//lcm_-A//lcm_)
if A%C==0:
    answer-=1
if A%D==0:
    answer-=1
if answer<=-1:
    print(0)
else:
    if A%C==0 and A%D==0:
        print(answer+1)
    else:
        print(answer)

Submission status: Accepted

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)
# INF =  float("inf")
INF = 10**18
import bisect
import statistics
mod = 10**9+7
# mod = 998244353

# リストのlcm
def lcm_base(x, y):
    return (x * y) // math.gcd(x, y)

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

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

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

Submission status: Accepted

Solution code:
import math
a, b, c, d = map(int, input().split())
e = c * d //math.gcd(c, d)
ans = b - a + 1
cnt_of_c = (b // c) - ((a - 1) // c)
cnt_of_d = (b // d) - ((a - 1) // d)
cnt_of_e = (b // e) - ((a - 1) // e)
ans -= cnt_of_c + cnt_of_d - cnt_of_e
#print(cnt_of_c, cnt_of_d, cnt_of_e)
print(ans)

Submission status: Accepted

Solution code:
import math
A, B, C, D = map(int, input().split())
CD = C * D // math.gcd(C, D)
ab = B - A + 1
c = B // C - (A - 1) // C
d = B // D - (A - 1) // D
cd = B // (CD) - (A - 1) // (CD)

print(ab - (c + d - cd))

Submission status: Accepted

Solution code:
import math


# def calc(n, c, d):
#     return n-(math.floor(n / c) +
#               math.floor(n / d) - math.floor(n / (c * d / math.gcd(c, d))))
def calc(n, c, d):
    return n-(n // c +
              n // d - n // (c * d // math.gcd(c, d)))


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

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
a,b,c,d=map(int,input().split())
from math import gcd
ans=0
def main(n):
    e=n//c+n//d-n//((c*d)//gcd(c,d))
    return n-e

print(main(b)-main(a-1))

Submission status: Accepted

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
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()))

a, b, c, d = LI()
ans = b - a + 1
lcm = c * d // math.gcd(c, d)
ans -= b // c - ((a-1) // c + 1) + 1
ans -= b // d - ((a-1) // d + 1) + 1
ans += b // lcm - ((a-1) // lcm + 1) + 1
print(ans)

Submission status: Accepted

Solution code:
import numpy
a,b,c,d= list(map(int, input().strip().split()))
e=numpy.lcm(c,d)
def kosuu(x):
    return(b//x-(a-1)//x)
print(b-a+1-kosuu(c)-kosuu(d)+kosuu(e))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

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

ans = b-(a-1)

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

ca = (a-1)//c
cb = b//c
da = (a-1)//d
db = b//d
cda = (a-1)//lcm(c,d)
cdb = b//lcm(c,d)
ans -= (cb+db-cdb) - (ca+da-cda)
print(ans)

Submission status: Accepted

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

def LCM(S, T):
    return (S*T)//math.gcd(S, T)

def f(N, S, T):#Nを含む
    return N- N//S - N//T + N//LCM(S, T)
    
    
print(f(B, C, D)-f(A-1, C, D))

Submission status: Accepted

Solution code:
from math import *

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

Submission status: Accepted

Solution code:
import math

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

#C,Dの最小公倍数
E = C*D//math.gcd(C,D)
def num_mult(fro,to,mult):
    goal = to//mult
    if fro % mult == 0:
        start = fro // mult
    else:
        start = fro // mult + 1
    
    return goal - start + 1

ans = B-A+1
#Cの倍数
ans -= num_mult(A,B,C)
#Dの倍数
ans -= num_mult(A,B,D)
#C,Dの公倍数
ans += num_mult(A,B,E)

print(ans)

Submission status: Accepted

Solution code:
import math

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

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

def num(i):
  ans = b//i - a//i
  if a % i == 0:
    ans +=1
  return ans

lcm_num = num(lcm)

cd_num  = num(c) + num(d)

num = cd_num - lcm_num

print(b-a+1-num)

Submission status: Accepted

Solution code:
import math
def yaku(n,p,q):
    lcm = (p*q)//math.gcd(p,q)
    return (n//p + n//q - n//lcm)
a,b,c,d = map(int,input().split())
print((b-a+1)-((yaku(b,c,d)) - yaku(a-1,c,d)))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    a,b,c,d=MI()
    cc=b//c - (a-1)//c
    dd=b//d - (a-1)//d
    
    import math
    g=(c*d)//math.gcd(c,d)
    gg=b//g - (a-1)//g
    
    print(b-a+1 -cc-dd+gg)

main()

Submission status: Accepted

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

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

g=b//lcm(c,d)-(a-1)//lcm(c,d)
ans=e+f-g
#print(e,f,g)
print(b-a+1-ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

cnt_C=B//C-(A-1)//C
cnt_D=B//D-(A-1)//D

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

ans=(B-A+1)-cnt_C-cnt_D+cnt_CD
print(ans)

Submission status: Accepted

Solution code:
import math

A,B,C,D = map(int, input().split())
E = C*D // math.gcd(C, D)
count =0
n = (A-1)//C
m = B//C
o = (A-1)//D
p= B//D
q= (A-1)//E
r= B//E
print((B-A+1) - ((m-n)+(p-o)-(r-q)))

Submission status: Accepted

Solution code:
import fractions
A,B,C,D = map(int,input().split())
#(B-B以下のCorDの倍数)-(A-1-A-1以下のCorDの倍数)
l = C*D//fractions.gcd(C,D)
x = B-(B//C+B//D-B//l)
y = (A-1)-((A-1)//C+(A-1)//D-(A-1)//l)
print(x-y)

Submission status: Accepted

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

def calc_div_num(a,b,x):
  return b//x - (a-1)//x

num_c = b//c - (a-1)//c
num_d = b//d - (a-1)//d

import math
lcd = int(c * d / math.gcd(c,d))
num_lcd = b//lcd - (a-1)//lcd

result = b-a+1 - (num_c+num_d-num_lcd)
print(result)

Submission status: Accepted

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

def calc_div_num(a,b,x):
  return b//x - (a-1)//x

num_c = calc_div_num(a, b, c)
num_d = calc_div_num(a, b, d)

import numpy as np
import math
#lcd = np.lcm(c, d)
lcd = c*d/math.gcd(c,d)
num_lcd = calc_div_num(a, b, int(lcd))

result = b-a+1 - (num_c+num_d-num_lcd)
print(result)

Submission status: Accepted

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

def calc_div_num(a,b,x):
  return b//x - (a-1)//x

num_c = calc_div_num(a, b, c)
num_d = calc_div_num(a, b, d)

import numpy as np
lcd = np.lcm(c, d)
num_lcd = calc_div_num(a, b, lcd)

result = b-a+1 - (num_c+num_d-num_lcd)
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
#a = [input() for _ in range(n)]

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

#b-a分を回してみるのは10**18なので間に合わない

#A以上B以下の整数でXで割り切れない個数を言い換えると
#Bが整数Xで割り切れない個数 - (A-1)が整数Xで割り切れない個数となる

#次にBが整数Xで割り切れない個数を考える。
#Bが整数Xで割り切れる個数は、B//Xなので、割り切れない個数は B-B//Xとなる

#次にBが整数XとYで割り切れない個数を考える
#B -B//X -B//Y + 2B//(X*Y)としたいところだが
#XとYでも割り切れるものは、B//(X*Y)ではなく、B//LCM(X,Y)となる
# X = 4, Y = 6だと 24で割り切れる個数となるが
# 12は、4と6でも割り切れるが24では割り切れない
#よってXとYの最小公倍数となる

import math

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

def func(val, x, y):
    return val-val//x-val//y+val//lcm(x,y)

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

Submission status: Accepted

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


def fn(n):
    lcm = (c * d) // math.gcd(c, d)
    return (n - (n // c) - (n // d) + (n // lcm))


print(fn(b)-fn(a-1))

Submission status: Accepted

Solution code:
import sys
import math

inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

a, b, c, d = inintm()

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

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

Submission status: Accepted

Solution code:
A, B, C, D = list(map(int, input().split()))
c1 = (A - 1) // C
c2 = B // C
d1 = (A - 1) // D
d2 = B //D
import math
E = (C * D) // math.gcd(C, D)
e1 = (A - 1) // E
e2 = B //E

print(B - A + 1 -(c2 - c1 + d2 - d1 - (e2 - e1)))

Submission status: Accepted

Solution code:
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
import itertools
from collections import Counter

def main():
    a,b,c,d = map(int, input().split())
    x = b//c - (a-1)//c
    y = b//d - (a-1)//d
    e = c*d//gcd(c,d)
    z = b//e - (a-1)//e
    ans = b-a+1 -x-y +z
    print(ans)

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
from collections import defaultdict as D
from functools import reduce
import numpy as np
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353    

A,B,C,D = LI()

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

# 下側は切り上げ
# tmp = (B//C)-math.ceil(A/C)+1 + (B//D)-math.ceil(A/D)+1 - (B//lcm - math.ceil(A/lcm) + 1)
tmp = (B//C)-((A-1)//C)+1 + (B//D)-((A-1)//D)+1 - (B//lcm - ((A-1)//lcm) + 1)
# 4-2+1, 3-2+1, 1-1+1
ans = B-(A-1)+1 - tmp
print(ans)

Submission status: Accepted

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

import math

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

cnt_a=a-a//c-a//d+a//lcm(c,d)
cnt_b=b-b//c-b//d+b//lcm(c,d)

if a%c==0 or a%d==0:
  print(cnt_b-cnt_a)
else:
  print(cnt_b-cnt_a+1)

Submission status: Accepted

Solution code:
import math

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

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

def cnt(x):
  return b//x - a//x + int(a%x==0)

print(b-a+1 - cnt(c) - cnt(d) + cnt(l))

Submission status: Accepted

Solution code:
from math import gcd


def count_indivs(N):
    return N - (N // C + N // D - N // L)


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

Submission status: Accepted

Solution code:
from math import gcd

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

Submission status: Accepted

Solution code:
def cnt(n):
    return n // c + n // d - n // e


from math import gcd

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

Submission status: Accepted

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