Problem:


Score : 300 points

Problem Statement

You are given four integers A, B, C, and D. Find the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Constraints

1\leq A\leq B\leq 10^{18}

1\leq C,D\leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Sample Input 1

4 9 2 3

Sample Output 1

2

5 and 7 satisfy the condition.

Sample Input 2

10 40 6 8

Sample Output 2

23

Sample Input 3

314159265358979323 846264338327950288 419716939 937510582

Sample Output 3

532105071133627368



Sample input:
4 9 2 3


Sample output:
2

Submission status: Accepted

Solution code:
import fractions


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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
def unchi(x,m,n):
  r=x//m+x//n-x//(m*n//(gcd(m,n)))
  return r
a,b,c,d=(int(i) for i in input().split())
print(b-a+1-(unchi(b,c,d)-unchi(a-1,c,d)))

Submission status: Accepted

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

def gcd(x,y):
    a = max(x,y)
    b = min(x,y)
    r = a % b
    while r != 0:
        a = b
        b = r
        r = a % b
    return b

if a % c == 0:
    l = a
    if b % c == 0:
        r = b
    else:
        r = b - (b%c)
    if l > r:
        x = 0
    else:
        x = (r-l) // c + 1
else:
    l = a+(c-(a%c))
    if b % c == 0:
        r = b
    else:
        r = b - (b%c)
    if l > r:
        x = 0
    else:
        x = (r-l) // c + 1
if a % d == 0:
    l = a
    if b % d == 0:
        r = b
    else:
        r = b - (b%d)
    if l > r:
        y = 0
    else:
        y = (r-l) // d + 1
else:
    l = a+(d-(a%d))
    if b % d == 0:
        r = b
    else:
        r = b - (b%d)
    if l > r:
        y = 0
    else:
        y = (r-l) // d + 1
g = c*d//gcd(c,d)
if a % g == 0:
    l = a
    if b % g == 0:
        r = b
    else:
        r = b - (b%g)
    if l > r:
        z = 0
    else:
        z = (r-l) // g + 1
else:
    l = a+(g-(a%g))
    if b % g == 0:
        r = b
    else:
        r = b - (b%g)
    if l > r:
        z = 0
    else:
        z = (r-l) // g + 1
ans = b-a+1-x-y+z
print(ans)

Submission status: Accepted

Solution code:
def gcd(a, b):
    return gcd(b%a, a) if a else b
def lcm(a, b):
    return a * b // gcd(a, b)

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

total = B - A + 1

group_CD = lcm(C, D)

if A % C == 0:
    lower_C = A // C
else:
    lower_C = A // C + 1
upper_C = B // C + 1
num_C = upper_C - lower_C

if A % D == 0:
    lower_D = A // D
else:
    lower_D = A // D + 1
upper_D = B // D + 1
num_D = upper_D - lower_D

if A % group_CD == 0:
    lower_CD = A // group_CD
else:
    lower_CD = A // group_CD + 1
upper_CD = B // group_CD + 1
num_CD = upper_CD - lower_CD

print(total - num_C - num_D + num_CD)

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 = [int(s) for s in input().split()]
E = B-B//C-B//D+B//lcm(C,D)
Ap = A-1
F = Ap-Ap//C-Ap//D+Ap//lcm(C,D)
print(E-F)

Submission status: Accepted

Solution code:

def calGCD(x,y):
    while(y > 0):
        x, y = y, x % y
    return x

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

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

