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

ansB = (B-B//C)+(B-B//D)-(B-B//X)
ansA = ((A-1)-((A-1)//C))+((A-1)-(A-1)//D)-((A-1)-(A-1)//X)

print(ansB-ansA)

Submission status: Accepted

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


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

X = lcm(C, D)

ansB = (B-B//C)+(B-B//D)-(B-B//X)
ansA = ((A-1)-((A-1)//C))+((A-1)-(A-1)//D)-((A-1)-(A-1)//X)

print(ansB-ansA)

Submission status: Accepted

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

#cで割り切れる数+dで割り切れる数-gcd(c,d)で割り切れる数を求めればよい
ans=(b//c+b//d-b//(c*d//math.gcd(c,d)))-((a-1)//c+(a-1)//d-(a-1)//(c*d//math.gcd(c,d)))

print(b-a+1-ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
#print#!/usr/bin/env python3
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**9)
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
def lispin(t=int):return list(pin(t))
#%%code
from fractions import gcd
import math
def resolve():
    A,B,C,D=pin()
    E=(C*D)//gcd(C,D)#lcm
    a=[]
    for x in [C,D,E]:
        s=((A-1)%x)+1
        t=(B)%x
        
        a.append((B-A+s-t)//x)
    #print(a)
    print(B-A+1-a[0]-a[1]+a[2])
    
    
#%%submit!
resolve()

Submission status: Accepted

Solution code:
from math import floor

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

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

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

def n(n):
    c = n // C
    d = n // D
    cd = n // lcm(C,D)
    return n - c - d + cd

print(n(B)-n(A-1))

Submission status: Accepted

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

# 最大公約数
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
# 最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

a1 = A // C if (A // C) * C >= A else A // C + 1
a2 = (B // C + 1) if (B // C + 1) * C <= B else B // C
a = a2 - a1 + 1

b1 = A // D if (A // D) * D >= A else A // D + 1
b2 = (B // D + 1) if (B // D + 1) * D <= B else B // D
b = b2 - b1 + 1

E = lcm(C, D)
c1 = A // E if (A // E) * E >= A else A // E + 1
c2 = (B // E + 1) if (B // E + 1) * E <= B else B // E
c = c2 - c1 + 1

ans = (B - A + 1) - (a + b - c)
print(ans)

Submission status: Accepted

Solution code:
def wareru(x,y,z):
  if x%z == 0:
    return y//z - x//z + 1
  else:
    return y//z - x//z

import fractions

a,b,c,d = map(int,input().split())
e = c*d//(fractions.gcd(c,d))
c_hoge = wareru(a,b,c)
d_hoge = wareru(a,b,d)
e_hoge = wareru(a,b,e)
print(b-a+1-c_hoge-d_hoge+e_hoge)

Submission status: Accepted

Solution code:
from fractions import gcd

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

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

def solve(n):
    x = n // c
    y = n // d
    z = n // lcm(c, d)
    return n - x - y + z

print(solve(b) - solve(a - 1))

Submission status: Accepted

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

def gcd(i, j):
    while j:
        i, j = j, i % j
    return i

def num_d(x, y, z):
    l, m, n = x//y, x//z, x//(y*z//(gcd(y, z)))
    return x - l - m + n


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

Submission status: Accepted

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

def gcd(i, j):
    while j:
        i, j = j, i % j
    return i

def num_d(x, y, z):
    l, m, n = x//y, x//z, x//(y*z//(gcd(y, z)))
    return l + m - n


X = b - num_d(b, c, d)
Y = a - num_d(a, c, d)

print(X - Y)

Submission status: Accepted

Solution code:
#dpでできないかな？
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod=10**9+7

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

def lcm(a, b):
    return a // gcd(a, b) * b
a,b,c,d=MI()
u=lcm(c,d)
#print(u)
#1~x mod y==0
'''def div(x,y):
    return int(x/y) '''
'''ansb=b-div(b,c)-div(b,d)+div(b,u)
ansa=a-1-div(a-1,c)-div(a-1,d)+div(a-1,u)'''
ansb=b-b//c-b//d+b//u
ansa=a-1-(a-1)//c-(a-1)//d+(a-1)//u

print(ansb-ansa)
mod = 10**9 + 7

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# モジュールのインポート
from fractions import gcd

# 標準入力の取得
A, B, C, D = list(map(int, input().split()))


def lcm(x: int, y: int) -> int:
    """最小公倍数を求めるメソッド

    Arguments:
        x {int} -- 整数
        y {int} -- 整数

    Returns:
        int -- 最小公倍数
    """
    return (x * y) // gcd(x, y)


def main() -> None:
    """Entry point
    """
    # 求解処理
    """
    CでもDでも割り切れないものの個数
    = 全体 - Cで割り切れる個数 - Dで割り切れる個数 + lcm(C,D)で割り切れる個数
    """
    div_C = (B // C) - ((A - 1) // C)
    div_D = (B // D) - ((A - 1) // D)
    lcm_CD = lcm(C, D)
    div_lcm_CD = (B // lcm_CD) - ((A - 1) // lcm_CD)
    result = (B - A + 1) - div_C - div_D + div_lcm_CD

    # 結果出力
    print(result)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# モジュールのインポート
from fractions import gcd

# 標準入力の取得
A, B, C, D = list(map(int, input().split()))


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


# 求解処理
div_C = (B // C) - ((A - 1) // C)
div_D = (B // D) - ((A - 1) // D)
lcm_CD = lcm(C, D)
div_lcm_CD = (B // lcm_CD) - ((A - 1) // lcm_CD)
result = (B - A + 1) - div_C - div_D + div_lcm_CD

# 結果出力
print(result)

Submission status: Accepted

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


def readstr():
    return input().strip()


def readint():
    return int(input())


def readnums():
    return map(int, input().split())


def readstrs():
    return input().split()


def main():
    A, B, C, D = readnums()
    g = C * D // gcd(C, D)
    c1 = B // C
    c2 = (A - 1) // C
    d1 = B // D
    d2 = (A - 1) // D
    g1 = B // g
    g2 = (A - 1) // g
    print(B - A + 1 + (g1 - g2) - (c1 - c2) - (d1 - d2))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import itertools
import math
import fractions
import string
import collections
from collections import Counter
from collections import deque
from operator import itemgetter
import sys
sys.setrecursionlimit(2*10**5)
INF = 2**60


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


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
a, b, c, d = map(int, input().split())


def func(n, c, d):
    x = n//c
    y = n//d
    z = n//((c * d) // fractions.gcd(c, d))
    zz = (x+y)-z
    return zz


tmp = b-func(b, c, d)
tmp2 = (a-1)-func(a-1, c, d)
print(tmp-tmp2)
#print('test', func(6, 2, 3))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

    def count(x, c, d) -> int:
        return x - x // c - x // d + x // lcm(c, d)

    res = count(B, C, D) - count(A - 1, C, D)
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from fractions import gcd

a, b, c, d = map(int, readline().split())
print((b - a + 1) - (b // c + b // d) + ((a - 1) // c + (a - 1) // d) + b // (c * d // gcd(c, d)) - (a - 1) // (c * d // gcd(c, d)))

Submission status: Accepted

Solution code:
# n, k = map(int, input().split())
# h = []
# for i in range(n):
#     h.append(int(input()))

# h.sort(reverse=True)
# ans = []
# for i in range(k):
#     if i+k-1 >= n:
#         break
#     ans.append(h[i] - h[i+k-1])
# print(min(ans))

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

def lcm(a):
    LCM = a[0]
    n = len(a)
    for i in range(1, n):
        LCM = LCM * a[i] // gcd(LCM, a[i])
    return LCM

lcm = lcm([c,d])

# a~bまでのうち、cの倍数
modC = b//c - (a-1)//c

# a~bまでのうち、dの倍数
modD = b//d - (a-1)//d

modCD = b//(lcm) - (a-1)//(lcm)

ans = b - a + 1 - modC - modD + modCD
# print(modC)
# print(modD)
# print(modCD)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import fractions
a,b,c,d=map(int,input().split())
x=b//c
y=b//d
xy=b//(c*d//fractions.gcd(c,d))
b=b-x-y+xy

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

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())
L = lcm(C,D)
#A-1以下のCorDで割り切れる個数
a = (A-1)//C + (A-1)//D - (A-1)//lcm(C,D)
#B以下のCorDで割り切れる個数
b = B//C + B//D - B//lcm(C,D)
ans = B-(A-1) - (b-a)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
MOD = 10 ** 9 + 7

from fractions import gcd

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

A -= 1

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

def calc(x):
    count = 0
    count += x // C
    count += x // D
    count -= x // lcm
    return x - count

ans = calc(B) - calc(A)
print (ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from fractions import gcd

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

def f(n, m):
    return n // m

l = c * d // gcd(c, d)
res = f(b, c) + f(b, d) - f(b, l)
res -= f(a - 1, c) + f(a - 1, d) - f(a - 1, l)
print(b - a + 1 - res)

Submission status: Accepted

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

import fractions
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
A -= 1
s = lcm(C,D)
X =  A - (A//C + A //D - A//s)
Y =  B - (B//C + B //D - B//s)
print(Y-X)

Submission status: Accepted

Solution code:
import sys, re, os
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 heapq import heapify, heappop, heappush
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

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


A, B, C, D = MAP()
# print(gcd(C, D))
def f(N):
    a = N // C
    b = N // D
    c = N // lcm(C, D)
    # print(a, b, c)
    return N - (a + b - c)
print(f(B)-f(A-1))
# print(f(B))
# print(f(A-1))

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

a,b,c,d = inpl()
a -= 1
e = c*d//fractions.gcd(c,d)
B = b - (b//c + b//d - b//e)
A = a - (a//c + a//d - a//e)
print(B - A)

Submission status: Accepted

Solution code:
from sys import stdin
from fractions import gcd
def main():
    #入力
    readline=stdin.readline
    a,b,c,d=map(int,readline().split())
    a-=1
    l=c*d//gcd(c,d)
    print((b-(b//c+b//d-b//l))-(a-(a//c+a//d-a//l)))
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


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

    l = lcm(c, d)

    def calc(n):
        res = n // c + n // d - n // l
        return n - res

    print(calc(b) - calc(a - 1))


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
from fractions import gcd

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

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))
import fractions
a,b,c,d = iim()

def antiDiv(n):
    return n-n//c-n//d+n//(c*d//fractions.gcd(c,d))
print(antiDiv(b)-antiDiv(a-1))

Submission status: Accepted

Solution code:
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

a,b,c,d = map(int,input().split())
C = b//c-(a-1)//c
D = b//d - (a-1)//d
def gcd(a, b):
    while(b != 0):
        a, b = b, a % b
    return a
    
def lcm(m,n):
    return (m*n)//gcd(m,n)
cd = lcm(c,d)
CD = b//cd - (a-1)//cd
print(b-a+1-C-D+CD)
# print(C,D,CD)

Submission status: Accepted

Solution code:
import sys
from fractions import gcd
input = sys.stdin.readline
A, B, C, D = map(int, input().split())
l = (A - 1) // C + (A - 1) // D - (A - 1) // (C * D // gcd(C, D))
r = B // C + B // D - B // (C * D // gcd(C, D))
print(B - A + 1 - (r - l))

Submission status: Accepted

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

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

#a,bの最小公倍数
def lcm(a, b):
    return a * b // gcd (a, b)
E = lcm(C,D)
print( (B-A+1)-((B//C-A//C + (A%C==0)) + (B//D-A//D+ (A%D==0)) - (B//E-A//E+ (A%E==0))))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import fractions
A,B,C,D = (int(x) for x in input().split())
c = 0
d = 0
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
M = lcm(C,D)
c = B//C - A//C
if A%C == 0:
  c = c+1
d = B//D - A//D
if A%D == 0:
  d = d+1
m = B//M - A//M
if A%M == 0:
  m = m+1
print(B-A+1-(c+d-m))

Submission status: Accepted

Solution code:
from functools import reduce

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

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

def lcm_list(numbers):
  return reduce(lcm_base, numbers, 1)

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

lcm = lcm_list([c,d])

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

print(res2 - res1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

from fractions import gcd
a,b,c,d = iim()


def num(i):
    n = c*d//gcd(c,d)
    return i-i//c-i//d+i//n

print(num(b)-num(a-1))

Submission status: Accepted

Solution code:
A,B,C,D = list(map(int,input().split()))
X = B//C - (A-1)//C
Y = B//D - (A-1)//D
x = max(C,D)
y = min(C,D)

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

L = (C*D)//gcd(x,y)
Z = B//L - (A-1)//L
  
print(B-A+1-X-Y+Z)

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)

LCM = lcm(C,D)
BC = B // C
BD = B // D
BCD = B // LCM
AC = (A-1) // C
AD = (A-1) // D
ACD = (A-1) // LCM
x = B - BC - BD + BCD
y = (A-1) - AC - AD + ACD
print(x - y)

Submission status: Accepted

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

Submission status: Accepted

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

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

z=lcm(c,d)
cddead=b//z-(a-1)//z
print(total-cdead-ddead+cddead)

Submission status: Accepted

Solution code:
from fractions import gcd


def main():
    A, B, C, D = [int(x) for x in input().split()]
    l = (C * D) // gcd(C, D)
    b = B - anti(B, C, D, l)
    a = (A - 1) - anti((A - 1), C, D, l)
    print(int(b - a))


def anti(x, y, z, l):
    return (x // y) + (x // z) - (x // l)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

num_b_c = b//c
num_b_d = b//d
num_b_cd = b//lcm

if a%c==0:
    num_a_c = a//c - 1
else:
    num_a_c = a//c

if a%d==0:
    num_a_d = a//d - 1
else:
    num_a_d = a//d

if a%lcm==0:
    num_a_cd = a//lcm - 1 
else:

    num_a_cd = a//lcm


alls = b - a + 1


c_div = num_b_c - num_a_c
d_div = num_b_d - num_a_d

cd_div = num_b_cd -num_a_cd


print(alls - ( c_div + d_div - cd_div))

Submission status: Accepted

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

def unko(x,C,D):
  import fractions
  t=int(fractions.gcd(C,D))
  t_=C*D//t
  return x-(x//C+x//D-x//t_)
print(int(unko(B,C,D)-unko(A-1,C,D)))

Submission status: Accepted

Solution code:
def solve():
    A, B, C, D = map(int, input().split())
    ans = B-A+1
    gcd_CD = gcd(C, D)
    CD = (C//gcd_CD)*(D//gcd_CD)*gcd_CD
    ans -= (B//C-(A-1)//C)
    ans -= (B//D-(A-1)//D)
    ans += (B//CD-(A-1)//CD)
    print(ans)


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


solve()

Submission status: Accepted

Solution code:
from fractions import gcd
A,B,C,D = map(int,input().split())
def Num_div(num,div):
    return num//div
    
def NoDiv(num,div1,div2):
    return num-Num_div(num,div1)-Num_div(num,div2)+Num_div(num,div1*div2//gcd(div1,div2))
    
print(NoDiv(B,C,D)-NoDiv(A-1,C,D))

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce
from fractions import gcd
#from math import gcd
import sys
def I(): return int(input())
def Is(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(input())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = float('inf')


a, b, c, d = Is()
def calc(num, x, y):
    l = (x*y)//gcd(x, y)
    return num - num//x - num//y + num//l

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

Submission status: Accepted

Solution code:
a,b,c,d = map(int,input().split())
a -= 1
from fractions import gcd
divac = a // c
divad = a // d
divbc = b // c
divbd = b // d
divacd = a // (c*d//gcd(c,d))
divbcd = b // (c*d//gcd(c,d))
Acd = divac + divad - divacd
Bcd = divbc + divbd - divbcd
wareru = Bcd - Acd
ans = b-a-wareru
print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd

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

div_c = b//c - a//c
if a%c == 0: div_c += 1
div_d = b//d - a//d
if a%d == 0: div_d += 1
lcm_cd = c*d//gcd(c, d)
div_cd = b//lcm_cd - a//lcm_cd
if a%lcm_cd == 0: div_cd += 1
print(b - a + 1 - div_c - div_d + div_cd)

Submission status: Accepted

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

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

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

ans_b = B - B//C - B//D + B//G
ans_a = A - 1 - (A-1)//C - (A-1)//D + (A-1)//G

print(ans_b - ans_a)

Submission status: Accepted

Solution code:
from fractions import gcd
a,b,c,d=map(int,input().split())
startC=(a-1)//c*c+c
startD=(a-1)//d*d+d
n=b-a+1
clength=b-startC
dlength=b-startD
cdnum=0
if clength>=0:
    cdnum+=1+clength//c
if dlength>=0:
    cdnum+=1+dlength//d
cd=c*d//gcd(c,d)
startCD=(a-1)//cd*cd+cd
cdlength=b-startCD
if cdlength>=0:
    cdnum-=1+cdlength//cd
print(n-cdnum)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def printlist(lst, k='\n'): return k.join(list(map(str, lst)))
INF = float('inf')

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

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

def f2(n, C, D):
    return n - n//C - n//D + n//lcm(C, D)

def solve():
    a, b, c, d = MI()

    g = lcm(c, d)
    bb = b - (b // c) - (b // d) + (b // g)
    a = a - 1
    aa = a - (a // c) - (a // d) + (a // g)
    print(bb - aa)
    # print(f(b, c, d) - f(a-1, c, d))
    # print(f2(b, c, d) - f2(a-1, c, d))


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def printlist(lst, k='\n'): return k.join(list(map(str, lst)))
INF = float('inf')

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

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

def f2(n, C, D):
    return n - n//C - n//D + n//lcm(C, D)

def solve():
    a, b, c, d = MI()

    # g = lcm(c, d)
    # bb = b - (b // c) - (b // d) + (b // g)
    # a = a - 1
    # aa = a - (a // c) - (a // d) + (a // g)
    # print(bb - aa)
    print(f(b, c, d) - f(a-1, c, d))
    # print(f2(b, c, d) - f2(a-1, c, d))


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

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

c = B // C - (A - 1) // C 
d = B // D - (A - 1) // D 
e = B // E - (A - 1) // E
ans = B - A - c - d + e + 1
print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd

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

# 全体-（cで可+dで可-両方可）
ans += b-((b//c)+(b//d)-(b//lcm))
ans -= a-((a//c)+(a//d)-(a//lcm))

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 solve(n):
    return n - n//C - n//D + n//lcm(C, D)
print(solve(B) - solve(A-1))

Submission status: Accepted

Solution code:
# ABC131C

def euclid(m, n):
    m0 = m = abs(m)
    n0 = n = abs(n)
    while n != 0:
        m, n = n, m % n
    qm = m0 // m
    lcm = qm * n0
    return lcm


def check(a, b, c):
    x = a // c
    y = b // c
    if x * c < a:
        x += 1
    return y - x + 1

a, b, c, d = map(int, input().split())
nc = check(a, b, c)
nd = check(a, b, d)
ncd = check(a, b, euclid(c, d))
print(b - a + 1 -(nc + nd - ncd))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# C - Anti-Division

from fractions import gcd

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

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

total_C = B // C
total_D = B // D
total_CxD = B // CxD

sub_C = (A - 1) // C
sub_D = (A - 1) // D
sub_CxD = (A - 1) // CxD

print((B - (A - 1)) -
        (total_C + total_D - total_CxD) + (sub_C + sub_D - sub_CxD))

Submission status: Accepted

Solution code:
import sys
#from collections import defaultdict, deque, Counter
#from bisect import bisect_left
#import heapq
#import math
#import numpy as np

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

#n = int(stdin.readline().rstrip())
#l = list(map(int, stdin.readline().rstrip().split()))
a,b,c,d = map(int, stdin.readline().rstrip().split())
#S = [list(stdin.buffer.readline().decode().rstrip()) for _ in range(h)]

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

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


cd = lcm(c,d)

# b以下
cwari = b//c
dwari = b//d
cdwari = b//cd
ub = b-cwari-dwari+cdwari

# a以下
cwari = (a-1)//c
dwari = (a-1)//d
cdwari = (a-1)//cd
ua = (a-1)-cwari-dwari+cdwari

print(ub-ua)

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

Submission status: Accepted

Solution code:
def gcd(a, b):
  while b:
    a, b = b, a % b
  return a
def lcm(x, y):
  return (x * y) // gcd(x, y)
A, B, C, D = map(int, input().split(' '))
can_dived_c = B // C - (A - 1) // C
can_dived_d = B // D - (A - 1) // D
can_dived_both = B // lcm(C, D) - (A - 1) // lcm(C, D)
res = B - (A - 1) - can_dived_c - can_dived_d + can_dived_both
print(res)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a%b)

def main():
    A, B, C, D = map(int, input().split())
    g = gcd(max(C,D),min(C,D))
    l = C*D//g
    NB = B - B//C - B//D + B//l
    NA = (A-1) - (A-1)//C - (A-1)//D + (A-1)//l
    ans = NB - NA
    print(int(ans))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import fractions

a,b,c,d=[int(x) for x in input().split()]

def count(n):
  r=b//n
  l=a//n
  if a%n!=0:
     l+=1
  return r-l+1

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

print(ans)
#print(b-a+1,count(c),count(d),count(c*d//fractions.gcd(c,d)))

Submission status: Accepted

Solution code:
import fractions

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

Submission status: Accepted

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

# a以上b以下でnで割り切れる物の数
def f(n):
    return b//n - (a-1)//n
from fractions import gcd
lc = c*d//gcd(c,d)

print((b-a+1) - (f(c)+f(d)-f(lc)))

Submission status: Accepted

Solution code:
import fractions

a, b, c, d = map(int, input().split())
lcm_cd = (c * d) // fractions.gcd(c, d)
total = b - a + 1
d_c = b // c - (a - 1) // c
d_d = b // d - (a - 1) // d
d_cd = b // lcm_cd - (a - 1) // lcm_cd
print(int(total - (d_c + d_d - d_cd)))

Submission status: Accepted

Solution code:
a,b,c,d=map(int,input().split())
lcm=0
cc=min(c,d)
dd=max(c,d)

while cc>0:
  amari=dd%cc
  dd=cc
  cc=amari

lcm=c*d//dd
  
bika=b-b//c-b//d+b//lcm
amiman=a-1-(a-1)//c-(a-1)//d+(a-1)//lcm
print(bika-amiman)

Submission status: Accepted

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

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

def nonbaisu(n):
    return n-(n//c + n//d - n//lcm(c,d))
print(nonbaisu(b) - nonbaisu(a-1))