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):
    while y!=0:
        x,y=y,x%y
    return x

NBC = B//C
NBD = B//D
CD_lcm = (C*D)//gcd(C,D)   #CとDの最小公倍数
NBCD = B//CD_lcm #CとD共通で割り切れる数
B_0 = (NBC+NBD)-NBCD  #Bの答え
ans_B = B-B_0
NAC = (A)//C
NAD = (A)//D
NACD = (A)//CD_lcm  
A_0 = (NAC+NAD)-NACD #Aの答え
ans_A = A-A_0
if A%C==0 or A%D==0:
    ans = ans_B-ans_A
else:
    ans = ans_B-ans_A+1
print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd
from decimal import *

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

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)


CD = lcm(C, D)

#print(CD)

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



#print(C_count, D_count, CD_count)

print(B - A + 1 - C_count - D_count + CD_count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#data=list(input().split())
a,b,c,d=map(int,input().split())
def gcd(x , y):
    if y == 0:
        return x 
    else:
        return gcd(y, x%y)

def gys(a,b):
    remainder = gcd(a,b)
    return a*b//remainder

lcmm=gys(c,d)
def ans(n,k):
    return n//k
def x(s):
    return ans(s,c)+ans(s,d)-ans(s,lcmm)
#print(x(b),x(a))

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

Submission status: Accepted

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

def lcm(a,b):
    return a//gcd(a,b)*b
a,b,c,d=[int(i) for i in input().split()]
bac=(b//c)-((a-1)//c)
bad=(b//d)-((a-1)//d)
bacd=(b//lcm(c,d))-((a-1)//lcm(c,d))
print(b+1-a-bac-bad+bacd)

Submission status: Accepted

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

c_BC = B // C *(C - 1) + B % C
c_BD = B // D * (D - 1) + B % D
c_BCD = B // CD * (CD - 1) + B % CD
c_AC = A // C * (C - 1) + A % C
c_AD = A // D * (D - 1)+ A % D
c_ACD = A // CD * (CD - 1) + A % CD

ans = c_BC + c_BD - c_BCD - (c_AC + c_AD - c_ACD)
print(int(ans))

Submission status: Accepted

Solution code:
import fractions
a,b,c,d = map(int,input().split())
ac = (a-1)//c
ad = (a-1)//d
bc = b//c
bd = b//d
saisyounum = int(c * d / fractions.gcd(c, d))
checknum = (b//saisyounum)-((a-1)//saisyounum)
ans = int((bc-ac)+(bd-ad)-checknum)
print(b-a-ans+1)

Submission status: Accepted

Solution code:

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

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

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

s = -1
c = C * (A // C + 1)
if A % C == 0:
    c = A
if c <= B:
    s += (B - c) // C + 1
        
d = D * (A // D + 1)
if A % D == 0:
    d = A
if d <= B:
    s += (B - d) // D + 1
    
cd_lcm = lcm(C, D)
cdl = cd_lcm * (A // cd_lcm + 1)
if A % cd_lcm == 0:
    cdl = A
if cdl <= B:
    s -= (B - cdl) // cd_lcm + 1

print(B - A - s)

Submission status: Accepted

Solution code:
import fractions
A,B,C,D=map(int,input().split())
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
'''
def get_factors(x,retlist):
    for i in range(2, int(x**0.5) + 3):
        while x % i == 0:
            if i not in retlist:
              retlist.append(i)
            x = x // i
    if x!=1:
      retlist.append(x)
    return retlist
  
prime=[]
prime=get_factors(C,prime)
prime=get_factors(D,prime)
'''
AmodC=A%C
AmodD=A%D
BmodC=B%C
BmodD=B%D
CD=lcm(C,D)
AmodCD=A%CD
BmodCD=B%CD

BA=B-A+1
Cbaisu=BA//C
Dbaisu=BA//D
CDbaisu=BA//CD
#print(BA)

if BA-(Cbaisu*C)>=C-((A+(Cbaisu*C))%C)+1:
  Cbaisu+=1
if BA-(Dbaisu*D)>=D-((A+(Dbaisu*D))%D)+1:
  Dbaisu+=1
if BA-(CDbaisu*CD)>=CD-((A+(CDbaisu*CD))%CD)+1: 
  CDbaisu+=1
#print(Cbaisu)
#print(Dbaisu)
#print(CDbaisu)
print(BA-Cbaisu-Dbaisu+CDbaisu)

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())
n=b-a+1
dc=(a%c==0)*1
dd=(a%d==0)*1
div_c=b//c-a//c+dc
div_d=b//d-a//d+dd
lcm=(c*d)//gcd(c,d)
dcd=(a%lcm==0)*1
div_cd=b//lcm-a//lcm+dcd
print(n-div_c-div_d+div_cd)

Submission status: Accepted

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

a,b,c,d = map(int, input().split())
ac = (a-1)//c
ad = (a-1)//d
bc = b//c
bd = b//d
cd = int(c*d/(gcd(c,d)))
acd = (a-1)//cd
bcd = b//cd
aaa = bc+bd-bcd-ac-ad+acd
print(b-a+1-aaa)

Submission status: Accepted

Solution code:
from fractions import gcd

def calc(A, B, x):
    n1 = (A - 1) // x
    n2 = B // x
    return n2 - n1

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

Submission status: Accepted

Solution code:
import fractions

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

num = B - A + 1


# e = num // lcm

if A % C == 0:
    first_c = A
else:
    first_c = A + (C - A % C)
if A % D == 0:
    first_d = A
else:
    first_d = A + (D - A % D)
if A % lcm == 0:
    first_e = A
else:
    first_e = A + (lcm - A % lcm)

c = (B - first_c) // C + 1
d = (B - first_d) // D + 1
e = (B - first_e) // lcm + 1

print(num - c - d + e)

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

b_num = B//C + B//D - B//lcm(C, D)
a_num = (A-1)//C + (A-1)//D - (A-1)//lcm(C, D)

print((B-A+1) -(b_num - a_num))

Submission status: Accepted

Solution code:
import fractions

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


#A-BまでのCの個数

Cnum=int((B-A+1)//C)+ ((B-A+1)%C >= (C-(A-1)%C))
Dnum=int((B-A+1)//D)+ ((B-A+1)%D >= (D-(A-1)%D))

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

CDnum=int((B-A+1)//CD)+ ((B-A+1)%CD >= (CD-(A-1)%CD))

print(B-A+1-Cnum-Dnum+CDnum)

Submission status: Accepted

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

import fractions
CD = C * D // fractions.gcd(C, D)

# C, D, C*Dで割り切れるものの個数
div_C = B//C - (A//C + (-1 if A%C==0 else 0))  # 40//6 - 10//6 -> 6 - 1 = 5
div_D = B//D - (A//D + (-1 if A%D==0 else 0))  # 40//8 - 10//8 -> 5 - 1 = 4
div_CD = B//(CD) - max(0, (A//(CD) + (-1 if A%(CD)==0 else 0)))
print(B-A+1 - div_C - div_D + div_CD)

Submission status: Accepted

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

E = (C * D) // fractions.gcd(C, D)
number = B - A + 1 - (B // C - (A - 1) // C + B // D - (A - 1) // D - B // E + (A - 1) // E)
    
print(number)

Submission status: Accepted

Solution code:
import sys
import fractions
input = sys.stdin.readline
A, B, C, D = map(int, input().split())
root_count = B - A + 1
c = B//C - (A - 1)//C
d = B//D - (A - 1)//D
cd_lcm = C * D // fractions.gcd(C, D)
e = B//cd_lcm - (A - 1)//cd_lcm
print(root_count - c - d + e)

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())
ac_can = int((a-1)//c)
ad_can = int((a-1)//d)
acd_can = int((a-1)//lcm(c,d))
bc_can = int(b//c)
bd_can = int(b//d)
bcd_can = int(b//lcm(c,d))
print(b-a+1-(bc_can+bd_can-bcd_can-(ac_can+ad_can-acd_can)))

Submission status: Accepted

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

CSeries = B//C -(A-1)//C
DSeries = B //D - (A-1) //D

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

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

CDSeries = B//LCM - (A-1)//LCM

Answer2 = B-A +1 - CSeries - DSeries + CDSeries

print(Answer2)

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

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

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

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

Submission status: Accepted

Solution code:
import sys
import fractions
sys.setrecursionlimit(1000000)

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


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

c_w = (b)//c - (a+c-1)//c + 1
d_w = (b)//d - (a+d-1)//d + 1

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



cd = lcm(c,d)

cd_w = (b)//(cd) - (a+cd-1)//(cd) + 1


print(int(b-a+1 - c_w - d_w + cd_w))

Submission status: Accepted

Solution code:
a,b,c,d=map(int,input().split())
#最大公約数
def gcd(x,y):
  r=x%y
  return gcd(y,r) if r else y
#最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

lcmnum = lcm(c,d)
cs=0
ds=0
cds=0

acs = a // c
ads = a // d
acds =  a // lcmnum
aa = a-(acs+ads-acds)

bcs = b // c

bds = b // d
bcds =  b // lcmnum
bb = b-(bcs+bds-bcds)
uo = 1

if a % c==0 or a % d ==0:
	uo-=1

print(bb-aa+uo)

Submission status: Accepted

Solution code:

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

temp=list(map(lambda x:int(x),input().split()))
A,B,C,D=temp[0],temp[1],temp[2],temp[3]
public_cd=gcd(C,D)

c_n_full=B//C
d_n_full=B//D
cd_n_full=B//((C*D)//public_cd)

c_n_par=(A-1)//C
d_n_par=(A-1)//D
cd_n_par=(A-1)//((C*D)//public_cd)

c_n=c_n_full-c_n_par
d_n=d_n_full-d_n_par
cd_n=cd_n_full-cd_n_par
counter=(B-A+1)-(c_n+d_n)+cd_n

print(counter)

Submission status: Accepted

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

Submission status: Accepted

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

def lcm(a, b):
    ori_a, ori_b = a, b
    if a > b: a, b = b, a
    while True:
        a, b = b%a, a
        if a == 0:
            g = b
            break
        elif a == 1:
            g = 1
            break

    return ori_a // g * ori_b

S1 = A - ( A//C + A//D - A//lcm(C, D) ) - (1 if A%C and A%D else 0)
S2 = B - ( B//C + B//D - B//lcm(C, D) )

print(S2 - S1)

Submission status: Accepted

Solution code:
import sys
from fractions import gcd

input = sys.stdin.readline
A, B, C, D = map(int, input().split(' '))
num = B - A + 1
div_c = B // C - (A - 1) // C
div_d = B // D - (A - 1) // D
lcd = C * D // gcd(C, D)
div_cd = B // lcd - (A - 1) // lcd
print(num - div_c - div_d + div_cd)

Submission status: Accepted

Solution code:
import fractions

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

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

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

Submission status: Accepted

Solution code:
def test(a,b,u):
    ret = 0
    x,y = divmod(a,u)
    if y>0:
        x = x+1
    z = b//u
    if (u*x)>=a and (u*x)<=b:
        ret = ret + 1
        if z>x:
            ret = ret + z-x
    return ret
def Gcd(x,y):
    if( y>x ): # x>yにする
        r = x
        x = y
        y = r
    if( y==0 ):
        return x
    return Gcd(y,x%y)
def lcm(x,y):
    return (x*y)//Gcd(x,y)

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

ans = (b-a+1-test(a,b,c)-test(a,b,d)+test(a,b,lcm(c,d)))
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 lcm(a, b):
    return a * b // gcd (a, b)

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

Submission status: Accepted

Solution code:
import fractions
s = input().split()
A = int(s[0])
B = int(s[1])
C = int(s[2])
D = int(s[3])
def lcm(c, d):
  return (c*d)//fractions.gcd(c, d)
count = 0
C_value = B//C -(A-1)//C
D_value = B//D -(A-1)//D
value = B//(lcm(C, D)) - (A-1)//(lcm(C, D))
print(B-(A-1)-(C_value + D_value - value))

Submission status: Accepted

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

    r = a % b
    while r!=0:
        a = b
        b = r
        r = a % b
    return b

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

A, B, C, D = list(map(int, input().split()))
A_ = A - 1
lcm_CD = lcm(C, D)
B_res = B - (B//C + B//D - B//lcm_CD)
A_res = A_ - (A_//C + A_//D - A_//lcm_CD)
print(B_res - A_res)

Submission status: Accepted

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

def ABC(x,y,z):
    return z-(z//x+z//y-z//lcm(x,y))

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

Submission status: Accepted

Solution code:
from fractions import gcd

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

Submission status: Accepted

Solution code:
from decimal import Decimal, ROUND_FLOOR


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 divisible_count(a, b, d):
    a = Decimal(str(a))
    b = Decimal(str(b))
    d = Decimal(str(d))

    x = (b / d).quantize(Decimal("0"), rounding=ROUND_FLOOR)
    y = ((a - 1) / d).quantize(Decimal("0"), rounding=ROUND_FLOOR)
    return int(x - y)


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

x = divisible_count(A, B, C)
y = divisible_count(A, B, D)
z = divisible_count(A, B, lcm(C, D))

print(B - A + 1 - (x + y - z))

Submission status: Accepted

Solution code:
import fractions

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

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

x= (C * D // fractions.gcd(C, D))
ee = (B//x)-((A-1)//x)

ans = (B-A+1)-(cc + dd - ee)
print(int(ans))

Submission status: Accepted

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

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


A, B, C, D = map(int, input().split())
ans = set()
if A%C == 0:
    c = (B//C) - (A//C) + 1
else:
    c = (B//C) - (A//C)

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

CD = lcm(C, D)

if A%CD == 0:
    cd = (B//CD) - (A//CD) + 1
else:
    cd = (B//CD) - (A//CD)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from fractions import gcd

def count_n(mini, maxi, n):
    min_n = n*(mini//n) if mini%n == 0 else n*(mini//n+1)
    max_n = n * (maxi//n)
    return (max_n-min_n)//n+1

a,b,c,d = [int(i) for i in input().split()]
cd = c * d // gcd (c, d)
count_1 = count_n(a,b,c)
count_2 = count_n(a,b,d)
count_3 = count_n(a,b,cd)
count = b-a+1 - (count_1 + count_2 - count_3) 
print(count)

Submission status: Accepted

Solution code:
import fractions

inp = [int(i) for i in input().split()]
c1 = (inp[0]-1) // inp[2]
c2 = inp[1] // inp[2]
c = c2 - c1

d1 = (inp[0]-1) // inp[3]
d2 = inp[1] // inp[3]
d = d2 -d1

e1 = (inp[0]-1) // int(inp[3] * inp[2]/fractions.gcd(inp[3],inp[2]))
e2 = inp[1] // int(inp[3] * inp[2]/fractions.gcd(inp[3],inp[2]))
e = e2 - e1

print(int(inp[1]-inp[0]+1-(c+d-e)))

Submission status: Accepted

Solution code:
import fractions
import sys

input = sys.stdin.readline

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

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

tmp = b - a + 1

target_value=lcm(c,d)


c_a_under=(a-1)//c
c_b_under=b//c

c_a2b=c_b_under-c_a_under


d_a_under=(a-1)//d
d_b_under=b//d

d_a2b=d_b_under-d_a_under


cd_a_under=(a-1)//target_value
cd_b_under=b//target_value

cd_a2b=cd_b_under-cd_a_under

mayans = c_a2b+d_a2b-cd_a2b

print(tmp-mayans)

Submission status: Accepted

Solution code:
import math
import sys
input=sys.stdin.readline
from collections import deque
from heapq import heappush,heappop
import re

def int_raw():
    return int(input())
 
def ss_raw():
    return input().split()
 
def ints_raw():
    return tuple(map(int, ss_raw()))


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


DIV = 10**9+7

def ncr(n, r, mod=DIV):
	r = min(r, n-r)
	ret = 1
	for i in range(1, r+1):
		ret = ret * (n-i+1) % mod
		ret = ret * mod_inv(i, mod) % mod
	return ret
    
def mod_modify(a,mod):
    return (a+mod)%mod

def cal_ABC(A,B,C):
    AC = A//C + (1 if A%C>0 else 0)
    BC = (B//C)
    return max(BC-AC+1,0)


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

def main():
    ans =0
    A,B,C,D = ints_raw()
    total = B-A+1
    dC = cal_ABC(A,B,C)
    dD = cal_ABC(A,B,D)
    dCD = cal_ABC(A,B,C*D//gcd(max(C,D),min(C,D)))
    return total-dC-dD+dCD

print(main())

Submission status: Accepted

Solution code:
import sys

def solve(a, b, c, d):
    c_num = b / c - a / c
    if a % c == 0:
        c_num += 1
    d_num = b / d - a / d
    if a % d == 0:
        d_num += 1

    v = c * d / gcd(c, d)
    v_num = b / v - a / v
    if a % v == 0:
        v_num += 1

    return (b - a + 1) - (c_num + d_num - v_num)


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


if __name__ == '__main__':

    a, b, c, d = map(int, sys.stdin.readline().strip().split(" "))
    print solve(a, b, c, d)

Submission status: Accepted

Solution code:
a,b,c,d=map(int,input().split())
import fractions
h=int(c*d/fractions.gcd(c,d))
if(a%c!=0):
    e=(b//c)-(a//c)
else:
    e=(b//c)-(a//c)+1
if(a%d!=0):
    f=(b//d)-(a//d)
else:
    f=(b//d)-(a//d)+1
if(a%(h)!=0):
    g=(b//(h))-(a//(h))
else:
    g=(b//(h))-(a//(h))+1
print('{0}'.format(b-a+1-e-f+g))

Submission status: Accepted

Solution code:
def gcd(a, b):  # greatest common divisor
    if (a < b):
        a,b = b,a
    while(b>0):
        a,b = b ,a%b
    return a


def lcm(a, b):  #  least common multiple
    return int(a*b/gcd(a,b))


A, B, C, D = [int(n) for n in input().split()]
intnum = B-A  + 1
CmulA = A // C
CmulB = B // C
Cmulnum = CmulB - CmulA
if A % C == 0:
    Cmulnum += 1
DmulA = A // D
DmulB = B // D
Dmulnum = DmulB - DmulA
if A % D == 0:
    Dmulnum += 1
CD = lcm(C,D)
CDmulA = A // CD
CDmulB = B // CD
CDmulnum = CDmulB - CDmulA
if A % CD == 0:
    CDmulnum += 1
print(intnum - (Cmulnum + Dmulnum) + CDmulnum)

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

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

min1 = Am // C
min2 = Am // D
min3 = Am // lcm

max1 = B // C
max2 = B // D
max3 = B // lcm

n = B - max1 - max2 + max3 - (Am - min1 - min2 + min3)
print(n)

Submission status: Accepted

Solution code:
import fractions
a,b,c,d= map(int,input().split())
count = 0
kou = (c*d) // fractions.gcd(c,d)
#print(kou)
if a % kou == 0:
    hajime = a
else :
    hajime = (kou - a % kou) + a
owari = b - (b % kou)
if a % c == 0:
    chajime = a
else :
    chajime = (c - a % c) + a
cowari = b - (b % c)
if a % d == 0:
    dhajime = a
else :
    dhajime = (d - a % d) + a
dowari = b - (b % d)


kousuu = ((cowari - chajime) // c + 1) + ((dowari - dhajime) // d + 1) - ((owari - hajime) // kou + 1)
sa = b - a + 1
print(sa - (kousuu))
#print(hajime,owari,chajime,cowari,dhajime,dowari,)

Submission status: Accepted

Solution code:
import fractions
A,B,C,D = map(int, input().split())
c_d = C * D // fractions.gcd(C, D)
Cd = B // C - (A - 1) // C
Dd = B // D - (A - 1) // D
C_D = max(B // c_d - (A - 1) // c_d,0)
print((B + 1 - A) - (Cd + Dd - C_D))

Submission status: Accepted

Solution code:
def computeGCD(x, y):
    while (y):
        x, y = y, x % y
    return x

def computeLCM(x, y):
    return x * y // computeGCD(x, y)

a, b, c, d = map(int, input().split())
ways1 = b // c - ((a - 1) // c)
ways2 = b // d - ((a - 1) // d)
lcm = computeLCM(c, d)
ways = ways1 + ways2 - (b // lcm - ((a - 1) // lcm))
print(b - a + 1 - ways)

Submission status: Accepted

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


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


a = (A-1) // C
b = B // C
c = (A-1) // D
d = B // D
x = lcm(C, D)
e = (A-1) // x
f = B // x

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

Submission status: Accepted

Solution code:
def lcm(a, b):
    from fractions import gcd
    return (a * b) // gcd(a, b)


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

all_warikire = B - (B // C) - (B // D) + B // LCM
sub_warikire = A - (A // C) - (A // D) + A // LCM

print(all_warikire - sub_warikire)

Submission status: Accepted

Solution code:
import math
A,B,C,D = (int(x) for x in 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 = lcm(C,D)

if A-1 < C:
  fst = B//C
else:
  fst = B//C - (A-1)//C

if A-1 < D:
  snd = B//D
else:
  snd = B//D - (A-1)//D

if A-1 < lcm:
  dlt = B//lcm
else:
  dlt = B//lcm - (A-1)//lcm

print(B-A+1 - (fst+snd-dlt))

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)

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

Submission status: Accepted

Solution code:
import fractions

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

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

Sum_A = (A-1) - ((A-1)//C + (A-1)//D - (A-1)//(Min))
Sum_B = B - (B//C + B//D - B//(Min))
Sum = Sum_B - Sum_A

print(Sum)

Submission status: Accepted

Solution code:
import fractions
def LCM(a,b):
    return int(a*b/fractions.gcd(a,b))
A,B,C,D = map(int,input().split())#A以上B以下
L = LCM(C,D)
Cbaisu = (B//C) - ((A-1)//C)
Dbaisu = (B//D) - ((A-1)//D)
Lbaisu = (B//L) - ((A-1)//L)
print(B-A+1 - Cbaisu - Dbaisu + Lbaisu)

Submission status: Accepted

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


def gcd(m, n):
    if m < n:
        return gcd(n, m)
    if m % n == 0:
        return n
    return gcd(n, m % n)


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


def solv(a, b, c, d):
    if b <= a:
        return 0

    lcm_cd = lcm(c, d)
    if a == 0:
        c0 = 0
    else:
        a = a - 1
        c0 = a - (a // c) - (a // d) + (a // lcm_cd)
    c1 = b - (b // c) - (b // d) + (b // lcm_cd)
    return c1 - c0


if __name__ == '__main__':

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

    # print(gcd(c, d))
    # print(lcm(c, d))

    ans = solv(a, b, c, d)

    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 lcm(a, b):
    return a * b // gcd (a, b)

def divided_num(a,b,c):
    return b//c - a//c +1 if a%c==0 else b//c - a//c

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

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

bc = B // C
bd = B // D
be = B // int((C*D/gcd(C, D)))
BB = B - bc - bd + be

ac = (A-1) // C
ad = (A-1) // D
ae = (A-1) // int(C*D/gcd(C, D))
AA = (A-1) - ac - ad + ae

print(BB-AA)

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

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

def count(x):
    r = ran if a % x == 0 else ran - (x - a % x)
    return r // x if r % x == 0 else r // x + 1

count1 = count(c)
count2 = count(d)
e = lcm(c, d)
count3 = count(e)
print(ran - (count1 + count2 - count3))

Submission status: Accepted

Solution code:
import fractions
a,b,c,d = map(int,input().split())
a = a-1
cd = fractions.gcd(c,d)
cm = c*d//cd
blk = cm - cm//c - cm//d + 1
M = (b//cm)*blk + b%cm - (b%cm)//c - (b%cm)//d
m = (a//cm)*blk + a%cm - (a%cm)//c - (a%cm)//d
print(int(M-m))

Submission status: Accepted

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

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

num = 0

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

num = b-b//c-b//d+b//lcm-((a-1)-(a-1)//c-(a-1)//d+(a-1)//lcm)





print(str(num))

Submission status: Accepted

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

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


lcm =  (C*D)//fractions.gcd(C,D)
c=0
End = (B//C)+(B//D)-(B//(lcm))

Start = ((A-1)//C)+((A-1)//D)-((A-1)//(lcm))

print(int((B-A+1)-(End-Start)))

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)


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

s = b//c - (a-1)//c
t = b//d - (a-1)//d
u = b//v - (a-1)//v

print((b-a+1) - (s+t-u))

Submission status: Accepted

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

Submission status: Accepted

Solution code:

a=[]#入力画面の二文字を別々の文字に格納
a=input().split(" ")
A=int(a[0])
B=int(a[1])
C=int(a[2])
D=int(a[3])
#最大公約数を作る
def gcd(m,n):
    x=max(m,n)
    y=min(m,n)
    if x % y ==0:
        return y
    else:
        while x % y != 0:
            z=x%y
            x=y
            y=z
        else:
            return z
#最小公倍数を作る
def lcm(m,n):
    return int(m*n/gcd(m,n))

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

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)

cd = lcm(c,d)

mul_count = b//c + b//d - b//cd
a -= 1
mul_count -= (a//c + a//d -a//cd)
print(b - a - mul_count)

# print(cd)
# print(b//c ,b//d ,b//cd)
# print(a//c ,a//d ,a//cd)

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)

tmp   = b-a+1
tmp_c = (b//c-(a-1)//c)
tmp_d = (b//d-(a-1)//d)
tmp_e = (b//lcm(c,d)-(a-1)//lcm(c,d))

print(tmp - tmp_c - tmp_d + tmp_e)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

sa = B - A + 1

C2 = (B // C) - ((A - 1) // C)
D2 = (B // D) - ((A - 1) // D)

CD = lcm(C, D)

CD2 = (B // CD) - ((A - 1) // CD)

print(sa - (C2 + D2 - CD2))

Submission status: Accepted

Solution code:
import fractions
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
a,b,c,d = map(int,input().split())
ans = b-a+1
dim = 0
ca = a % c
if not ca:
    dim +=1
da = a % d
if not da:
    dim +=1
cd = lcm(c,d)
cda = a%cd
if not cda:
    ans+=1
dim+= (b-a+ca)//c
dim+= (b-a+da)//d
ans += (b-a+cda)//cd
print(ans-dim)

Submission status: Accepted

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

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())
divC = B//C - (A-1)//C
divD = B//D - (A-1)//D
CD = lcm(C,D)
divCD = B//CD - (A-1)//CD
divsum=divC+divD-divCD
answer=B-A+1-divsum

print(answer)

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline

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

def get_gcd(x, y):
    while True:
        if x > y:
            t = x % y
            a = y
            x = t
        else:
            t = y % x
            a = x
            y = t
        if t == 0:
            return a

c = B // C - (A - 1) // C
d = B // D - (A - 1) // D
lcm = C * D // get_gcd(C, D)
cd = B // lcm - (A - 1) // lcm


ans = B - (A - 1) - (c + d - cd)

print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd


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

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

div_by_c = b // c - (a-1) // c
div_by_d = b // d - (a-1) // d
div_both = b // cd - (a-1) // cd

dividable = div_by_c + div_by_d - div_both
non_dividable = (b - a + 1) - dividable

print(non_dividable)

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

n_c = B//C - (A-1)//C
n_d = B//D - (A-1)//D
lcm_cd = C*D // gcd(C,D)
n_cd = B//lcm_cd - (A-1)//lcm_cd

print(B-A+1 - (n_c+n_d-n_cd))

Submission status: Accepted

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

C_s = (A-A%C)//C
C_e = (B-B%C)//C

D_s = (A-A%D)//D
D_e = (B-B%D)//D

C_cnt = C_e - C_s
if A%C==0:
    C_cnt += 1
D_cnt = D_e - D_s
if A%D==0:
    D_cnt += 1

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

lcm_s = (A-A%lcm)//lcm
lcm_e = (B-B%lcm)//lcm

lcm_cnt = lcm_e - lcm_s
if A%lcm==0:
    lcm_cnt += 1

cnt = C_cnt + D_cnt - lcm_cnt
ans = B - A + 1 - cnt
print(ans)

Submission status: Accepted

Solution code:
import fractions
a,b,c,d = map(int,input().split())
a=a-1
x=(c*d)//fractions.gcd(c,d)
n=b//c-a//c
m=b//d-a//d
o=b//x-a//x
ans=b-a-n-m+o
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)

x = (b - (b//c)) + (b - (b//d)) - (b - (b//lcm(c, d)))
y = (a-1 - ((a-1)//c)) + (a-1 - ((a-1)//d)) - (a-1 - ((a-1)//lcm(c, d)))

print(x-y)

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)

# print(lcm(2,3))


a, b, c, d = map(int, input().split())
l = []
cnt_c = 0
cnt_d = 0
n = b+1-a
# print("n",n)


# print(a//c)
# print(a//d)
# print(b//c)
# print(b//d)

lcm = lcm(c, d)

x = b//c - (a-1)//c
y = b//d - (a-1)//d
z = b//lcm - (a-1)//lcm
# print(x)
# print(y)
print(n-x-y+z)




# for i in range(a, b+1):
#     if i%c == 0:
#         cnt_c += 1
#     if i%d == 0:
#         cnt_d += 1
    
    # if i%c == 0 and i%d != 0:
    #     l.append(i)
# print(cnt_c)
# print(cnt_d)

Submission status: Accepted

Solution code:
import fractions
A,B,C,D = list(map(int,input().split()))
dis = B - A + 1

CD = C*D//(fractions.gcd(C,D))
#print(CD)
perCfromA = (A-1)//C
perDfromA = (A-1)//D
perCDfromA = (A-1)//(CD)


perCfromB = B//C
perDfromB = B//D
perCDfromB = B//(CD)

#print(perCfromA,perDfromA,perCDfromA,perCfromB,perDfromB,perCDfromB)
warikire = (perCfromB + perDfromB -perCDfromB) - (perCfromA + perDfromA -perCDfromA)
warikirenai = dis -warikire
print(warikirenai)

Submission status: Accepted

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