cntC = (B // C) - (A // C)
cntD = (B // D) - (A // D)
cntCD = (B // CD) - (A // CD)

if A % C == 0:
    cntC += 1
if A % D == 0:
    cntD += 1
if A % CD == 0:
    cntCD += 1

ans = B - A + 1 - (cntC + cntD - cntCD)
print(ans)

Submission status: Accepted

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


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


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

first_c = c * (a // c + 1)
if a % c == 0:
    first_c -= c
last_c = c * (b // c)

first_d = d * (a // d + 1)
if a % d == 0:
    first_d -= d
last_d = d * (b // d)

cd = lcm(c, d)
first_cd = cd * (a // cd + 1)
if a % cd == 0:
    first_cd -= cd
last_cd = cd * (b // cd)

cnum = 0
dnum = 0
cdnum = 0

if first_c <= b:
    cnum = (last_c - first_c) // c + 1

if first_d <= b:
    dnum = (last_d - first_d) // d + 1

if first_cd <= b:
    cdnum = (last_cd - first_cd) // cd + 1

total = cnum + dnum - cdnum
print(b - a + 1 - total)

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

c1=B//C
c2=(A-1)//C

cd1=B//((C*D)//gcd(C,D))
cd2=(A-1)//((C*D)//gcd(C,D))

d1=B//D
d2=(A-1)//D


print(str(int(B-A+1-(c1-c2)-(d1-d2)+(cd1-cd2))))

Submission status: Accepted

Solution code:
import fractions

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

    def num_of_multiples(st, ed, m):
        a = st % m
        a0 = st + (m - a)
        if a == 0:
            a0 = st
        if a0 > ed:
            return 0
        b = ed % m
        b0 = ed - b
        num = (b0 - a0)
        if num % m != 0:
            raise Exception()
        return (b0 - a0) // m + 1
    
    nc = num_of_multiples(A, B, C)
    nd = num_of_multiples(A, B, D)
    ncd = num_of_multiples(A, B, C * D // fractions.gcd(C, D))
    print(B - A + 1 - (nc + nd - ncd))


main()

Submission status: Accepted

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

from fractions import gcd

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

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

print(b-a+1-hoge)

Submission status: Accepted

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

cd_gcd = fractions.gcd(c, d)
cd_lcm = (c*d)//cd_gcd

div_c_min = (a-1) // c
div_c_max = b // c
div_d_min = (a-1) // d
div_d_max = b // d
div_cd_min = (a-1) // (cd_lcm)
div_cd_max = b // (cd_lcm)

total =  b - a +1

diff = (div_c_max-div_c_min) + (div_d_max-div_d_min) - (div_cd_max - div_cd_min)
diff_total = total -diff

print(diff_total)

Submission status: Accepted

Solution code:
import sys

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


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


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

    total = B - A + 1
    multi_c = max(0, B // C) - max(0, A // C)
    if A % C == 0:
        multi_c += 1
    multi_d = max(0, B // D) - max(0, A // D)
    if A % D == 0:
        multi_d += 1
    multi_e = max(0, B // E) - max(0, A // E)
    if A % E == 0:
        multi_e += 1
    return total - (multi_c + multi_d - multi_e)


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

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
# import bisect
# import math
# import numpy as np
# from fractions import gcd

"""Template"""


class IP:
    """
    入力を取得するクラス
    """
    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        """
        1文字の取得に使います
        :return: int
        """
        return int(self.input())

    def S(self):
        """
        1文字の取得(str
        :return: str
        """
        return self.input()

    def IL(self):
        """
        1行を空白で区切りリストにします(int
        :return: リスト
        """
        return list(map(int, self.input().split()))

    def SL(self):
        """
        1行の文字列を空白区切りでリストにします
        :return: リスト
        """
        return list(map(str, self.input().split()))

    def ILS(self, n):
        """
        1列丸々取得します(int
        :param n: 行数
        :return: リスト
        """
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        """
        1列丸々取得します（str
        :param n: 行数
        :return: リスト
        """
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        """
        Some Int LineS
        横に複数、縦にも複数
        :param n: 行数
        :return: list
        """
        return [self.IL() for _ in range(n)]

    def SSLS(self, n):
        """
        Some String LineS
        :param n: 行数
        :return: list
        """
        return [self.SL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        """
        Half enumeration
        半分全列挙です
        pの要素の和の組み合わせを作ります。
        ソート、重複削除行います
        :param p: list : 元となるリスト
        :return: list : 組み合わせられた和のリスト
        """
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        """
        bit_full_search2
        bit全探索の改良版
        全探索させたら2進数のリストと10進数のリストを返す
        :return: list２つ : １個目 2進数(16桁)  2個目 10進数
        """
        # 参考
        # https://blog.rossywhite.com/2018/08/06/bit-search/
        # https://atcoder.jp/contests/abc105/submissions/4088632
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    """右からj+1番目のiが1かどうか判定"""
                    # output.append(a[j])
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        """
        ソート関係行います。色々な設定あります。
        :param s: 元となるリスト
        :param r: reversするかどうか 0=False 1=True
        :param m: （2次元配列）何番目のインデックスのソートなのか
        :return: None
        """
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        """
        bit探索で使います。0以上のときにTrue出します
        自然数だからn
        :param a: int
        :param b: int
        :return: bool
        """
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        """
        bit探索で使います。1のときにTrue出すよ
        oneで1
        :param a: int
        :param b: int
        :return: bool
        """
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        """
        Round up
        小数点切り上げ割り算
        :param x: int
        :param y: int
        :return: int
        """
        return -(-x//y)

    def ave(self, a):
        """
        平均を求めます
        :param a: list
        :return: int
        """
        return sum(a) / len(a)

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

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


"""ここからメインコード"""


def main():
    # 1文字に省略
    r, e = range, enumerate
    ip = IP()
    id = Idea()

    """この下から書いてね"""
    a, b, c, d = ip.IL()
    memo = b - a + 1
    memo2 = id.lcm(c, d)
    a_c = (a-1) // c
    a_d = (a-1) // d
    b_c = b // c
    b_d = b // d
    b_cd = b // memo2
    a_cd = a // memo2
    if a % memo2 == 0:
        aaa = 1
    else:
        aaa = 0

    acd = a_c + a_d
    bcd = b_c + b_d
    kaburi = b_cd - a_cd
    # print(memo)
    # print(a_c, a_d, b_c, b_d, b_cd, a_cd)
    # print(bcd - acd - kaburi)
    ans = bcd - acd - kaburi - aaa
    print(memo - ans)


main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

import fractions

def ri():
    return int(input())

def rl():
    return list(input().split())

def rli():
    return list(map(int, input().split()))

def calc(n, x):
    return n // x

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

def main():
    a, b, c, d = rli()
    lcm_cd = lcm(c, d)
    n_a = a-1 - calc(a-1, c) - calc(a-1, d) + calc(a-1, lcm_cd)
    n_b = b - calc(b, c) - calc(b, d) + calc(b, lcm_cd)
    print(n_b-n_a)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
a, b, c, d = map(int, input().split())
cc = c
dd = d
if cc < dd:
    cc, dd = dd, cc
while cc % dd != 0:
    cc, dd = dd, cc % dd
gcd = dd

sum = 0
mi = (a-1)//c
ma = b//c
sum += ma - mi

mi = (a-1)//d
ma = b//d
sum += ma - mi

cd = c * d // gcd
mi = (a-1)//cd
ma = b//cd
sum -= ma - mi
print(b-a-sum+1)

Submission status: Accepted

Solution code:
import fractions
A, B, C, D = map(int, input().split())
waric = 1 if A%C == 0 else 0
warid = 1 if A%D == 0 else 0
l = (C*D)//(fractions.gcd(C,D))
waricd = 1 if A%l == 0 else 0
cnd = B - A + 1 - (B//C - A//C + waric)
dnd = B - A + 1 - (B//D - A//D + warid) 
cdnd = B - A + 1 - (B//l - A//l + waricd)
print(cnd + dnd - cdnd)

Submission status: Accepted

Solution code:
#! usr/bin/env python
# -*- coding: utf-8 -*-


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


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


def func(x):
    return x - x // c - x // d + x // l


def main():
    print(func(b)-func(a-1))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from fractions import gcd

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

cnt = int(0)

for i in [c, d]:
    t4 = a // i
    if a % i == 0:
        t1 = t4
    else:
        t1 = t4+1
    t2 = b // i

    cnt += int(t2 - t1 + 1)

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

i = lcm
t4 = a // i
if a % i == 0:
    t1 = t4
else:
    t1 = t4 + 1
t2 = b // i

cnt -= int(t2 - t1 + 1)

print(b - a + 1 - cnt)

Submission status: Accepted

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

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

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

count = b // c - a // c
if a % c == 0:
    count += 1

count += b // d - a // d
if a % d == 0:
    count += 1

count -= (b // lcm - a // lcm)
if a % lcm == 0:
    count -= 1

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

Submission status: Accepted

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

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

def keisan(s,e,d):
    if s%d == 0:
        return e//d - s//d + 1
    else:
        return e//d - s//d

if B == A:
    if A%C == 0 or A%D == 0:
        print(0)
    else:
        print(1)
else:
    print(B-A+1-(keisan(A,B,C)+keisan(A,B,D)-keisan(A,B,lcm(C,D))))

Submission status: Accepted

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

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

def main():
    A, B, C, D = map(int, input().split())
    B_C = B // C
    B_D = B // D
    A_C = (A-1) // C
    A_D = (A-1) // D
    min_lcm = lcm(C, D)
    B_min_lcm = B // min_lcm
    A_min_lcm = (A-1) // min_lcm
    x = (B - A + 1) - ((B_C - A_C) + (B_D - A_D) - (B_min_lcm - A_min_lcm))
    print(x)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

def ans(v1,v2,v3):
    return v1-v1//v2-v1//v3+v1//(v2*v3//fractions.gcd(v2,v3))
print(int(ans(B,C,D)-ans(A-1,C,D)))

Submission status: Accepted

Solution code:
from sys import stdin
input = stdin.readline().split()
A, B, C, D = int(input[0]), int(input[1]), int(input[2]), int(input[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))
# print(gcd(C, D))
val_lcm = lcm(C, D)
# print(val_lcm)
b_c = B // C
b_d = B // D
a_c = (A - 1) // C
a_d = (A - 1) // D
b_lcm = B // val_lcm
a_lcm = (A - 1) // val_lcm
dif_c = b_c - a_c
dif_d = b_d - a_d
dif_lcm = b_lcm - a_lcm
# print(dif_c, dif_d, dif_lcm)
print(B - A + 1 - (dif_c + dif_d) + dif_lcm)

Submission status: Accepted

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

ABc = b // c - (a - 1) // c
ABd = b // d - (a - 1) // d
cd = c * d // fractions.gcd(c, d)
ABcd = b // cd - (a - 1) // cd

print(b - a + 1 - (ABc + ABd - ABcd))

Submission status: Accepted

Solution code:
from sys import stdin

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
  
A,B,C,D = map(int,stdin.readline().rstrip().split())
lcm=(C * D) // gcd(C, D)

AdivLcm=A//lcm
BdivLcm=B//lcm
ABdivLcmNum=BdivLcm-AdivLcm
if A%lcm==0:
  ABdivLcmNum+=1

AdivC = A//C
BdivC = B//C
ABdivCNum = BdivC-AdivC
if A%C==0:
  ABdivCNum+=1

AdivD = A//D
BdivD = B//D
ABdivDNum = BdivD-AdivD
if A%D==0:
  ABdivDNum+=1

candiv=ABdivCNum+ABdivDNum-ABdivLcmNum
print(B-A+1-candiv)

Submission status: Accepted

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

# 最小公倍数を求める
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))

div_C = (B//C) - ((A // C - 1) if A % C == 0 else (A // C))
div_D = (B//D) - ((A // D - 1) if A % D == 0 else (A // D))
mul = lcm(C,D)
div_mul = (B//mul) - ((A // mul - 1) if A % mul == 0 else (A // mul))
ans = (B - A + 1) - (div_C + div_D - div_mul)
print(ans)

Submission status: Accepted

Solution code:
def gcd(a,b):
    while b > 0:
        a, b = b, a % b
    return a
    
def lcm(a, b):
    return a * b / gcd(a, b)
  
a = [int(i) for i in input().split()]
k = a[1]//a[2] - (a[0] - 1)//a[2]
r = a[1]//a[3] - (a[0] - 1)//a[3]
s = a[1]//(int(lcm(a[2],a[3]))) - (a[0] - 1)//int((lcm(a[2],a[3])))
print((a[1] - a[0] + 1 - (k + r - s)))

Submission status: Accepted

Solution code:

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
        
A, B, C, D = map(int, input().split())
lcm = int(C * D / gcd(C, D))

num_B_C = B // C
num_B_D = B // D
num_B_CD = B // (lcm)

num_A_C = (A-1) // C
num_A_D = (A-1) // D
num_A_CD = (A-1) // (lcm)

num_all = (num_B_C - num_A_C) + (num_B_D - num_A_D) - (num_B_CD - num_A_CD)
result = (B - (A-1)) - num_all

print(result)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import fractions as math
#import math
a,b,c,d = map(int,input().split())
cd=c*d//math.gcd(c,d)
tc=((a-1)//c+1)*c
td=((a-1)//d+1)*d
tcd=((a-1)//(cd)+1)*(cd)

ans= (b-a)+1
if c<=b:
    ans-=((b-tc)//c+1)
if d<=b:
    ans-=((b-td)//d+1)
if cd<=b:
    ans+=((b-tcd)//(cd)+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)

BdivC=B//C
BdivD=B//D
BdivCD=B//(lcm(C,D))
AdivC=(A-1)//C
AdivD=(A-1)//D
AdivCD=(A-1)//(lcm(C,D))

ans=B-(A-1)-(BdivC-AdivC+BdivD-AdivD-BdivCD+AdivCD)

print(ans)

Submission status: Accepted

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

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

A, B, C, D = MAP()

num_C_yaku = B//C - (A-1)//C  # Cで割り切れる
num_D_yaku = B//D - (A-1)//D  # Dで割り切れる

num_CD_yaku = B//(C*D//int(gcd(C, D))) - (A-1)//(C*D//int(gcd(C, D)))  # CとDの最小公倍数で割り切れる 

# print("C",num_C_yaku)
# print("D",num_D_yaku)
# print("CDの公倍数",num_CD_yaku)
print((B-A+1)-(num_C_yaku+num_D_yaku-num_CD_yaku))

Submission status: Accepted

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

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

def solve(num, c, d):
  l=lcm(c,d)
  g=gcd(c,d)
  z=l-c//g-d//g+1
  left=(num//l)*l
  ret=(num//l)*z
  ret+=(num-left)-(num-left)//c-(num-left)//d
  return ret

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

Submission status: Accepted

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

def mul(head, tail, x):
    length = tail - head + 1
    ans = length // x
    if x - (head % x) <= length % x - 1:
        return ans+1
    return ans

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 - (mul(A, B, C) + mul(A, B, D) - mul(A, B, lcm(C, D))))

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())
target = B - A + 1

hoge1 = B // C - (A - 1) // C
hoge2 = B // D - (A - 1) // D
hoge3 = B // (C * D) - (A - 1) // (C * D)

saisho = lcm(C, D)
hoge4 = B // saisho - (A - 1) // saisho

print(target - hoge1 - hoge2 + hoge4)

Submission status: Accepted

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

e = (c//f.gcd(c,d))*d


if a%c == 0:
    p = b//c - a//c +1
else:
    p = b//c - a//c
if a%d == 0:
    q = b//d - a//d +1
else:
    q = b//d - a//d
if a%e == 0:
    r = b//e - a//e +1
else:
    r = b//e - a//e  

print(b-a+1-p-q+r)

Submission status: Accepted

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

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

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

l = lcm(C, D)

A = A-1
x = (B-A)
y = (B//C - A//C)
z = (B//D - A//D)
a = (B//l - A//l)
print(x+a - (y+z))

Submission status: Accepted

Solution code:
def gcd(a,b):
    if b==0:
        return a
    
    return gcd(b,a%b)
  
def lcm(x,y):
    return (x*y) // gcd(x, y)

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

A_C=A//C
if C*A_C!=A:
  A_C+=1
B_C=B//C  
A_D=A//D
if D*A_D!=A:
  A_D+=1
B_D=B//D

E=lcm(C,D)
A_E=A//E
if E*A_E!=A:
  A_E+=1
B_E=B//E

print(B-A+1-(B_C-A_C+1+B_D-A_D+1-(B_E-A_E+1)))

Submission status: Accepted

Solution code:
def lcm(x, y):
    return (x * y) // gcd(x, y)
def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    c = a % b
    return gcd(b, c)
A,B,C,D=map(int,input().split())
abc=B//C-A//C
abd=B//D-A//D
abcd=(B//(lcm(C,D)))-(A//(lcm(C,D)))
if A%C==0:
    abc+=1
if A%D==0:
    abd+=1
if A%(lcm(C,D))==0:
    abcd+=1



#print(B-A+1)
#print(abc,abd,abcd)
print(B-A+1-(abc+abd-abcd))

Submission status: Accepted

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

amount = b - a + 1

if a % c == 0:
  c_bottom = a // c
else:
  c_bottom = a // c + 1
c_top = b // c
num_c = c_top - c_bottom + 1

if a % d == 0:
  d_bottom = a // d
else:
  d_bottom = a // d + 1
d_top = b // d
num_d = d_top - d_bottom + 1

import fractions

e = int(c * d / fractions.gcd(c, d))
if a % e == 0:
  e_bottom = a // e
else:
  e_bottom = a // e + 1
e_top = b // e
num_e = e_top - e_bottom + 1

print(amount - num_c - num_d + num_e)

Submission status: Accepted

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

x = c
y = d
while y:
    x, y = y, x % y
gcd = x
lcm = c * d // gcd
if a != b:
    sc = (b // c - (a-1) // c)
    sd = (b // d - (a-1) // d)
    slcm = b // lcm - (a-1) // lcm
    print(b-a+1 - sc - sd + slcm)
else:
    if a % c != 0 and a % d != 0:
        print(1)
    else:
        print(0)

Submission status: Accepted

Solution code:

def gcd(a,b):
  x = max([a,b])
  y = min([a,b])
  while y:
    x, y = y, x % y
  return x

# x * y = lcm(x, y) // gcd(x, y)より
def lcm(x, y):
  return x * y // gcd(x, y)


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

cd = lcm(c,d)
b_c = b // c
b_d = b // d
a_c = (a-1)//c
a_d = (a-1)//d
b_cd = b//(cd)
a_cd = (a-1)//(cd)
nums = b-a+1

ans = nums - (b_c - a_c) - (b_d - a_d) + (b_cd - a_cd)


print(ans)

Submission status: Accepted

Solution code:
import fractions

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

countAB = B - A + 1
ansC = B // C - (A - 1) // C
ansD = B // D - (A - 1) // D
lcmCD = (C * D) // fractions.gcd(C,D)
ansCD = B // (lcmCD) - (A - 1) // (lcmCD)

print(countAB - (ansC + ansD - ansCD))

Submission status: Accepted

Solution code:
import fractions

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

lower_C = -(-A//C)
upper_C = B//C
num_C = upper_C - lower_C + 1
lower_D = -(-A//D)
upper_D = B//D
num_D = upper_D - lower_D + 1
lcm = C*D//fractions.gcd(C, D)
lower_lcm = -(-A//lcm)
upper_lcm = B//lcm
num_lcm = upper_lcm - lower_lcm + 1

print(B - A + 1 - num_C - num_D + num_lcm)

Submission status: Accepted

Solution code:
from sys import stdin
import fractions
def lcm(a,b):
    return a * b // fractions.gcd(a,b)
A,B,C,D = [int(x) for x in stdin.readline().rstrip().split()]
div_C = (B // C) - ((A-1) // C)
div_D = (B // D) - ((A-1) // D)
div_lcm = (B // lcm(C,D)) - ((A-1) // lcm(C,D))
ans = B - A  - div_C - div_D + div_lcm + 1
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin


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


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


def main():
    A, B, C, D = [int(x) for x in stdin.readline().rstrip().split()]
    min_c = A // C if A % C == 0 else A // C + 1
    max_c = B // C if B % C == 0 else B // C
    min_d = A // D if A % D == 0 else A // D + 1
    max_d = B // D if B % D == 0 else B // D
    cd = lcm(C, D)
    min_cd = A // cd if A % cd == 0 else A // cd + 1
    max_cd = B // cd if B % cd == 0 else B // cd

    print((B - A + 1) - ((max_c + 1 - min_c) + (max_d + 1 - min_d) - (max_cd + 1 - min_cd)))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import fractions

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

def cantdivide(num, d1, d2):
  def lcm(num1, num2):
    return int(num1 * num2 / fractions.gcd(num1, num2))
  
  return num - (num//d1) - (num//d2) + (num//lcm(d1,d2))
  
ans = cantdivide(B, C, D) - cantdivide((A-1), C, D)

print(ans)

Submission status: Accepted

Solution code:
#ユークリッド互除法！
def gcd(a,b):
  if(a<b):
    a,b=b,a
  if(b==0):
    return a
  c= a%b
  return gcd(b,c)

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

a_c=a//c
a_d=a//d
a_cd=a//(c*d//gcd(c,d))
a_ans=a-a_c-a_d+a_cd
#print(a_ans,a,a_c,a_d,a_cd)

b_c=b//c
b_d=b//d
b_cd=b//(c*d//gcd(c,d))
b_ans=b-b_c-b_d+b_cd
#print(b_ans,b,b_c,b_d,b_cd)

ans=1
if(a%c==0 or a%d==0):
  ans=0

print(int(b_ans-a_ans)+ans)

Submission status: Accepted

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

Submission status: Accepted

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

Cprimes = {}
Dprimes = {}

tmpC = C
d = 2
while tmpC > 1:
    if tmpC % d == 0:
        if d in Cprimes:
            Cprimes[d] += 1
        else:
            Cprimes[d] = 1
        tmpC //= d
    else:
        d += 1

tmpD = D
d = 2
while tmpD > 1:
    if tmpD % d == 0:
        if d in Dprimes:
            Dprimes[d] += 1
        else:
            Dprimes[d] = 1
        tmpD //= d
    else:
        d += 1

comPrimes = {}
for k in Cprimes:
    comPrimes[k] = Cprimes[k]
for k in Dprimes:
    if k in comPrimes:
        if comPrimes[k] < Dprimes[k]:
            comPrimes[k] = Dprimes[k]
    else:
        comPrimes[k] = Dprimes[k]

comVal = 1
for k in comPrimes:
    comVal *= k**comPrimes[k]

#print(Cprimes)
#print(Dprimes)
#print(comPrimes)
#print(comVal)

CdivNum = B//C - (A-1)//C
DdivNum = B//D - (A-1)//D
comDivNum = B//comVal - (A-1)//comVal
ans = B-(A-1)
ans -= CdivNum
ans -= DdivNum
ans += comDivNum

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

# A-1 以下の割り切れるものの個数
a_c = (A-1) // C
a_d = (A-1) // D
a_cd = (A-1) // lcm(C, D)

# B以下の割り切れるものの個数
b_c = B // C
b_d = B // D
b_cd = B // lcm(C, D)

# A以上B以下の割り切れるものの個数
ab = (b_c + b_d - b_cd) - (a_c + a_d - a_cd)

# 割り切れないものの個数
ans = (B - A + 1) - ab

print(ans)

Submission status: Accepted

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

#a~bの整数の数-(bまでのc,dで割り切れる数-(a-1)までのc,dで割り切れる数)
#ans = x-(y-z)

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

e = c*d//gcd(c,d)
#eはlcm(最小公倍数)
x = b-a+1
f = a-1
y = b//c+b//d-b//e
z = f//c+f//d-f//e

ans = x-(y-z)
#print("x:"+str(x))
#print("y:"+str(y))
#print("z:"+str(z))
print(ans)

Submission status: Accepted

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

lcm_cd = c*d//fractions.gcd(c,d)

a_div_c = a//c
ac_mod = a%c
bc = b//c

a_div_d = a//d
ad_mod = a%d
bd = b//d

a_div_lcm_cd = a//lcm_cd
alcm_cd_mod = a%lcm_cd
blcm_cd = b//lcm_cd

cnt_c = 0
cnt_d = 0
cnt_lcmcd = 0

cnt_c = (bc-a_div_c)
if not ac_mod:
    cnt_c+=1

cnt_d = (bd-a_div_d)
if not ad_mod:
    cnt_d+=1

cnt_lcmcd = (blcm_cd-a_div_lcm_cd)
if not alcm_cd_mod:
    cnt_lcmcd+=1
print(b-a+1-(cnt_c+cnt_d-cnt_lcmcd))

Submission status: Accepted

Solution code:
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
inputs = [int(s) for s in input().split()]
a = inputs[0]
b = inputs[1]
c = inputs[2]
d = inputs[3]
count = 0
minCD = min(c, d)
maxCD = max(c, d)

x0 = b-a+1
x1 = b//minCD - (a-1)//minCD
x2 = b//maxCD - (a-1)//maxCD
lcm = minCD * maxCD // gcd(minCD, maxCD)
x3 = b//lcm - (a-1)//lcm
if maxCD % minCD == 0:
    print(x0-x1)
else:
    print(x0-(x1+x2-x3))

Submission status: Accepted

Solution code:
import fractions

def fnc(start, end, divisor):
  if start > end:
    return False
  return start % divisor > end % divisor

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

rng = B - A + 1

Cmod = rng // C
if fnc(A+C*(rng // C), B, C):
  Cmod += 1

Dmod = rng // D
if fnc(A+D*(rng // D), B, D):
  Dmod += 1

lcm = C*D // fractions.gcd(C, D)
lcmod = rng // lcm
if fnc(A+lcm*(rng // lcm), B, lcm):
  lcmod += 1

print(rng - Cmod - Dmod+ lcmod)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import fractions

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


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

c_cnt = B//C - (A-1)//C


d_cnt = B//D - (A-1)//D

cd_lcm = lcm(C,D)

cd_cnt = B//cd_lcm - (A-1)//cd_lcm


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

Submission status: Accepted

Solution code:
import sys,heapq,fractions
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True  and False
def ddprint(x):
  if DBG:
    print(x)

def foo(x,c,d):
    xc = (x)//c + 1
    xd = (x)//d + 1
    g = c * d // fractions.gcd(c,d)
    xg = (x)//g + 1
    z = x+1-(xc+xd-xg)
    ddprint("x {} c {} d {} xc {} xd {} g {} xg {} z {}".format(x,c,d,xc,xd,g,xg,z))
    return z

a,b,c,d = inm()
print(foo(b,c,d)-foo(a-1,c,d))

Submission status: Accepted

Solution code:

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

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
def lcm(x, y):
    return (x * y) // gcd(x, y)

div1 = b//c - a//c

div2 = b//d - a//d

e = lcm(c,d)
div3 = b//e - a//e

if a%c==0:
    div1 += 1
if a%d==0:
    div2 += 1
if a%e==0:
    div3 += 1

ans = b-a+1 - div1 - div2 + div3
print(ans)