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

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

if a%c == 0 and a%d != 0:
    ans -=1
elif a%c != 0 and a%d == 0:
    ans -=1
elif a%c == 0 and a%d == 0:
    ans -=1
print(ans)

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 cal(num, a, b):
    x = num // a
    y = num // b
    z = num // (lcm(a, b))
    return num - (x + y - z)

num_a = cal(b, c, d)
num_b = cal(a-1, c, d)

print(num_a - num_b)

Submission status: Accepted

Solution code:
from fractions import gcd

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


def nd(n, c, d):
    return n - n // c - n // d + n // lcm


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

Submission status: Accepted

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

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

def notDiv(a, c, d):
    return a - a//c - a//d + a//lcm(c, d)

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

Submission status: Accepted

Solution code:
# 方針はb以下の条件にあったものからa - 1以下の条件にあったものを引く
# 最小公倍数を使って引きすぎたものを置換

# greatest common divisor
# gcd はユークリッドの互除法をつかって再帰の形で求められる
def gcd(x,y):
  if y == 0: return x
  return gcd(y,x%y)

# least common multiple
# // を使った除算で小数点を省ける
def lcm(e,f):
  return e * f // gcd(e,f)
a,b,c,d = map(int,input().split())

all = b - (b // c) - (b // d) + (b // lcm(c,d))
to_a = (a - 1) - ((a - 1) // c) - ((a - 1) // d) + ((a - 1) // lcm(c,d))
print(str(all - to_a))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 2019-11-14 20:54:46(JST)
import sys
# import collections
import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 


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 main():
    a, b, c, d = [int(x) for x in sys.stdin.readline().split()]
    divisors = 0
    divisors += b // c - (a - 1) // c
    divisors += b // d - (a - 1) // d
    l = lcm(c, d)
    divisors -= b // l - (a - 1) // l
    ans = b - (a - 1) - divisors
    print(ans)



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# 2019-11-14 20:54:46(JST)
import sys
# import collections
import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 


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 main():
    a, b, c, d = [int(x) for x in sys.stdin.readline().split()]
    res1 = b - (b // c + b // d - b // lcm(c, d))
    res2 = (a - 1) - ((a - 1) // c + (a - 1) // d - (a - 1) // lcm(c, d))
    ans = res1 - res2
    print(ans) 

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def xgcd(a, b):
    x0, y0, x1, y1 = 1, 0, 0, 1
    while b != 0:
        q, a, b = a // b, b, a % b
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return a


a, b, c, d = map(int, input().split())
t0 = b//c - a//c + (a%c==0)
t1 = b//d - a//d + (a%d==0)
z = c*d//xgcd(c,d)
t2 = b//z - a//z + (a%z==0)

print(b-a+1-t0-t1+t2)

Submission status: Accepted

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

c_cnt = b//c - (a-1)//c
d_cnt = b//d - (a-1)//d
cd = (c * d) // fractions.gcd(c, d)
cd_cnt= b//cd - (a-1)//cd

ans = b-(a-1) - (c_cnt + d_cnt - cd_cnt)
print(ans)

Submission status: Accepted

Solution code:
import fractions
a,b,c,d=map(int,input().split())
CD=fractions.gcd(c,d)
cd=(c*d)//CD
width=b-a
cnt=0
if width%c==0:
  if a%c==0:
    cnt+=(width//c)+1
  else:
    cnt+=(width//c)
else:
  if c-(a%c)+b%c>=c:
    if a%c==0 or b%c==0:
      cnt+=(width//c)+1
    else:
      cnt+=(width//c)
  else:
    cnt+=(width//c)+1

if width%d==0:
  if a%d==0:
    cnt+=(width//d)+1
  else:
    cnt+=(width//d)
else:
  if d-(a%d)+b%d>=d:
    if a%d==0 or b%d==0:
      cnt+=(width//d)+1
    else:
      cnt+=(width//d)
  else:
    cnt+=(width//d)+1

if width%cd==0:
  if a%cd==0:
    cnt-=(width//cd)+1
  else:
    cnt-=(width//cd)
else:
  if cd-(a%cd)+b%cd>=cd:
    if a%cd==0 or b%cd==0:
      cnt-=(width//cd)+1
    else:
      cnt-=(width//cd)
  else:
    cnt-=(width//cd)+1

print(b-a+1-cnt)

Submission status: Accepted

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

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

def notDiv(a, c, d):
    return a - a//c - a//d + a//lcm(c, d)

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

Submission status: Accepted

Solution code:
import fractions
A,B,C,D=map(int,input().split())
A=A-1
AS=A//(C*D//fractions.gcd(C,D))
BS=B//(C*D//fractions.gcd(C,D))
AC=A//C
AD=A//D
BC=B//C
BD=B//D
print(B-A-(BC+BD-BS)+(AC+AD-AS))

Submission status: Accepted

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

Submission status: Accepted

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

def gcd(x,y):
  if x==0:
    return y
  else:
    return gcd(y%x,x)
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 sys
def input():
    return sys.stdin.readline().rstrip()

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

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

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

    dividable = (B//C + B//D - B//d) - (A-1)//C - (A-1)//D + (A-1)//d
    print(B-A+1 - dividable)



# %%
if __name__ == '__main__':
    main()

# %%
# from atcoder_test import doTest
# doTest("abc131","c",main)

Submission status: Accepted

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


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

b_c_cnt = b // c
b_d_cnt = b // d
b_lcm_cnt = b // lcm

b_cnt = b - (b_c_cnt + b_d_cnt) + b_lcm_cnt

a_c_cnt = a // c
a_d_cnt = a // d
a_lcm_cnt = a // lcm

a_cnt = a - (a_c_cnt + a_d_cnt) + a_lcm_cnt

if (a % c != 0 and a % d != 0):
    print(b_cnt - a_cnt + 1)
else:
    print(b_cnt - a_cnt)

Submission status: Accepted

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

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

    A_num = (A-1)//C + (A-1)//D - (A-1)//lcm(C, D)
    B_num = B//C + B//D - B//lcm(C, D)
    return (B-(A-1))-(B_num - A_num)


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import fractions

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

cxd = (c * d) // fractions.gcd(c, d)
sa = b - a
ans = (b - b//c - b//d + b//cxd) - ((a-1) - (a-1)//c - (a-1)//d + (a-1)//cxd)

print(ans)

Submission status: Accepted

Solution code:
import fractions
a,b,c,d = map(int,input().split())
N = b-a+1
s = fractions.gcd(c,d)
t = c*d // s
lcm_div = b//t - (a-1)//t
divc = b//c - (a-1)//c
divd = b//d - (a-1)//d
if c%d==0 or d%c==0:
    w = min(c,d)
    print(N-(b//w-(a-1)//w))
else:
    print(N-(divc+divd-lcm_div))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline# 最小公倍数
 
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 = map(int, input().split())
e = lcm(c, d)
count_c = b // c - (a - 1) // c
count_d = b // d - (a - 1) // d
count_e = b // e - (a - 1) // e
print(b - a + 1 - (count_c + count_d - count_e))

Submission status: Accepted

Solution code:
import sys
import math

input = sys.stdin.readline# 最小公倍数
 
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 = map(int, input().split())
e = lcm(c, d)
#print("e: {}".format(e))
#print("c: {} - {}".format(b // c, (a - 1) // c))
#print("d: {} - {}".format(b // d, (a - 1) // d))
#print("e: {} - {}".format(b // e, (a - 1) // e))
count_c = b // c - (a - 1) // c
count_d = b // d - (a - 1) // d
count_e = b // e - (a - 1) // e
# b - a の中から d の倍数の個数を取得
#print("count_c: {}".format(count_c))
#print("count_d: {}".format(count_d))
#print("count_e: {}".format(count_e))
print(b - a + 1 - (count_c + count_d - count_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())
e = lcm(c, d)
#print("e: {}".format(e))
#print("c: {} - {}".format(b // c, (a - 1) // c))
#print("d: {} - {}".format(b // d, (a - 1) // d))
#print("e: {} - {}".format(b // e, (a - 1) // e))
count_c = b // c - (a - 1) // c
count_d = b // d - (a - 1) // d
count_e = b // e - (a - 1) // e
# b - a の中から d の倍数の個数を取得
#print("count_c: {}".format(count_c))
#print("count_d: {}".format(count_d))
#print("count_e: {}".format(count_e))
print(b - a + 1 - (count_c + count_d - count_e))

Submission status: Accepted

Solution code:
import sys
from fractions import gcd
input = sys.stdin.readline

MOD = 10 ** 9 + 7


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


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

    c_count = B // C - (A - 1) // C
    d_count = B // D - (A - 1) // D
    e_count = B // E - (A - 1) // E
    print((B - A + 1) - (c_count + d_count - e_count))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import fractions
# 最小公倍数
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)    
a, b, c, d = map(int, input().split())
e = lcm(c, d)
#print("e: {}".format(e))
#print("c: {} - {}".format(b // c, (a - 1) // c))
#print("d: {} - {}".format(b // d, (a - 1) // d))
#print("e: {} - {}".format(b // e, (a - 1) // e))
count_c = b // c - (a - 1) // c
count_d = b // d - (a - 1) // d
count_e = b // e - (a - 1) // e
# b - a の中から d の倍数の個数を取得
#print("count_c: {}".format(count_c))
#print("count_d: {}".format(count_d))
#print("count_e: {}".format(count_e))
print(b - a + 1 - (count_c + count_d - count_e))

Submission status: Accepted

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

c_num = b // c - (a-1) // c
d_num = b // d - (a-1) // d


def lcm(x, y):
    return (x * y) // gcd(x, y)
    
e = lcm(c,d)

e_num = b // e - (a-1) // e

print((b-a+1)-(c_num+d_num-e_num))

Submission status: Accepted

Solution code:
import fractions as fc

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

tmp = (c * d) // fc.gcd(c, d)

all_num = b//c + b//d - b//tmp
a_num = (a-1)//c + (a-1)//d - (a-1)//tmp

ans = (b-a+1) - (all_num - a_num)
print(ans)

Submission status: Accepted

Solution code:
import unittest


class TestC(unittest.TestCase):
    def test_1(self):
        self.assertEqual(think(4, 9, 2, 3), 2)

    def test_2(self):
        self.assertEqual(think(10, 40, 6, 8), 23)

    def test_3(self):
        self.assertEqual(think(314159265358979323, 846264338327950288, 419716939, 937510582), 532105071133627368)

    def test_4(self):
        self.assertEqual(count_multiple_between(4, 9, 2), 3)

    def test_5(self):
        self.assertEqual(count_multiple_between(4, 9, 3), 2)

    def test_6(self):
        self.assertEqual(count_multiple_between(4, 9, 6), 1)

    def test_7(self):
        self.assertEqual(count_multiple_between(10, 40, 6), 5)

    def test_8(self):
        self.assertEqual(count_multiple_between(10, 40, 8), 4)

    def test_9(self):
        self.assertEqual(count_multiple_between(10, 40, 24), 1)


def solve():
    a, b, c, d = read()
    result = think(a, b, c, d)
    write(result)


def read():
    return read_int(4)


def read_int(n):
    return list(map(lambda x: int(x), read_line().split(' ')))[:n]


def read_float(n):
    return list(map(lambda x: float(x), read_line().split(' ')))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(a, b, c, d):
    if c > d:
        return think(a, b, d, c)

    if c == 1:
        return 0

    if d % c == 0:
        num_of_multiple = count_multiple_between(a, b, c)
        return b - a + 1 - num_of_multiple

    lcm = least_common_multiple(c, d)

    num_of_multiple_c = count_multiple_between(a, b, c)
    num_of_multiple_d = count_multiple_between(a, b, d)
    num_of_multiple_lcm = count_multiple_between(a, b, lcm)

    return b - a + 1 - (num_of_multiple_c + num_of_multiple_d - num_of_multiple_lcm)


def count_multiple_between(a, b, c):
    minimum_multiple = a if a % c == 0 else a + (c - (a % c))
    maximum_mulptile = b if b % c == 0 else b - (b % c)

    if maximum_mulptile < minimum_multiple:
        return 0

    return (maximum_mulptile - minimum_multiple) // c + 1


def least_common_multiple(a, b):
    return (a * b) // greatest_common_divisor(a, b)


def greatest_common_divisor(a, b):
    if a < b:
        return greatest_common_divisor(b, a)

    if a % b == 0:
        return b
    return greatest_common_divisor(b, a % b)


def write(result):
    print(result)


if __name__ == '__main__':
    # unittest.main()
    solve()

Submission status: Accepted

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

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

can_c_under_a = (a-1)//c
can_d_under_a = (a-1)//d
can_cd_under_a = (a-1)//lcm(c, d)
cant_under_a = (a-1) + can_cd_under_a - can_c_under_a - can_d_under_a

can_c_all = b//c
can_d_all = b//d
can_cd_all = b//lcm(c, d)
cant_all = b + can_cd_all - can_c_all - can_d_all

print(cant_all - cant_under_a)

Submission status: Accepted

Solution code:
from fractions import gcd
def f(c):
  if a%c!=0:
    if B>=(c-a%c)+a:
      l=(c-a%c)+a
    else:
      l=B+c
  else:
    l=a
  return(1+(B-l)//c)
  
  

a,B,c,d=map(int,input().split())
print(B-a+1-(f(c)+f(d)-f(c*d//gcd(c,d))))

Submission status: Accepted

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

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

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

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

def l(a, x):
    if a // x == 0:
        return 1
    if a % x != 0:
        t = a // x + 1
        return t
    t = a // x
    return t

cl = l(A, C)
dl = l(A, D)
cdl = l(A, CD)
cr = B // C
dr = B // D
cdr = B // (CD)

ans = B - A + 1 - (cr - cl + 1) - (dr - dl + 1) + (cdr - cdl + 1)
        
print(ans)

Submission status: Accepted

Solution code:
a,b,c,d = map(int,input().split())
from fractions import gcd
lcm = c * d // gcd(c, d)
def num_of_complements(x,c,d,lcm):
    num = x + x//lcm - x//c - x//d
    return num

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

Submission status: Accepted

Solution code:
a,b,c,d = map(int,input().split())
a -= 1
def gcd(A, B):
    while B:
        A, B = B, A % B
    return A
def lcm(A, B):
    return A * B // gcd (A, B)

ans = 0
tmp0 = b // c
tmp1 = b // d
tmp2 = b // lcm(c,d)
Tmp0 = tmp1 +tmp0 -tmp2

tmp3 = a // c
tmp4 = a // d
tmp5 = a // lcm(c,d)
Tmp1 = tmp3 + tmp4 - tmp5

ans = b-a - (Tmp0 - Tmp1)

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    a,b,c,d=map(int,input().split())
    from fractions import gcd
    L=c*d//gcd(c,d)
    M=b-(b//c)-(b//d)+(b//L)
    m=a-1-((a-1)//c)-((a-1)//d)+((a-1)//L)
    print(M-m)
resolve()

Submission status: Accepted

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

first_c = ( A - 1) // C
first_d = ( A - 1) // D
first_cd = ( A - 1) // lcm 
first = ( A - 1 ) - ( first_c + first_d - first_cd )

end_c = B // C
end_d = B // D
end_cd = B // lcm 
end_ = B - ( end_c + end_d - end_cd )

print(end_ - first)

Submission status: Accepted

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

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

lcmv = lcm(C, D)
bc = B // C
bd = B // D
bl = B // lcmv
ac = (A-1) // C
ad = (A-1) // D
al = (A-1) // lcmv
print(B - A + 1 - ((bc + bd - bl) - (ac + ad - al)))

Submission status: Accepted

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

from fractions import gcd

def div(x):
    if(a%x == 0):
        return (b//x-a//x)*(x-1)+(b%x)
    else:
        return (b//x-a//x)*(x-1)+(b%x)-(a%x-1)

ans = div(c)+div(d)-div(int(c*d//gcd(c,d)))
print(ans)

Submission status: Accepted

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

large = np.max([C, D])
small = np.min([C, D])

g = large
while small != 0:
    large, small = small, large % small

g = large

l = C * D // g

num_B_1 = B // C
num_B_2 = B // D
num_B_3 = B // l
num_B = B - (num_B_1 + num_B_2 - num_B_3)

num_A_1 = (A - 1) // C
num_A_2 = (A - 1) // D
num_A_3 = (A - 1) // l
num_A = (A - 1) - (num_A_1 + num_A_2 - num_A_3)

print(num_B - num_A)

Submission status: Accepted

Solution code:
import numpy as np
import math
A, B, C, D = [int(i) for i in input().split()]
 
large = np.max([C, D])
small = np.min([C, D])
#print(large)
#print(small)
#quo = large // small
#rem = large % small
g = large
while small != 0:
    large, small = small, large % small
#    print(large)
#    print(small)
 
g = large
#print(g)
 
l = C * D // g
 
num_B_1 = B // C
num_B_2 = B // D
num_B_3 = B // l
num_B = B - (num_B_1 + num_B_2 - num_B_3)

num_A_1 = (A - 1) // C
num_A_2 = (A - 1) // D
num_A_3 = (A - 1) // l
num_A = (A - 1) - (num_A_1 + num_A_2 - num_A_3)
 
print(num_B - num_A)

Submission status: Accepted

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

large = np.max([C, D])
small = np.min([C, D])

g = large
while small != 0:
    large, small = small, large % small

g = large

l = C * D // g

num_B_1 = B // C
num_B_2 = B // D
num_B_3 = B // l
num_B = B - (num_B_1 + num_B_2 - num_B_3)

num_A_1 = (A - 1) // C
num_A_2 = (A - 1) // D
num_A_3 = (A - 1) // l
num_A = (A - 1) - (num_A_1 + num_A_2 - num_A_3)

print(num_B - num_A)

Submission status: Accepted

Solution code:
import numpy as np
import math
A, B, C, D = [int(i) for i in input().split()]
 
large = np.max([C, D])
small = np.min([C, D])
 
while small != 0:
    large, small = small, large % small
gcd = large

large = np.max([C, D])
small = np.min([C, D])

lcm = C * D // gcd
 
num_B_1 = B // C
num_B_2 = B // D
num_B_3 = B // lcm
 
num_A_1 = (A-1) // C
num_A_2 = (A-1) // D
num_A_3 = (A-1) // lcm
 
numB = num_B_1 + num_B_2 - num_B_3
numA = num_A_1 + num_A_2 - num_A_3
 
print((B-A+1) - (numB - numA))

Submission status: Accepted

Solution code:
import numpy as np
import math
A, B, C, D = [int(i) for i in input().split()]
 
large = np.max([C, D])
small = np.min([C, D])
 
#def gcd(a, b):
#    while b:
#        a, b = b, a % b
#    return a
while small != 0:
    large, small = small, large % small
gcd = large

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

large = np.max([C, D])
small = np.min([C, D])
 
num_B_1 = B // C
num_B_2 = B // D
num_B_3 = B // lcm(large, small, gcd)
 
num_A_1 = (A-1) // C
num_A_2 = (A-1) // D
num_A_3 = (A-1) // lcm(large, small, gcd)
 
numB = num_B_1 + num_B_2 - num_B_3
numA = num_A_1 + num_A_2 - num_A_3
 
print((B-A+1) - (numB - numA))

Submission status: Accepted

Solution code:
import numpy as np
import math
A, B, C, D = [int(i) for i in input().split()]
 
large = np.max([C, D])
small = np.min([C, D])
 
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
 
def lcm(a, b):
    return a * b // gcd (a, b)
 
num_B_1 = B // C
num_B_2 = B // D
num_B_3 = B // lcm(large, small)
 
num_A_1 = (A-1) // C
num_A_2 = (A-1) // D
num_A_3 = (A-1) // lcm(large, small)
 
numB = num_B_1 + num_B_2 - num_B_3
numA = num_A_1 + num_A_2 - num_A_3
 
print((B-A+1) - (numB - numA))

Submission status: Accepted

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

large = np.max([C, D])
small = np.min([C, D])

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

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

num_B_1 = B // C
num_B_2 = B // D
num_B_3 = B // lcm(large, small)

num_A_1 = (A-1) // C
num_A_2 = (A-1) // D
num_A_3 = (A-1) // lcm(large, small)

numB = num_B_1 + num_B_2 - num_B_3
numA = num_A_1 + num_A_2 - num_A_3

print((B-A+1) - (numB - numA))

Submission status: Accepted

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

def multiple(X, Y, Z):
    # 天井関数math.ceilが誤差でおかしくなってるから組み込み関数を使う
    if X % Z != 0:
        min_z = X // Z + 1
    else:
        min_z = X / Z
    max_z = Y // Z
    num_z = max_z - min_z + 1
    return int(num_z)

def gcd(X, Y):
    while Y!= 0:
        X, Y = Y, X % Y
    return X

if C == 1 or D == 1:
    print(0)
else:
    num_c = multiple(A, B, C)
    num_d = multiple(A, B, D)
    lcm = int((C * D) / gcd(C, D)) 
    num_lcm = multiple(A, B, lcm)
    print(B - A + 1 - num_c - num_d + num_lcm)

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:
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())
c = B // C - (A - 1) // C
d = B // D - (A - 1) // D
c_d = B // lcm(C, D) - (A - 1) // lcm(C, D)
print(B - A + 1 - c - d + c_d)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

lcm_CD = lcm(C, D)

div_AB_C = (B // C) - ((A - 1) // C)
div_AB_D = (B // D) - ((A - 1) // D)
div_AB_CD = (B // lcm_CD) - ((A - 1) // lcm_CD)
print(B - A + 1 - (div_AB_C + div_AB_D - div_AB_CD))

Submission status: Accepted

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


def p_c():
    A, B, C, D = map(int, input().split())
    A -= 1
    gcd_value = gcd(max(C, D), min(C, D))
    lcm_value = C * D // gcd_value
    print(B - A - B // C + A // C - B // D + A // D + B // lcm_value - A // lcm_value)
    
p_c()

Submission status: Accepted

Solution code:
def gcd(x,y):
  if(y==0):
  	return x
  if(x<y):
    x,y=y,x
  return gcd(y,x % y)
def lllen(a,b,c):
  at=a//c
  at=at*c
  b=b//c *c
  ansc=0
  while(at<a):
    at+=c
  ansc=b-at
  if ansc<0 :
  	ansc=0
  else:
  	ansc=ansc//c + 1
  return ansc

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

ansc=lllen(a,b,c)
ansd=lllen(a,b,d)
ansl=lllen(a,b,(c*d)//gcd(c,d))
#print(ansc,ansl,ansd)
print(b-a+1 -ansc -ansd + ansl)

Submission status: Accepted

Solution code:
import fractions

tmp = list(map(int, input().split()))
a = tmp[0]
b = tmp[1]
c = tmp[2]
d = tmp[3]

divc = b // c - (a-1) // c
divd = b // d - (a-1) // d

lcmcd = c * d // fractions.gcd(c,d)
divcd = b // lcmcd - (a-1) // lcmcd

print(b - a + 1 - divc - divd + divcd)

Submission status: Accepted

Solution code:
def gcd(a, b):
  if a%b == 0:
    return b
  else:
    return gcd(b, a%b)
a, b, c, d = map(int, input().split())
n = b - a + 1
mod_c = (n + a%c - 1)//c + 1 if a%c == 0 else (n + a%c - 1)//c
mod_d = (n + a%d - 1)//d + 1 if a%d == 0 else (n + a%d - 1)//d
mod_cd = (n + a%(int(c*d/gcd(c, d))) - 1)//(int(c*d/gcd(c,d))) + 1 if a%(c*d/gcd(c,d)) == 0 else (n + a%(int(c*d/gcd(c,d))) - 1)//(int(c*d/gcd(c,d)))
ans = n - mod_c - mod_d + mod_cd
print(int(ans))

Submission status: Accepted

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

cnt_c = b // c - (a-1) // c
cnt_d = b // d - (a-1) // d
lcm = c * d // fractions.gcd(c, d)
cnt_cd = b // lcm - (a-1) // lcm

mul = cnt_c + cnt_d - cnt_cd

print(b - a + 1 - mul)

Submission status: Accepted

Solution code:
from fractions import gcd

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

g = gcd(C, D)
E = C * D // g

num1 = B // C - (A - 1) // C
num2 = B // D - (A - 1) // D
num3 = B // E - (A - 1) // E
print(B - A + 1 - num1 - num2 + num3)

Submission status: Accepted

Solution code:
def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    return gcd(b, a % b)
def resolve():
    a, b, c, d = list(map(int, input().split()))
    lcm = int(c * d / gcd(c, d))
    c_b = b - (b//c + b//d - b//(lcm))
    c_a = (a-1) - ((a-1)//c + (a-1)//d - (a-1)//(lcm))
    print(c_b - c_a)
    
resolve()

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

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

ub = B - B // C - B // D + B // lcm(C, D)

A -= 1
ua = A - A // C - A // D + A // lcm(C, D)

print(ub - ua)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import fractions
A,B,C,D=map(int,input().split())
A-=1
f=fractions.gcd(C,D)
f2=C*D//f
ans = B-A-(B//C-A//C+B//D-A//D-B//f2+A//f2)
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)

C_ok = (B // C) - ((A-1) // C) 
D_ok = (B // D) - ((A-1) // D)
C_D_ok = (B // lcm(C, D)) - ((A-1) // lcm(C, D))
print((B-A+1)-C_ok-D_ok+C_D_ok)

Submission status: Accepted

Solution code:
from fractions import gcd

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

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

div_A_C = (A-1)//C
div_A_D = (A-1)//D

div_B_C = B//C
div_B_D = B//D

CD = lcm(C, D)

div_A_CD = (A-1)//CD
div_B_CD = B // CD
C_or_D = (div_B_C+div_B_D-div_B_CD)-(div_A_C+div_A_D-div_A_CD) 
print(B-A+1-C_or_D)

Submission status: Accepted

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

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

Submission status: Accepted

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

x1=(b//z)
x2=(b//c)
x3=(b//d)
x=x2+x3-x1
y1=(a-1)//z
y2=(a-1)//c
y3=(a-1)//d
y=y2+y3-y1

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

Submission status: Accepted

Solution code:
from fractions import gcd
a, b, c, d = [int(i) for i in input().split()]
lcm = c * d // 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:
from fractions import gcd
def lcm(a, b):
	return a * b // gcd (a, b)
A, B, C, D = map(int,input().split())
l = lcm(C, D)
ans = B - (B // C + B // D - B // l)
ans -= (A-1) - ((A-1) // C + (A-1) // D - (A-1) // l)
print(ans)

Submission status: Accepted

Solution code:
import fractions

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


def main():
    a,b,c,d = map(int,input().split())
    c_mul_count = b//c - (a-1)//c
    d_mul_count = b//d - (a-1)//d
    cd_lcm = lcm(c, d)
    lcm_mul_count = b//cd_lcm - (a-1)//cd_lcm
    s = b-a+1
    print(s-c_mul_count-d_mul_count+lcm_mul_count)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import fractions
a,b,c,d=map(int,input().split())
count=0
e=(c*d)//fractions.gcd(c,d)
D=[c,d,e]
f=[]

for i in D:
  f.append((b//i)-((a-1)//i))
print(b-a+1-f[0]-f[1]+f[2])

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
num_B=B-B//C-B//D+B//CD
num_A=A-A//C-A//D+A//CD
print(int(num_B-num_A))

Submission status: Accepted

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

import fractions

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

Submission status: Accepted

Solution code:
A,B,C,D = map(int,input().split())
def gcd(x,y):
  if x<y:
    l = y
    k = x
    x = y
    y = k
  else:
    k = y
    l = x
  z = x%y
  while z != 0:
    x = y
    y = z
    z = x%y
  return (k//y)*(l//y)*y

if A%C ==0:
  i = (A//C)-1
else:
  i = A//C

j = B//C

if A%D ==0:
  k = (A//D)-1
else:
  k = A//D

l = B//D

if A%(gcd(C,D)) ==0:
  m = (A//(gcd(C,D)))-1
else:
  m = A//gcd(C,D)
  
n = B//gcd(C,D)

#print(i,j,k,l,m,n)
#print(B-A+1,j-i,l-k,n-m)
print((B-A+1)-(j-i)-(l-k)+(n-m))

Submission status: Accepted

Solution code:
def gcd(a,b):
    if(a<b):
        tmp=b
        b=a
        a=tmp
    while(a%b!=0):
        tmp_1=b
        tmp_2=a%b
        a=tmp_1
        b=tmp_2
    return b
    
def lcm(x,y):
    return int(x*y/gcd(x,y))

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

S=B-A+1
if(A%C==0):
    n_C=B//C-A//C+1
else:
     n_C=B//C-A//C

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

if(A%lcm(C,D)==0):
    n_CD=B//lcm(C,D)-A//lcm(C,D)+1
else:
    n_CD=B//lcm(C,D)-A//lcm(C,D)
    
print(S-n_C-n_D+n_CD)