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

Submission status: Accepted

Solution code:

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

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

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

    count_c = B // C
    count_d = B // D
    count_union = B // lcm(C, D)

    total1 = B - count_c - count_d + count_union

    count_c = (A - 1) // C
    count_d = (A - 1) // D
    count_union = (A - 1) // lcm(C, D)

    total2 = A - 1 - count_c - count_d + count_union

    print(total1 - total2)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import fractions
import numpy as np
A, B, C, D = map(int, input().split())
# python3.5以前ではfractionsモジュールにgcdがあることに注意、それ以降ではmathモジュールにある
lcmofCD = int(C * D / fractions.gcd(C, D))
#A未満の「ＣでもＤでも割り切れない数」を求めたいのでA-1に対して計算を行うことに注意
divofAbyC = (A-1) // C
divofAbyD = (A-1) // D
divofBbyC = B // C
divofBbyD = B // D
divofBbylcm = B // lcmofCD
divofAbylcm = (A-1) // lcmofCD
ansofA = (A-1) - (divofAbyC + divofAbyD - divofAbylcm)
ansofB = B-(divofBbyC + divofBbyD - divofBbylcm)
ans = ansofB-ansofA

print("{}".format(ans))

Submission status: Accepted

Solution code:
import math
 
def gcd(a,b):
  while b!=0:
    a,b=b,a%b
  return a
 
def lcm(a,b):
  if a >= b:
    return a*b//gcd(a,b)
  else:
    return a*b//gcd(b,a)
 
 
A, B, C, D = map(int, input().split())
A -= 1

k1 = A // C
k2 = B // C
k3 = A // D
k4 = B // D
 
k5 = k2 - k1 + 1
k6 = k4 - k3 + 1
 
lcm_num = lcm(C, D)
k7 = A // lcm_num
k8 = B // lcm_num
 
k9 = k8 - k7 + 1
 
print(B - A + 1 - k5 - k6 + k9)

Submission status: Accepted

Solution code:
import fractions

A, B, C, D = map(int, input().split())
A -= 1
tmp = fractions.gcd(C, D)
lcd = (C * D) // tmp
acnt1 = A // C
acnt2 = A // D
acnt3 = B // C
acnt4 = B // D
acnt5 = A // lcd
acnt6 = B // lcd
ans = B - A
atmp = acnt3 + acnt4 - acnt1 - acnt2 + acnt5 - acnt6
ans -= atmp
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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


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

Submission status: Accepted

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

E = (C*D) // gcd(C,D)
x = (A-1) // C
y = (A-1) // D
z = (A-1) // E

xx = B // C
yy = B // D
zz = B // E

Cdiv = xx-x
Ddiv = yy-y
Ediv = zz-z
tmp = xx-x + yy-y - (zz-z)
total = B-A+1
print(int(total - Cdiv - Ddiv + Ediv))

Submission status: Accepted

Solution code:
import fractions
a,b,c,d = map(int,input().split())
count_c = 0
count_d = 0
count_cd = 0
count = 0
j = 1
g = c*d//fractions.gcd(c,d)

count_c = b//c - (a-1)//c
count_d = b//d - (a-1)//d
count_cd = b//g -(a-1)//g
count_s = count_c+count_d-count_cd
print(b-a+1-count_s)

Submission status: Accepted

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

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

def calc(A, B, x):
    s = A // x * x
    if s < A:
        s += x
    e = B // x * x
    result = (e - s) // x + 1
    #print(s, e, result)
    return result

