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: Wrong Answer

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 calc2(n, m):
    return n-(math.floor(n / m))


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

    if b < c and b < d:
        print(b-a+1)
    elif b < c:
        print(calc2(b, d)-calc2(a-1, d))
    elif b < d:
        print(calc2(b, c) - calc2(a - 1, c))
    else:
        print(calc(b, c, d)-calc(a-1, c, d))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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 calc2(n, m):
    return n-(math.floor(n / m))


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

    if b < c and b < d:
        print(b-a)
    elif b < c:
        print(calc2(b, d)-calc2(a-1, d))
    elif b < d:
        print(calc2(b, c) - calc2(a - 1, c))
    else:
        print(calc(b, c, d)-calc(a-1, c, d))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

    if b < c and b < d:
        print(b-a)
    else:
        print(calc(b, c, d)-calc(a-1, c, d))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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 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: Wrong Answer

Solution code:
import math


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

    if b == a:
        print(1)
    else:
        print(b-a-(math.floor((b - a) / c) +
                   math.floor((b - a) / d) - math.floor((b - a) / (c*d/math.gcd(c, d)))))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import math


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

    print(b-a-(math.floor((b - a) / c) +
               math.floor((b - a) / d) - math.floor((b - a) / (c*d/math.gcd(c, d)))))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import math


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

    if b < c and b < d:
        print(b - a)
    else:
        print(b-a-(math.floor((b - a) / c) +
                   math.floor((b - a) / d) - math.floor((b - a) / (c*d/math.gcd(c, d)))))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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)
    return n-e

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

Submission status: Wrong Answer

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

print(b-a-(((b//c+b//d)-((a)//c+(a)//d))-(b//(c*d)-(a)//(c*d))))

Submission status: Wrong Answer

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 = math.floor((a-1)/c)
cb = math.floor(b/c)
da = math.floor((a-1)/d)
db = math.floor(b/d)
cda = math.floor((a-1)/lcm(c,d))
cdb = math.floor(b/lcm(c,d))
ans -= (cb+db-cdb) - (ca+da-cda)
print(ans)

Submission status: Wrong Answer

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

ans = b-a+1
#print(ans)

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

#C:number of integers between a and b that can be divided by c 
C = math.floor(b/c) - math.ceil(a/c) + 1
D = math.floor(b/d) - math.ceil(a/d) + 1
CD = math.floor(b/lcm(c,d)) - math.ceil(a/lcm(c,d)) + 1
ans -= (C + D - CD)
#print(C,D,CD)
print(ans)

Submission status: Wrong Answer

Solution code:
import math

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

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

def num(i):
  ans = int(b/i) - int(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: Wrong Answer

Solution code:
import math

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

lcm = 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(int(b-a+1-num))

Submission status: Wrong Answer

Solution code:
import math

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

lcm = 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: Wrong Answer

Solution code:
import math

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

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

def num(i):
  ans = int(b/i) - int(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: Wrong Answer

Solution code:
a,b,c,d=map(int,input().split())
#c,dの最小公倍数周期で回ってくる
import math
gcd = math.gcd(c,d)

lcf = int(c*d/gcd)
cyc_count = int(lcf/c)+ int(lcf/d) -1
#print(lcf)

cyc_num = (b-a)//lcf



count = b-a-cyc_num*lcf


cyc_num_before = a//lcf

res = a - cyc_num_before*lcf
#print(res)
ans = (res+count)//c +(res+count)//d
ans -= (res//c + res//d)


#print(count)
if b-a-(cyc_num*cyc_count+ans)+1 >0:
    print(b-a-(cyc_num*cyc_count+ans)+1)

Submission status: Wrong Answer

Solution code:
a,b,c,d=map(int,input().split())
#c,dの最小公倍数周期で回ってくる
import math
gcd = math.gcd(c,d)

lcf = int(c*d/gcd)
cyc_count = int(lcf/c)+ int(lcf/d) -1
#print(lcf)

cyc_num = (b-a)//lcf



count = b-a-cyc_num*lcf


cyc_num_before = a//lcf

res = a - cyc_num_before*lcf
#print(res)
ans = (res+count)//c +(res+count)//d
ans -= (res//c + res//d)


#print(count)
if b-a-(cyc_num*cyc_count+ans)+1 < 10*12:
    print(b-a-(cyc_num*cyc_count+ans)+1)
else:
    print(b-a-(cyc_num*cyc_count+ans)+2)

Submission status: Wrong Answer

Solution code:
a,b,c,d=map(int,input().split())
#c,dの最小公倍数周期で回ってくる
import math
gcd = math.gcd(c,d)

lcf = int(c*d/gcd)
cyc_count = int(lcf/c)+ int(lcf/d) -1
#print(lcf)

cyc_num = (b-a)//lcf



count = b-a-cyc_num*lcf


cyc_num_before = a//lcf

res = a - cyc_num_before*lcf
#print(res)
ans = (res+count)//c +(res+count)//d
ans -= (res//c + res//d)


#print(count)
if b-a-(cyc_num*cyc_count+ans)+1 < 10*8:
    print(b-a-(cyc_num*cyc_count+ans)+1)
else:
    print(b-a-(cyc_num*cyc_count+ans)+2)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

def calculation(x,c,d):
	x-=x//c
	x-=x//d
	x+=x//((c*d)//gcd(c,d))
	return x
	
print(calculation(b,c,d)-calculation(a,c,d))

Submission status: Wrong Answer

Solution code:
A,B,C,D = map(int, input().split())
E = 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: Wrong Answer

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: Wrong Answer

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(int(x-y))

Submission status: Wrong Answer

Solution code:
A,B,C,D = map(int,input().split())
ans = 0
for i in range(A,B+1):
    if i % (C*D)==0:
        print(ans)
        exit()
    else:
        ans+=1

Submission status: Wrong Answer

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, lcd)

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

Submission status: Wrong Answer

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

num_c = b//c - a//c
num_d = b//d - a//d

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys

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

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

Submission status: Wrong Answer

Solution code:
import sys

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

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

Submission status: Wrong Answer

Solution code:
A, B, C, D = list(map(int, input().split()))
nans = 0
import math
cs = A if A % C == 0 else math.ceil(A / C) * C
ds = A if A % D == 0 else math.ceil(A / D) * D
E = (C * D) // math.gcd(C, D)
print(E)
es = A if A % E == 0 else math.ceil(A / E) * E

ce = B // C * C
de = B // D * D
ee = B // E * E

nans = (ce - cs) // C + 1 + (de - ds) // D + 1 - ((ee - es) // E + 1)
print(B - A + 1 - nans)

Submission status: Wrong Answer

Solution code:
import sys

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

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

Submission status: Wrong Answer

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

lcm_CD = C * D / math.gcd(C, D)
ans = (B-A+1) - (((B-A+1)+((A-1)%C)) // C) - (((B-A+1)+((A-1)%D)) // D) + (((B-A+1)+((A-1)%lcm_CD)) // lcm_CD)

print(int(ans))

Submission status: Wrong Answer

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)
# 4-2+1, 3-1+1
ans = B-A+1 - tmp
print(ans)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
a,b,c,d = map(int, input().split())
n = b-a+1
x = c*d//math.gcd(c,d)
c_cnt =  b//c - (a-1)//c
d_cnt = b//d - (a-1)//d

print(n - c_cnt - d_cnt + b//x)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import fractions
A, B, C, D = map(int, input().split())
if B > C:
    n_C = int(B / C) - int((A-1) /C)
else:
    n_C = 0
if B > D:
    n_D = int(B / D) - int((A-1) /D)
else:
    n_D = 0
n = (C * D) // fractions.gcd(C, D)
if B > n:
    n_C_D = int(B / n) - int((A-1)/n)
else:
    n_C_D = 0
print(B-A-n_C-n_D+n_C_D+1)

Submission status: Wrong Answer

Solution code:
import fractions
A, B, C, D = map(int, input().split())
if B > C:
    n_C = int(B / C) - int((A-1) /C)
else:
    n_C = 0
if B > D:
    n_D = int(B / D) - int((A-1) /D)
else:
    n_D = 0
n = C * D // fractions.gcd(C, D)
if B > n:
    n_C_D = int(B / n) - int((A-1)/n)
else:
    n_C_D = 0
print(B-A-n_C-n_D+n_C_D+1)

Submission status: Wrong Answer

Solution code:
import math

a, b, c, d = map(int, input().split())
abNum = b - a + 1
cd = (c * d) // math.gcd(c, d)
cMin = math.ceil(a / c)
cMax = math.floor(b / c)
cNum = cMax - cMin + 1
dMin = math.ceil(a / d)
dMax = math.floor(b / d)
dNum = dMax - dMin + 1
cdMin = math.ceil(a / cd)
cdMax = math.floor(b / cd)
cdNum = cdMax - cdMin + 1

print(abNum - cNum - dNum + cdNum)

Submission status: Wrong Answer

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

e = lcm(c,d)
#print(e)
s1 = b//c + b//d - b//e
s2 = a//c + a//d - a//e
print((b-a)-(s1-s2-1))

Submission status: Wrong Answer

Solution code:
def binary_search_int(ok, ng, test, p):
    """
    :param ok: solve(x) = True を必ず満たす点
    :param ng: solve(x) = False を必ず満たす点
    """
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if test(mid, p):
            ok = mid
        else:
            ng = mid
    return ok

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

from math import gcd

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

def test1(x, p):
    return x*p<=B
b = [binary_search_int(1,10**18+1,test1, x) for x in [C, D, l]]

def test2(x, p):
    return x*p>=A

a = [binary_search_int(10**18, 0,test2, x) for x in [C, D, l]]

print((B-A+1)-(b[0]-a[0]+1)-(b[1]-a[1]+1)+(b[2]-a[2]+1))

Submission status: Wrong Answer

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

cnt = 0
numc = b//c - math.ceil(Decimal(a/c)) + 1
numd = b//d - math.ceil(Decimal(a/d)) + 1

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

cd = (c*d) // gcd(c, d)
numcd = b//cd - math.ceil(Decimal(a/cd)) + 1

cnt = b - a + 1 - numc - numd + numcd

print(cnt)

Submission status: Wrong Answer

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

cnt = 0
numc = math.floor(b/c) - math.ceil(a/c) + 1
numd = math.floor(b/d) - math.ceil(a/d) + 1

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

cd = (c*d) // gcd(c, d)
numcd = math.floor(b/cd) - math.ceil(a/cd) + 1

cnt = b - a + 1 - numc - numd + numcd

print(cnt)

Submission status: Wrong Answer

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

cnt = 0
numc = math.floor(b/c) - math.ceil(a/c) + 1
numd = math.floor(b/d) - math.ceil(a/d) + 1

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

cd = (c*d) // gcd(c, d)
numcd = math.floor(b/cd) - math.ceil(a/cd) + 1

cnt = b - a + 1 - numc - numd + numcd

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)
    
A, B, C, D = list(map(int,input().rstrip().split()))
lcm = C/gcd(C,D)*D
ans = B-A+1
ans -= math.floor(B/C)-math.ceil(A/C)+1
ans -= math.floor(B/D)-math.ceil(A/D)+1
ans += math.floor(B/lcm)-math.ceil(A/lcm)+1
print(ans)

Submission status: Wrong Answer

Solution code:
import math


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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math


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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math


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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# 問題：https://atcoder.jp/contests/abc126/tasks/abc131_c

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

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

def f(x, c, d):
    res = x
    res -= x // c 
    res -= x // d 
    res += x // lcm(c, d)
    return res

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

print(int(res))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# 問題：https://atcoder.jp/contests/abc126/tasks/abc131_c

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

a, b, c, d = map(int, input().strip().split())
a -= 1
lcm = (c * d / gcd(c, d))
num_c = b // c - a // c
num_d = b // d - a // d
num_c_and_d = b // lcm - a // lcm
num_all = b - a
res = num_all - num_c - num_d + num_c_and_d
print(int(res))

Submission status: Wrong Answer

Solution code:
a,b,c,d=map(int, input().split())
def modz(x,y,z):
  if x%z==0:
    if y%z==0:
      return (y-x)//z+1
    else:
      return (y-x)//z+1
  else:
    if b%c==0:
      return (y-x)//z+1
    else:
      return y//z-x//z
if a//c or a//d:
  if b//c or b//d:
    ans=2
  else:
    ans=1
else:
  if b//c or b//d:
    ans=1
  else:
    ans=0
print(b-a-(modz(a-1,b,c)+modz(a-1,b,d)-modz(a-1,b,c*d))+ans)

Submission status: Wrong Answer

Solution code:
a,b,c,d = map(int, input().split())
a1 = a-1
cd = c*d
l = []
for i in [c,d,cd]:
    x1 = a%i
    if x1 == 0:
        x = (b//i) - (a//i) + 1
        l.append(x)
    else:
        x = (b//i) - (a//i)
        l.append(x)
print(b-a1-l[0]-l[1]+l[2])

Submission status: Wrong Answer

Solution code:
a,b,c,d = map(int, input().split())
num = b-a+1
for i in range(a,b+1):
    if i%c == 0 and i%d == 0:
        num -= 1
print(num)

Submission status: Wrong Answer

Solution code:
import math

def getDivCount( a, b, x ):
    count=0
    if a==0 :
        count+=1
    count+=b//x-a//x
    return count

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

count = b-a+1
count -= getDivCount(a,b,c)
count -= getDivCount(a,b,d)
count += getDivCount(a,b,c*d//math.gcd(c,d))
print(count)

Submission status: Wrong Answer

Solution code:

def getDivCount( a, b, x ):
    count=0
    if a==0 :
        count+=1

    if a<=x :
        ai = x
    else:
        ai = (a//x)*x
        if ai<a :
            ai +=x
    if ai<=b :
        count+=1
        count+=(b-ai)//x
    return count

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

count = b-a+1
count -= getDivCount(a,b,c)
if c!=d :
    count -= getDivCount(a,b,d)
    count += getDivCount(a,b,c*d)
print(count)

Submission status: Wrong Answer

Solution code:
def getDivCount( a, b, x ):
    count=0
    if a==0 :
        count+=1

    if a<=x :
        ai = x
    else:
        ai = (a//x)*x
        if ai<a :
            ai +=x
    if ai<=b :
        count+=1
        count+=(b-ai)//x
    return count

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

count = b-a+1
count += getDivCount(a,b,c*d)
count -= getDivCount(a,b,c)
count -= getDivCount(a,b,d)
print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
l = list(map(int,input().split()))

l[0] -= 1
la = l[0] - l[0] // l[2] - l[0] // l[3] + l[0] // (l[2] * l[3])
lb = l[1] - l[1] // l[2] - l[1] // l[3] + l[1] // (l[2] * l[3])
print(lb - la)

Submission status: Wrong Answer

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

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

CD = lcm(C,D)

CD1 = (A//CD +1)*CD

kyoutuu = (B-CD1)//CD

C1 = (A//C + 1)*C
Cnum = (B - C1)//C

D1 =(A//D +1)*D
Dnum = (B - D1)//D

print(B-A-Cnum-Dnum+kyoutuu)

Submission status: Wrong Answer

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

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

ans = (b-(a-1)) - (b_c - a_c) - (b_d - a_d) + (b_cd - a_cd)

print(ans)

Submission status: Wrong Answer

Solution code:
import math

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

Submission status: Wrong Answer

Solution code:
import math

A,B,C,D = map(int, input().split())
a = B//C - math.ceil(A/C) + 1
b = B//D - math.ceil(A/D) + 1
lcm = C*D//math.gcd(C,D)
c = B//lcm - math.ceil(A/lcm) + 1
print(B-(A-1)-(a+b-c))

Submission status: Wrong Answer

Solution code:
import math

A,B,C,D = map(int, input().split())
a = B//C - A//C
b = B//D - A//D
lcm = C*D//math.gcd(C,D)
c = B//lcm - A//lcm
print(B-A-(a+b-c))

Submission status: Wrong Answer

Solution code:
import math

A,B,C,D = map(int, input().split())
a = B//C - A//C
b = B//D - A//D
c = B//math.gcd(C,D) - A//math.gcd(C,D)
print(a+b-c)

Submission status: Wrong Answer

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

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

c_num = f(a, b, c)
d_num = f(a, b, d)

ap = a
dp = d
mod = 1
while mod > 0:
    mod = ap % dp
    ap = dp
    dp = mod

cd_num =  f(a, b, (c * d) // ap)
print(b - a + 1 - c_num - d_num + cd_num)

Submission status: Wrong Answer

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

print(int(b-a+1-(yakusuu(b)-yakusuu(a-1))))

Submission status: Wrong Answer

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

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

ans = (b - a + 1) - (b_c - a_c) - (b_d - a_d) + (b_cd - a_cd)

print(ans)

Submission status: Wrong Answer

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

def div(z):
    p = (z//c) + (z//d) - (z//e)
    return p

x = a-1 - div(a-1)
y = b - div(b)
print(y-x)

Submission status: Wrong Answer

Solution code:
a,b,c,d = map(int,input().split())
a1 = a // c
a2 = b // c
b1 = b // c
b2 = b // d
e = c
while c % d != 0:
    c += e
a3 = a // c
b3 = b // c
print((b - b1 - b2 + b3) - (a - a1 - a2 + a3))

Submission status: Wrong Answer

Solution code:
import math
a,b,c,d = map(int, input().split())
e = c*d//math.gcd(c,d)
# CでもDでも割り切れない -> Cで割り切れない＋Dで割り切れない - CとDで割り切れない
xa = math.ceil(a/c)-1
xb = b//c
ya = math.ceil(a/d)-1
yb = b//d
za = math.ceil(a/e)-1
zb = b//e
print((b-a+1)-(xb-xa)-(yb-ya)+(zb-za))

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import numpy as np
import math
sys.setrecursionlimit(10**6)
def S(): return sys.stdin.readline().rstrip()
def SL(): return map(str,sys.stdin.readline().rstrip().split())
def I(): return int(sys.stdin.readline().rstrip())
def IL(): return map(int,sys.stdin.readline().rstrip().split())

if __name__=='__main__':
    a,b,c,d = IL()
    length = b-a
    mc = length//c
    md = length//d
    lcm = (c*d)//math.gcd(c,d)
    mcd = length//lcm
    print(length-mc-md+mcd)
    exit()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import numpy as np
import math
sys.setrecursionlimit(10**6)
def S(): return sys.stdin.readline().rstrip()
def SL(): return map(str,sys.stdin.readline().rstrip().split())
def I(): return int(sys.stdin.readline().rstrip())
def IL(): return map(int,sys.stdin.readline().rstrip().split())

if __name__=='__main__':
    a,b,c,d = IL()
    length = b-a
    mc = length//c
    md = length//d
    lcm = c*d//math.gcd(c,d)
    mcd = length//lcm
    print(length-mc-md+mcd)
    exit()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

x = b - b//c - b//d + b//(c*d/gcd(c, d))
y = (a-1) - (a-1)//c - (a-1)//d + (a-1)//(c*d/gcd(c, d))
print(int(x-y))

Submission status: Wrong Answer

Solution code:
from math import gcd

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

Submission status: Wrong Answer

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

all = B - A + 1
Cmod = B//C - (A-1)//C
Dmod = B//D - (A-1)//D
CDmod = B//(C*D) - (A-1)//(C*D)

print(all - Cmod - Dmod + CDmod)

Submission status: Wrong Answer

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

if C == 1 or D == 1:
    print(0)
    exit()

def check(A, B, x):
    sp = A if A%x == 0 else A + (x - A%x)
    ep = B if B%x == 0 else B - (x - B%x)
    if sp > B or ep < A:
        return 0
    return (ep - sp)//x + 1

print((B - A + 1) - (check(A, B, C) + check(A, B, D) - check(A, B, C*D//gcd(C, D))) )

Submission status: Wrong Answer

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

if C == 1 or D == 1:
    print(0)
    exit()

def check(A, B, x):
    sp = A if A%x == 0 else A + (x - A%x)
    ep = B if B%x == 0 else B - (x - B%x)
    if sp > B or ep < A:
        return 0
    return (ep - sp)//x + 1

print((B - A + 1) - (check(A, B, C) + check(A, B, D) - check(A, B, C*D)) )

Submission status: Wrong Answer

Solution code:
import math

A, B, C, D = map(int, input().split())
GCD_CD = math.gcd(C, D)
LCM_CD = C * D / GCD_CD

C_mltp = math.floor(B / C) - math.ceil(A / C) + 1 if math.floor(B / C) != math.ceil(A / C) else 0
D_mltp = math.floor(B / D) - math.ceil(A / D) + 1 if math.floor(B / D) != math.ceil(A / D) else 0
CD_mltp = math.floor(B / LCM_CD) - math.ceil(A / LCM_CD) + 1 if math.floor(B / LCM_CD) != math.ceil(A / LCM_CD) else 0

C_or_D_mltp = C_mltp + D_mltp - CD_mltp

print(B - A + 1 - C_or_D_mltp)

Submission status: Wrong Answer

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


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


c_low = math.ceil(A / C)
c_up = B // C
d_low = math.ceil(A / D)
d_up = B // D
lcm = C * D // gcd(C, D)
lcm_low = math.ceil(A / lcm)
lcm_up = B // lcm

c_cnt = max(0, c_up - c_low)
if c_up >= c_low and c_up > 0 and c_low > 0:
    c_cnt += 1

d_cnt = max(0, d_up - d_low)
if d_up >= d_low and d_up > 0 and d_low > 0:
    d_cnt += 1

lcm_cnt = max(0, lcm_up - lcm_low)
if lcm_up >= lcm_low and lcm_up > 0 and lcm_low > 0:
    lcm_cnt += 1

print((B - A + 1) - c_cnt - d_cnt + lcm_cnt)

Submission status: Wrong Answer

Solution code:
import math

A, B, C, D = map(int, input().split())
GCD_CD = math.gcd(C, D)
LCM_CD = C * D / GCD_CD

C_mltp = math.floor(B / C) - math.ceil(A / C) + 1
D_mltp = math.floor(B / D) - math.ceil(A / D) + 1
CD_mltp = math.floor(B / LCM_CD) - math.ceil(A / LCM_CD) + 1

C_or_D_mltp = C_mltp + D_mltp - CD_mltp

print(B - A + 1 - C_or_D_mltp)

Submission status: Wrong Answer

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

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

def gcd(C,D):
    a=min([C,D])
    b=max([C,D])
    tmp=a
    a=b%a
    b=tmp
    if a==0:
        return tmp
    else:
        return gcd(a,b)

CD=C*D//gcd(C,D)
if A%CD==0:
    cd=B//CD-A//B+1
else:
    cd=B//CD-A//CD

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    import math
    a,b,c,d = map(int,input().split())
    al = b-a+1
    cdiv = b//c-math.ceil(a/c)+1
    ddiv = b//d-math.ceil(a/d)+1
    gcd = c*d//math.gcd(c,d)
    cddiv = b//gcd-math.ceil(a/gcd)+1
    print(al-cdiv-ddiv+cddiv)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import fractions
a,b,c,d = map(int,input().split())
l = (c*d)//fractions.gcd(c,d)
if fractions.gcd(c,d) == 1:
	l = 10**18+1
	num =((a//c)+(a//d))-(a//l)- (a//(c*d))
	num2 =((b//c)+(b//d))-(b//l)-(b//(c*d))
	print(b-a-(num2-num)+1)
else:
	num =((a//c)+(a//d))-(a//l)- (a//(c*d))
	num2 =((b//c)+(b//d))-(b//l)-(b//(c*d))-1
	print(b-a-(num2-num))