result = B - A + 1 - (calc(A, B, C) + calc(A, B, D) - calc(A, B, C * D // gcd(C, D)))

print(result)

Submission status: Accepted

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

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

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

c_first = 0
c_last = 0
d_first = 0
d_last = 0
cd_first = 0
cd_last = 0

c_first = (A-1) // C
c_last = B // C
d_first = (A-1) // D
d_last = B // D
cd_first = (A-1) // CD
cd_last = B // CD

c_count = 0
d_count = 0
cd_count = 0
if c_last != 0:
  c_count = c_last - c_first
if d_last != 0:
  d_count = d_last - d_first
if cd_last != 0:
  cd_count = cd_last - cd_first
    
#print(c_first, c_last, d_first, d_last, cd_first, cd_last)
#print(c_count, d_count, cd_count)
print(B - A + 1 - (c_count + d_count - cd_count))

Submission status: Accepted

Solution code:
A, B, C, D = map(int, input().split())
import fractions
def lcm(x, y):
    return (x*y)//fractions.gcd(x,y)
ret = 0
ret += B//C - (A-1)//C
ret += B//D - (A-1)//D
L = lcm(C, D)
ret -= B//L - (A-1)//L
print(B-A+1-ret)

Submission status: Accepted

Solution code:
from math import ceil,floor
from fractions import gcd

def lcm(x, y):
    return (x * y) // 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())
A -= 1

cc = B//C - A//C
dd = B//D - A//D

E = C*D//gcd(C, D)
ee = B//E - A//E

print(B - A - cc - dd + ee)

Submission status: Accepted

Solution code:
import fractions
a = list(map(int, input().split()))
n = a[1]-a[0]+1
wac = a[1]//a[2]-(a[0]-1)//a[2]


wad= a[1]//a[3]-(a[0]-1)//a[3]

  
lcm = a[2]*a[3]//fractions.gcd(a[2], a[3])
cd =a[1]//lcm-(a[0]-1)//lcm


print(n-wac-wad+cd)

Submission status: Accepted

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

import fractions
import math

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

print(x-y)

Submission status: Accepted

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

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

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

Ac = (A-1)//C
Ad = (A-1)//D
Acd = (A-1)//CD
a = (A-1)-(Ac+Ad-Acd)

Bc = B//C
Bd = B//D
Bcd = B//CD
b = B-(Bc+Bd-Bcd)

print(b-a)

Submission status: Accepted

Solution code:
import fractions as f
a,b,c,d=map(int,input().split())
x=b-a+1
l=c*d//f.gcd(c,d)
i=b//c-(a-1)//c
j=b//d-(a-1)//d
k=b//l-(a-1)//l
if d%c==0 or c%d==0:
  print(x-max(i,j))
else:
  print(x-i-j+k)

Submission status: Accepted

Solution code:
from fractions import gcd

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

Submission status: Accepted

Solution code:
# 未完成
import fractions
import numpy as np
A, B, C, D = map(int, input().split())  # aaaaa
lcmofCD = int(C * D / fractions.gcd(C, D))
divofAbyC = (A-1) // C
divofAbyD = (A-1) // D
divofBbyC = B // C
divofBbyD = B // D  # aaaaa
divofBbylcm = B // lcmofCD
divofAbylcm = (A-1) // lcmofCD
ansofA = (A-1) - (divofAbyC + divofAbyD - divofAbylcm)
ansofB = B-(divofBbyC + divofBbyD - divofBbylcm)
ans = ansofB-ansofA

print("{}".format(ans))

Submission status: Accepted

Solution code:
# 2019/06/22
# AtCoder Beginner Contest 131 - C

import fractions
# Least Common Multiple
def lcm_fgf(a, b):
    return int(a * b / fractions.gcd(a, b))

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

clist = list()
dlist = list()
cdlist = list()

cd = lcm_fgf(c, d)

ccnt1 = (a - 1) // c
ccnt2 = b // c

dcnt1 = (a - 1) // d
dcnt2 = b // d

cdcnt1 = (a - 1) // cd
cdcnt2 = b // cd

ans = (b - a + 1) - ((ccnt2 - ccnt1) + (dcnt2 - dcnt1) - (cdcnt2 - cdcnt1))

# Output
print(ans)

Submission status: Accepted

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

_c = c
_d = d
while _d % _c != 0:
	_d, _c = _c, _d % _c
cd = (c * d) // _c

_a = (a//c) + (a//d) - (a//cd)
_b = (b//c) + (b//d) - (b//cd)
ans = (b - a) - (_b - _a)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

print(num-ex)

Submission status: Accepted

Solution code:
def gcd(x,y):
    if y>x:
        a=y
        y=x
        x=a
    while y:
        a=x%y
        x=y
        y=a
    return x
a,b,c,d=map(int,input().split())
ans=b-a+1
G=c*d//gcd(c,d)
ans-=(b-(b+1)%-c-(a-a%-c)+1)//c
ans-=(b-(b+1)%-d-(a-a%-d)+1)//d
ans+=(b-(b+1)%-G-(a-a%-G)+1)//G
print(ans)

Submission status: Accepted

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


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


def count(n):
    return b // n - (a - 1) // n


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

Submission status: Accepted

Solution code:
import math
A,B,C,D = map(int,input().split())
ans = 0
def gcd(a,b):
        while b!=0:
                a,b=b,a%b
        return a
 
def lcm(a,b):
        return a*b//gcd(a,b)
 
def kosu(x):
        global C,D
        return x-(x//C + x//D - x//lcm(C,D))

print(kosu(B)-kosu(A-1))

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


ans_C = B//C - (A-1)//C
ans_D = B//D - (A-1)//D
CD = (C*D) // gcd(C, D)
ans_CD = B//CD - (A-1)//CD

print(B-A+1 -(ans_C + ans_D - ans_CD))

Submission status: Accepted

Solution code:
import fractions
a,b,c,d = map(int,input().split())
cnt = 0
numc = b // c - (a - 1) // c
numd = b // d - (a - 1) // d
com = c * d // fractions.gcd(c,d)
numcd = b // com - (a - 1) // com
print(int(b - a + 1 - (numc + numd - numcd)))

Submission status: Accepted

Solution code:
from fractions import gcd
from math import ceil,floor
 
A,B,C,D=map(int, input().split())
 
Cans = B//C - (A-1)//C
Dans = B//D - (A-1)//D
 
CD = C * D // gcd(C,D)
CDans = B//CD - (A-1)//CD
 
ans = (B-A+1) - (Cans + Dans - CDans)
 
print(ans)

Submission status: Accepted

Solution code:
#import math
import fractions

def main():

    # 1 <= a <= b <= 10^18
    # 1 <= c,d <= 10^9

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

    ac = -(-a // c)
    bc = b // c

    ad = -(-a // d)
    bd = b // d

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

    acd = -(-a // cd)
    bcd = b // cd

    cm_num = bc - ac + 1
    dm_num = bd - ad + 1
    cdm_num = bcd - acd + 1

    count = cm_num + dm_num - cdm_num

    total = b - a + 1

    print(total - count)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 0


c_n = (B // C) - ((A - 1) // C)
d_n = (B // D) - ((A - 1) // D)
cd = C * D // fractions.gcd(C,D)
cd_n = (B // cd) - ((A - 1) // cd)

ans = (B - A + 1) - (c_n + d_n - cd_n)
print(ans)

Submission status: Accepted

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

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

ex = A - (A // C + A // D - A // lcm)
num = B - (B // C + B // D - B // lcm)


print(num-ex)

Submission status: Accepted

Solution code:
import fractions

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

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

b = B // C + B // D - B // G
am = (A-1) // C + (A-1) // D - (A-1) // G

result = B-A+1-(b-am)
print(result)

Submission status: Accepted

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

Submission status: Accepted

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

a,b,c,d = map(int,input().split())
t = 0
l = b-a+1

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

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

lc = (c*d) // gcd(c,d)
lc_count = (b // lc) - ((a-1) // lc)

print(l-c_count-d_count+lc_count)

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)

lcm_num = lcm(c,d)
c_num = (b//c - a//c) if a%c!=0 else (b//c - a//c + 1)
d_num = (b//d - a//d) if a%d!=0 else (b//d - a//d + 1)
ba_num = b//lcm_num - a//lcm_num if a%lcm_num!=0 else (b//lcm_num - a//lcm_num + 1)

if c == 1 or d == 1:
    print(0)
else:
    print(b-a+1-(c_num+d_num-ba_num) if b-a+1-(c_num+d_num-ba_num) > 0 else 0)

Submission status: Accepted

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

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


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

tmp = lcm(C,D) 


C_1 = B//C
C_2 = (A-1)//C
C_total = C_1 - C_2

D_1 = B//D
D_2 = (A-1)//D
D_total = D_1 - D_2

tmp_1 = B//tmp
tmp_2 = (A-1)//tmp
tmp_total = tmp_1 - tmp_2
cnt = C_total + D_total - tmp_total
print(total-cnt)

Submission status: Accepted

Solution code:
import fractions

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

tmp = fractions.gcd(C, D)
CD = (C * D) // tmp
ac = (A-1)//C
bc = B//C
ad = (A-1)//D
bd = B//D
acd = (A-1)//CD
bcd = B//CD

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

Submission status: Accepted

Solution code:
import sys
from collections import deque  # 双方向キュー
from collections import defaultdict  # 初期化済み辞書
from heapq import heapify, heappush, heappop, heappushpop  # プライオリティキュー
from bisect import bisect_left, bisect_right  # 二分探索
#import numpy as np  # 1.8.2
#import scipy  # 0.13.3

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

# A以上B以下にXの倍数が幾つあるか？ 1 < A < B
def check(A, B, X):
    x = (A-1) // X
    y = (B-0) // X
    return y - x

import fractions

def main():
    A, B, C, D = i2nn()
    # A以上B以下の数
    n = B - A + 1
    # Cの倍数
    n -= check(A, B, C)
    # Dの倍数
    n -= check(A, B, D)
    # GCD(C, D)の倍数
    E = C * D // fractions.gcd(C, D)
    n += check(A, B, E)
    print(n)

main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

from fractions import gcd
from math import ceil,floor

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

Cans = B//C - (A-1)//C
Dans = B//D - (A-1)//D

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

ans = (B-A+1) - (Cans + Dans - CDans)

print(ans)

Submission status: Accepted

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

mi = min(c, d)
ma = max(c, d)

l = int(mi ** (1 / 2))
r = []
s = mi
for i in range(1, l + 1):
    if (mi % i == 0):
        r.append([i, mi // i])
ind = len(r)
for i in range(ind):
    if ((ma * r[i][0]) % mi == 0):
        s = r[i][0]
        break
if (s == mi):
    for i in range(ind):
        if ((ma * r[ind - 1 - i][1]) % mi == 0):
            s = r[ind - 1 - i][1]
            break

e = ma * s
cl = a // c + 1 if a % c != 0 else a // c
dl = a // d + 1 if a % d != 0 else a // d
el = a // e + 1 if a % e != 0 else a // e
ch = b // c
dh = b // d
eh = b // e

print(b - a - (ch - cl + dh - dl) + eh - el)

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(x, y):
    return (x * y) // gcd(x, y)

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

Cnum = (B//C) - ((A-1)//C)
Dnum = (B//D) - ((A-1)//D)
Enum = (B//E) - ((A-1)//E)

num = allnum - Cnum - Dnum + Enum
print(int(num))

Submission status: Accepted

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

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)

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

Submission status: Accepted

Solution code:
import fractions

a,b,c,d = map(int, input().split())
def sakana(x):
    e = (a-1)//x
    f = b//x
    g = f - e
    return g
m = sakana(c)
n = sakana(d)
o = sakana(c*d//fractions.gcd(c,d))
print(b-a+1-(m+n-o))

Submission status: Accepted

Solution code:
from fractions import gcd
a,b,c,d=map(int,input().split(' '))
r1=b-b%c
if a%c!=0:
	l1=a-a%c+c
else:
	l1=a
r2=b-b%d
if a%d!=0:
	l2=a-a%d+d
else:
	l2=a
lcm=(c*d)//gcd(c,d)
r3=b-b%lcm
if a%lcm!=0:
	l3=a-a%lcm+lcm
else:
	l3=a
nc=(r1-l1+c)//c
nd=(r2-l2+d)//d
nlcm=(r3-l3+lcm)//lcm
divi=nc+nd-nlcm
out=b-a+1
if c>a and c>b and d>a and d>b:
	print(0)
else:
	print(out-divi)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest
import fractions
import math


#from numba import jit
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)


def resolve():
    A, B, C, D = map(int, input().split())
    size = B - A + 1
    C_warikire = B // C - (A - 1) // C
    D_warikire = B // D - (A - 1) // D
    C_and_D_warikire = B // lcm(C, D) - (A - 1) // lcm(C, D)
    print(size - C_warikire - D_warikire + C_and_D_warikire)
resolve()

Submission status: Accepted

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

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

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

CD = lcm(C,D)
ac = (A-1)//C
bc = B//C
ad = (A-1)//D
bd = B//D
acd = (A-1)//CD
bcd = B//CD

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

Submission status: Accepted

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

Submission status: Accepted

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

a,b,c,d =map(int,input().split())
e = (c*d)//gcd(c,d)
ac = a//c   
if a % c == 0:
    ac-=1
bc = b//c 

ad=a//d 
if a % d == 0:
    ad-=1
bd= b//d


acd = a//e 
if a % e == 0:
    acd-=1
bcd= b//e 


count = b-a+1
count -= bc-ac
count -= bd-ad
count += bcd-acd

print(count)
##print(e)
#print(bcd-acd)
#print(bd-ad)
#print(bc-ac)

Submission status: Accepted

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

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

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

cd=b//c-(a-1)//c
dd=b//d-(a-1)//d
cdd=b//(cdlcm)-(a-1)//(cdlcm)

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

Submission status: Accepted

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

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

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

cd=b//c-(a-1)//c
dd=b//d-(a-1)//d
cdd=b//(cdlcm)-(a-1)//(cdlcm)

print(round(b-a+1-(cd+dd-cdd)))

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)

divc = B // C - (A - 1) // C
divd = B // D - (A - 1) // D
dive = B // E  - (A - 1) // E

print(B + 1 - A - (divc + divd - dive))

Submission status: Accepted

Solution code:
import fractions
a,b,c,d=map(int,input().split())
 
s=c*d//fractions.gcd(c, d)
bk=0
ak=0
ans=0
bc=b//c
ac=a//c    
bd=b//d
ad=a//d
bsa=b//s
asa=a//s
bk=b-bc-bd+bsa
ak=a-ac-ad+asa
ans=bk-ak
if a%c==0 or a%d==0 or a%s==0:
    ans=ans
else:
    ans+=1
print(ans)

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

Submission status: Accepted

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

Submission status: Accepted

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


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

def main():
    a, b, c, d = MAP()

    l = c * d // gcd(c, d)
    a -= 1

    kukan1 = b - b // c - b // d + b // l
    kukan2 = a - a // c - a // d + a // l

    print(kukan1 - kukan2)


if __name__ == '__main__':
    main()

Submission status: Accepted

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


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

a1=0
a2=0
b1=0
b2=0
e1=0
e2=0


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

if(a-1>=c):
    a1=(a-1)//c
else:
    a1=0

if(b>=c):
    b1=b//c
else:
    b1=0

if(a-1>=d):
    a2=(a-1)//d
else:
    a2=0

if(b>=d):
    b2=b//d
else:
    b2=0

if(a-1>=e):
    e1=(a-1)//e
else:
    e1=0

if(b>=e):
    e2=b//e
else:
    e2=0
        
ans=b-a+1-(b1-a1)-(b2-a2)+(e2-e1)

print(ans)

Submission status: Accepted

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

import fractions

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

com = lcm(c,d)

total = b - a + 1
fir = b//c - (a-1)//c
sec = b//d - (a-1)//d
rem = b//com - (a-1)//com

result = total - (fir + sec -rem)
  
print(result)

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(" "))
min_C = B//C-(A-1)//C
min_D = B//D-(A-1)//D
lcm = lcm(C,D)
min_lcm = B//lcm-(A-1)//lcm
print(B-A-min_C-min_D+min_lcm+1)

Submission status: Accepted

Solution code:
from fractions import gcd
A, B, C, D = map(int, input().split())
#l = [i for i in range(A, B+1)]
div_a_c = (A-1) // C
div_b_c = B // C
div_c = div_b_c - div_a_c

div_a_d = (A-1) // D
div_b_d = B // D
div_d = div_b_d - div_a_d

lcm = C * D // gcd(C, D)
div_a_l = (A-1) // lcm
div_b_l = B // lcm
div_l = div_b_l - div_a_l
#print(div_c, div_d, div_l)
divs = div_c + div_d - div_l
print(B-A+1-divs)

Submission status: Accepted

Solution code:
import fractions

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

cd = (C * D) // fractions.gcd(C, D)
bc = int(B // C)
bd = int(B // D)
bcd = int(B // cd)
ac = int((A-1) // C)
ad = int((A-1) // D)
acd =int((A-1) // cd)

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

Submission status: Accepted

Solution code:
import fractions

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

ans = 0

if c % d == 0:
    print((b - a + 1) - ((b)//d - (a-1)//d))
elif d % c == 0:
    print((b - a + 1) - ((b)//c - (a-1)//c))
else:
    tot = c * d // fractions.gcd(c, d)
    tmp = ((b) // d - (a-1) // d) + ((b)//c - (a-1)//c) - ((b) // tot - (a-1) // tot)
    print(int((b - a + 1) - tmp))

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)
  
cd = lcm(c,d)
dab = b -a

if a%c != 0:
  cf = a + (c - a%c)
else:
  cf = a
ce = b - (b%c)
if a%d != 0:
  df = a + (d - a%d)
else:
  df = a
de = b - (b%d)
if a%cd != 0:
  cdf = a + (cd - a%cd)
else:
  cdf = a
cde = b - (b%cd)
nc = (ce-cf + c )//c
nd = (de-df + d )//d
ncd = (cde-cdf + cd )//cd
print(dab-(nc+nd-ncd)+1)

Submission status: Accepted

Solution code:
from fractions import gcd

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

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


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

Submission status: Accepted

Solution code:

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

MofD = 0
MofCD = 0

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

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


def sten(st, en, x):
    if x == 1:
        return en - st + 1
    st = st // x if st % x == 0 else st // x + 1
    en = en // x
    st *= x
    en *= x
    if en >= st:
        n = (en - st) // x + 1
    else:
        n = 0
    return n


MofC = sten(A, B, C)
if C != D:
    MofD = sten(A, B, D)
    MofCD = sten(A, B, lcm(C, D))

print(U-MofC-MofD+MofCD)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def gcd(a,b):
  while b!=0:
    a,b=b,a%b
  return a
lis=list(map(int,input().split()))
A=lis[0]
B=lis[1]
C=lis[2]
D=lis[3]
sum=B-A+1

times=C*D//gcd(C,D)
restac=A%C
restbc=B%C
restad=A%D
restbd=B%D
restat=A%times
restbt=B%times
x=int((B-restbc)//C)-int((A-restac)//C)
y=int((B-restbd)//D)-int((A-restad)//D)
z=int((B-restbt)//times)-int((A-restat)//times)
if restac==0:
  x+=1
if restad==0:
  y+=1
if(restat==0):
  z+=1
print(sum-x-y+z)

Submission status: Accepted

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


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


def abc():
    from math import ceil
    a, b, c, d = map(int, input().split())

    cnt_all = b - a + 1

    l = ceil(a / c)
    r = b // c
    cnt_c = r - l + 1

    l = ceil(a / d)
    r = b // d
    cnt_d = r - l + 1

    cd = lcm(c, d)
    l = ceil(a / cd)
    r = b // cd
    cnt_cd = r - l + 1

    ans = cnt_all - (cnt_c + cnt_d - cnt_cd)
    if ans < 0:
        ans = 0
    print(ans)


abc()

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

Submission status: Accepted

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

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

e=int(c/u(c,d)*d)
a=a-1
ac = (a // c)
bc = (b // c)
ad = (a // d)
bd = (b // d)
ae = (a // e)
be = (b // e)

print(b - a - (bc - ac + bd - ad ) + (be - ae))

Submission status: Accepted

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

Submission status: Accepted

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


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


# Function to return LCM of two numbers
def lcm(a, b):
    return (a * b) // gcd(a, b)

s1 = (a-1)//c
s2 = b//c
s3 = (a-1)//d
s4 = b//d
k = lcm(c, d)
s5 = (a-1)//(k)
s6 = b//(k)
# print((s2-s1),(s4-s3),(s6-s5))
s = (s2-s1)+(s4-s3)-(s6-s5)
print(b-a+1-s)

Submission status: Accepted

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

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


a, b, c, d = (int(i) for i in input().split())
m = int(lcm(c, d))
#print(m)
print((b-a+1)-(int((b)//c)-int((a-1)//c))-(int((b)//d)-int((a-1)//d))+(int((b)//(m))-int((a-1)//(m))))
##print(m)
#print("b-a", (b-a+1))
#print(int((b)//c)-int((a-1)//c))
#print((int((b)//d)-int((a-1)//d)))
#print((int((b-1)//m)-int((a-1)//m)))

Submission status: Accepted

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

def div (n):
    rt = lg // n
    if lg % n != 0 and ((n- a%n) + (b%n) < n):
        rt += 1
    return(rt)

cm = c * d // fractions.gcd(c,d)
ans = lg - div(c) - div(d) + div(cm)
print(ans)

Submission status: Accepted

Solution code:
a,b,c,d=map(int,input().split())
gcdc=[]
gcdd=[]
for i in range(1,int(c**(1/2))+1):
  if c%i==0:
    gcdc.append(i)
    if i!=c//i:
      gcdc.append(c//i)
for i in range(1,int(d**(1/2))+1):
  if d%i==0:
    gcdd.append(i)
    if i!=d//i:
      gcdd.append(d//i)
E=[]
for i in range(len(gcdc)):
  if gcdc[i] in gcdd:
    E.append(gcdc[i])
e=max(E)
f=c*d//e
if a%c==0:
  g=b//c-a//c+1
else:
  g=b//c-a//c
if a%d==0:
  h=b//d-a//d+1
else:
  h=b//d-a//d
if a%f==0:
  i=b//f-a//f+1
else:
  i=b//f-a//f
print(b-a+1-g-h+i)

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

Submission status: Accepted

Solution code:
a,b,c,d=map(int, input().split())
count = 0
num=b-a+1
import fractions
lcm= int(c*d / fractions.gcd(c,d))
ac=(a-1)//c
ad=(a-1)//d
acd=(a-1)//lcm

bc=b//c
bd=b//d
bcd=b//lcm

print(num-((bc+bd-bcd)-(ac+ad-acd)))

Submission status: Accepted

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

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

num_lesser_than_A_non_dividable_by_C = (A - 1) - (A - 1) // C
num_lesser_than_A_non_dividable_by_D = (A - 1) - (A - 1) // D
num_lesser_than_A_non_dividable_by_C_or_D = (A - 1) - (A - 1) // lcm
num_lesser_than_A_dividable_by_C_nor_D = num_lesser_than_A_non_dividable_by_C + num_lesser_than_A_non_dividable_by_D - num_lesser_than_A_non_dividable_by_C_or_D
num_not_larger_than_than_B_non_dividable_by_C = B - B // C
num_not_larger_than_than_B_non_dividable_by_D = B - B // D
num_not_larger_than_than_B_non_dividable_by_C_or_D = B - B // lcm
num_not_larger_than_than_B_dividable_by_C_nor_D = num_not_larger_than_than_B_non_dividable_by_C + num_not_larger_than_than_B_non_dividable_by_D - num_not_larger_than_than_B_non_dividable_by_C_or_D
print(num_not_larger_than_than_B_dividable_by_C_nor_D - num_lesser_than_A_dividable_by_C_nor_D)

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

num = b - a + 1
divc_b = b // c
divd_b = b // d
divcd_b = b // (lcm(c, d))
divc_a = (a-1) // c
divd_a = (a-1) // d
divcd_a = (a-1) // (lcm(c, d))

print(num - (divc_b + divd_b - divcd_b) + (divc_a + divd_a - divcd_a))