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 baisuu(a, b, n):
    return (b // n) - ((a-1) // n)

def gcd(d,c):
    if d > c:
        c, d = d, c
    
    x = c * d

    r = c % d
    while r != 0:
        c = d
        d = r
        r = c % d
    return x // d

def answer(a,b,c,d):
    cn = baisuu(a,b,c)
    dn = baisuu(a,b,d)
    cdn = baisuu(a,b,gcd(c,d))
    return (b-a+1) - (cn+dn-cdn)

print(answer(A,B,C,D))

Submission status: Accepted

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

sumC=0
if A%C==0:
  div = 0
else:
  div = -(A%C)+C
  
if A+div <= B:
  first = A+div
  sumC = Decimal(B-first)//Decimal(C) + 1
  
sumD=0
if A%D==0:
  div = 0
else:
  div = -(A%D)+D
if A+div <= B:
  first = A+div
  sumD = Decimal(B-first)//Decimal(D) + 1
  
sumCD=0
f=fractions.gcd(C,D)
f2=C*D//f
if A%f2==0:
  div = 0
else:
  div = -(A%f2)+f2
if A+div <= B:
  first = A+div
  sumCD = Decimal((B-first))//Decimal(f2) + 1
print(B-A+1-(sumC+sumD-sumCD))

Submission status: Accepted

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

def div(A,B,X):
    return B//X - (A-1)//X

N = b - a + 1
LCM = int(c * d / gcd(c, d))
print(N - div(a,b,c) - div(a,b,d) + div(a,b,LCM))

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())
count_c = (B // C) - ((A-1) // C)
count_d = (B // D) - ((A-1) // D)

cd = lcm(C, D)
count_cd = (B // cd) - ((A-1) // cd)
count_c_or_d = count_c + count_d - count_cd
count_all = B - A + 1
ans = B - A + 1 - count_c_or_d
print(ans)

Submission status: Accepted

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

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

x = 0
if A%C == 0:
    x += B//C - A//C + 1
else:
    x += B//C - A//C

if A%D == 0:
    x += B // D - A // D + 1
else:
    x += B // D - A // D

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

print(B-A-x+1)

Submission status: Accepted

Solution code:
a, b, c, d = [int(v) for v in input().split()]

def gcd(p, q):
    if q > p:
        p, q = q, p
    m = -1
    while m != 0:
        m = p % q
        p, q = q, m
    return p

a = a - 1
lcm = c * d // gcd(d, c)
dc = b//c - a//c
dd = b//d - a//d
dcd = b//lcm - a//lcm
divable = dc + dd - dcd
print(b - a - divable)

Submission status: Accepted

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

A,B,C,D = map(int, input().split())
if C > D:
  g = gcd(C,D)
else:
  g = gcd(D,C)
g = int(C*D/g)
ac = (A-1) // C
bc = B // C
ad = (A-1) // D
bd = B // D
ag = (A-1) // g
bg = B // g
print(int(B-A+1 -(bc-ac) -(bd-ad) +(bg-ag)) )

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)

A_C = A // C
A_D = A // D
A_CD = A // lcm(C, D)

B_C = B // C
B_D = B // D
B_CD = B // lcm(C, D)

A_e = A - (A_C + A_D - A_CD)
B_e = B - (B_C + B_D - B_CD)

if A % C == 0 or A % D == 0:
    ans = B_e - A_e
else:
    ans = B_e - A_e + 1
print(ans)

Submission status: Accepted

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

import sys
if sys.version_info.minor >= 5: from math import gcd
else: from fractions import gcd

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

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

A,B,C,D=MAP()

cntC=B//C-(A-1)//C
cntD=B//D-(A-1)//D
CD=lcm(C, D)
cntCD=B//CD-(A-1)//CD
AB=B-A+1
print(AB-cntC-cntD+cntCD)

Submission status: Accepted

Solution code:
def gcd(x, y):
    if x < y:
        x, y = y, x
    while x % y != 0:
        x, y = y, x % y
    return y
        
def lcm(x, y):
    return (x * y) // gcd(x, y)

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

c_b  = B//C
d_b  = B//D
cd_b = B//lcm(C,D)

c_a  = (A-1)//C
d_a  = (A-1)//D
cd_a = (A-1)//lcm(C,D)

ans = (B - c_b - d_b + cd_b) - (A - 1 - c_a - d_a + cd_a)



print(str(ans))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC131 C
import sys
import math

A,B,C,D = list(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)

sub = B - A
c = A
ans = B
ans -= B // C
ans -= B // D
ans += B // (lcm(C,D))
c -= A // C
c -= A // D
c += A // (lcm(C,D))
if A % C != 0 and A % D != 0:
    ans += 1
ans -= c
print(ans)

Submission status: Accepted

Solution code:
import fractions
a, b, c, d = map(int, input().split())
ac = (a-1)//c*c
bc = b//c*c
ccnt = (bc-ac)//c
ad = (a-1)//d*d
bd = b//d*d
dcnt = (bd-ad)//d
e = c*d//fractions.gcd(c,d)
ae = (a-1)//e*e
be = b//e*e
ecnt = (be-ae)//e
print(-a+b+1-ccnt-dcnt+ecnt)
#print(ccnt, dcnt, ecnt)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(4100000)
import fractions
a,b,c,d = [int(i) for i in input().split()]
lcm = (c * d) // fractions.gcd(c, d)
# a = 9-9//2-9//3+9//lcm
# b = 4-4//2-4//3+4//lcm
a -= 1
e = b-b//c-b//d+b//lcm
f = a-a//c-a//d+a//lcm
print(e-f)

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

Submission status: Accepted

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

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

total = B - A + 1
count_C = B//C - (A-1)//C 
count_D = B//D - (A-1)//D 
lcm = int(C * D / fractions.gcd(C, D))
count_CD = B//lcm - (A-1)//lcm

print(total - count_C - count_D + count_CD)

Submission status: Accepted

Solution code:
def main():
    a,b,c,d = map(int, input().split())
    ans = b-a+1
    cd = c*d
    g = gcd(c,d)
    if g != 0:
        cd //= g
    ans -= b//c-(a-1)//c
    ans -= b//d-(a-1)//d
    ans += b//cd-(a-1)//cd
    print(ans)

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from fractions import gcd
A,B,C,D = map(int, input().split())
Cs = B//C - A//C
if A%C == 0:
    Cs += 1
Ds = B//D - A//D
if A%D == 0:
    Ds += 1
lcm = C*D//gcd(C,D)
CandDs = B//lcm - A//lcm
if A%lcm == 0:
    CandDs += 1
ans = (B - A + 1) - Cs - Ds + CandDs
print(ans)

Submission status: Accepted

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

def gcd(m, n):
  while n:
    m, n = n, m%n
  return m

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

def f(n, m, l):
  a = n // l
  if l * a < n:
    a += 1
  b = m // l
  return b - a + 1

N = B - A + 1
print(N - (f(A, B, C) + f(A, B, D) - f(A, B, lcm(C, D))))

Submission status: Accepted

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

import sys
sys.dont_write_bytecode = True
stdin = sys.stdin
input = stdin.readline
sys.setrecursionlimit(10000000)

#import numpy as np
#import scipy as sp

# Python 3 modules: https://docs.python.org/3/library/

import math, itertools, operator
from math import *
from math import factorial as fact
from operator import itemgetter, attrgetter
from itertools import permutations, combinations
from functools import reduce, lru_cache
# Use scipy for stats: https://docs.scipy.org/doc/scipy/reference/stats.html
# Before Python 3.5.  Import from math after it.
from fractions import gcd
from fractions import Fraction as Frac

inf = float('inf')

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

def perm(a, b):
    import scipy.special
    return scipy.special.perm(a, b, exact = True)

def comb(a, b):
    import scipy.special
    return scipy.special.comb(a, b, exact = True)

def dfact(n):
    import scipy.special
    return scipy.special.factorial2(n, exact = True)

def prod(l):
    return reduce(operator.mul, l)

def input_ints(func = input, sep = None, conv = int):
    return list(map(conv, func().split(sep = sep)))

################################################################################


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

    # // excludes 0
    s0 = (A-1) // C
    s1 = (B-0) // C
    t0 = (A-1) // D
    t1 = (B-0) // D
    u0 = (A-1) // lcm(C,D)
    u1 = (B-0) // lcm(C,D)
    s = s1 - s0
    t = t1 - t0
    u = u1 - u0
    c = s + t - u
    c = B - A + 1 - c
    print(c)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from sys import stdin
import math

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

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

def xxx(x,c,d):
    return x - x//c - x//d + x//lcm(c,d)

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

ans = 0

aa = xxx(a-1,c,d)
bb = xxx(b,c,d)
ans = bb - aa
print(ans)

Submission status: Accepted

Solution code:
def gcd(a,b):
    a,b=max(a,b),min(a,b)
    if a%b==0:
        return b
    else:
        while a%b!=0:
            c=a%b
            a=b
            b=c
        return c

[A,B,C,D]=[int(_) for _ in input().split()]
E=C*D//gcd(C,D)
X=(B//C)-((A-1)//C)
Y=(B//D)-((A-1)//D)
Z=(B//E)-((A-1)//E)
N=B-A+1
print(N-(X+Y-Z))

Submission status: Accepted

Solution code:
def gcd(x, y):
    if y == 0:
        return x
    if x < y:
        return gcd(y, x)
    return gcd(y, x%y)

def fai(n, x, y):
    g = gcd(x, y)
    l = x * (y//g)
    return n - (n//x) - (n//y) + (n//l)

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

alpha = 0
if a%c != 0 and a%d != 0:
    alpha = 1

print(fai(b, c, d) - fai(a, c, d) + alpha)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline


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


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

def calc_num(X, C, D, E):
    return X - (X/C + X/D - X/E)

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [A, B, C, D] = [long(input_raw[0]), long(input_raw[1]), long(input_raw[2]), long(input_raw[3])]
    E = lcm(C, D)
    print calc_num(B, C, D, E)  - calc_num(A - 1, C, D, E)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#a,bの最大公約数
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)
def main():
    A, B, C, D = [int(n) for n in input().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)
    pro = lcm(C, D)
    min_pro = A // (pro) if A % (C*D) == 0 else (A // (pro))+1
    max_pro = B // (pro) if B % (C*D) == 0 else (B // (pro))
    

    print((B-A+1) - (max_c-min_c+1) - (max_d-min_d+1) + (max_pro-min_pro + 1))
main()

Submission status: Accepted

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

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

n_a_lcm = a // lcm
n_b_lcm = b // lcm

n_a_c = a // c
n_b_c = b // c

n_a_d = a // d
n_b_d = b // d


n_lcm = n_b_lcm - n_a_lcm
n_c = n_b_c - n_a_c
n_d = n_b_d - n_a_d


n = b - a - n_c - n_d + n_lcm

if a % c != 0 and a % d != 0:
    n += 1

print(n)

Submission status: Accepted

Solution code:
import fractions
na = lambda: list(map(int, input().split()))
A,B,C,D = na()
ee = (C * D)//fractions.gcd(C, D)
A = A-1
cc = B//C
dd = B//D
ansb = cc+dd-B//ee
ansa = A//C+A//D-A//ee
print(B-A-(ansb-ansa))

Submission status: Accepted

Solution code:
import fractions

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

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

Submission status: Accepted

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

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

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

print(B-A+1-ans)

Submission status: Accepted

Solution code:
def gcd(a: int, b: int) -> int:
    """
    ユークリッドの互除法を用いて二数の最大公約数を求める。
    (関数名は"greatest common divisor"の略。)

    Parameters
    ----------
    a, b : 最大公約数を求めたい数。

    Returns
    -------
    b : 最大公約数
    """
    while a % b != 0:
        r = a % b
        a = b
        b = r
    return b


def lcm(a: int, b: int) -> int:
    """
    gcd関数を用いて二数の最小公倍数を求める。
    (関数名は"least common multiple"の略。)

    Parameters
    ----------
    a, b : 最小公倍数を求めたい数。

    Returns
    -------
    最小公倍数を返す。
    """
    return int(a * b / gcd(a, b))


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

div = lcm(C, D)
ct_1 = B // div - A // div
ct_1 += 0 if A % div else 1

div = C
ct_2 = B // div - A // div
ct_2 += 0 if A % div else 1

div = D
ct_3 = B // div - A // div
ct_3 += 0 if A % div else 1

ct_sum = ct_2 + ct_3 - ct_1

ans = B - A + 1 - ct_sum

print(ans)

Submission status: Accepted

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

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

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

y = b//d - (a - 1)//d

cd = lcm(c, d)

z = b//cd - (a - 1)//cd

ans = b - a + 1 - x - y + z
print(ans)

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)


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

M = lcm(C, D)
bc, bd, bm = B // C, B // D, B // M
ac, ad, am = (A - 1) // C, (A - 1) // D, (A - 1) // M

# print(M)
# print(bc, bd, bm)
# print(ac, ad, am)

c = bc - ac
d = bd - ad
m = bm - am
n = B - A + 1

ans = n - (c + d - m)

print(ans)

Submission status: Accepted

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

def calc(N):
    return B//N - (A-1)//N

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

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

print(B - A + 1 - calc(C) - calc(D) + calc(lcm(C, D)))

Submission status: Accepted

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

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

numc,numd,numlcm = 0,0,0
if a%c == 0:
    numc = b//c - a //c + 1
else:
    numc = b//c - a //c

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

if a%lcm == 0:
    numlcm = b//lcm - a //lcm + 1
else:
    numlcm = b//lcm - a //lcm

print(b-a+1-numc-numd+numlcm)

Submission status: Accepted

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

cy=(a-1)//c
dy=(a-1)//d
ey=(a-1)//e


print(b-cx-dx+ex -a+cy+dy-ey+1)

Submission status: Accepted

Solution code:
from fractions import gcd

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

a = (B-A+1)
b = -(B//C + B//D - B//(C*D//gcd(C,D)))
c = ((A-1)//C + (A-1)//D - (A-1)//(C*D//gcd(C,D)))

print(a+b+c)

Submission status: Accepted

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

# BまででCで割り切れる数
b1 = B // C
b2 = B // D
b3 = B // (C * D // fractions.gcd(C, D))

a1 = A // C
a2 = A // D
a3 = A // (C * D // fractions.gcd(C, D))

bb = B - b1 - b2 + b3
aa = A - a1 - a2 + a3

# Aが対象なら1を足す
if A % C != 0 and A % D != 0:
    print(bb-aa + 1)
else:
    print(bb-aa)

Submission status: Accepted

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

oc = c
od = d

for i in range(d):
    c, d = d, c%d
    if d == 0:
        gcd = c
        break
    else:
        pass
c = oc
d = od

lcm = c * d // gcd


b_c = b // c
b_d = b // d
b_l = b // lcm

a_c = (a-1) // c
a_d = (a-1) // d
a_l = (a-1) // lcm

sum = (b_c + b_d - b_l) - (a_c + a_d - a_l)
print(b - a + 1 - sum)

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 s(i):
    if a % i == 0:
        t = b // i - a // i + 1
    else:
        t = b // i - a // i
    return t


if __name__ == "__main__":
    n = b - a + 1 - (s(c) + s(d) - s(lcm(c, d)))
    print(n)

Submission status: Accepted

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


a,b,c,d=map(int,input().split())
a-=1
ans=b
t=lcm(c,d)
ans-=b//c-a//c
ans-=b//d-a//d
ans+=b//t-a//t
print(ans-a)

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)

A_C = (A - 1)// C
A_D = (A - 1) // D
A_CD = (A - 1) // lcm(C, D)
a = A_C + A_D - A_CD
B_C = B // C
B_D = B // D
B_CD = B // lcm(C, D)
b = B_C + B_D - B_CD

print(B - A + 1 - (b - a))

Submission status: Accepted

Solution code:
import fractions


def cnt(A, B, d):
    return B // d - (A - 1) // d


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

Submission status: Accepted

Solution code:
import fractions

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

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

div_1A_C = (A-1)//C
div_1B_C = B//C
div_AB_C = div_1B_C-div_1A_C

div_1A_D = (A-1)//D
div_1B_D = B//D
div_AB_D = div_1B_D-div_1A_D

div_1A_CD = (A-1)//lcm_CD
div_1B_CD = B//lcm_CD
div_AB_CD = div_1B_CD-div_1A_CD

print(all_AB-div_AB_C-div_AB_D+div_AB_CD)

Submission status: Accepted

Solution code:
import fractions as f

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

c = B // C - A // C
d = B // D - A // D
l = B // L - A // L

if A % C == 0:  c += 1
if A % D == 0:  d += 1
if A % L == 0:  l += 1

print( int( (2*(B-A+1) - (c+d) - (c+d-2*l)) // 2 ) )

Submission status: Accepted

Solution code:
import fractions

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

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

# 最小公倍数
c_d_gcd = lcm(c, d)

divable_c = b // c - (a - 1) // c
divable_d = b // d - (a - 1) // d
divable_gcd = b // c_d_gcd - (a - 1) // c_d_gcd

result = (b - a + 1) - divable_c - divable_d + divable_gcd

print(result)

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)

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

tmp1 = c + d - cd

c = B // C
d = B // D
cd = B // lcm(C, D)

tmp2 = c + d -cd

print((B - A + 1) - (tmp2 - tmp1))

Submission status: Accepted

Solution code:
from fractions import gcd

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

def prime_cd(n,c,d):
    c_div=n//c
    d_div=n//d
    lcm=c*d//gcd(c,d)
    cd_div=n//lcm
    return n-c_div-d_div+cd_div

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

Submission status: Accepted

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

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

# B以下の整数で、CでもDでも割り切れないものの個数
ans_B = B - B//C - B//D + B//(C*D//gcd(C, D))
# A-1以下の整数で、CでもDでも割り切れないものの個数
ans_A = (A-1) - (A-1)//C - (A-1)//D + (A-1)//(C*D//gcd(C, D))

print(ans_B - ans_A)

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 = (int(i) for i in input().split())

hogeb = B // D
hogec = B // C
hogebc = B // lcm(D,C)

hoge = B-(hogeb + hogec -hogebc)

ononb = (A-1) // D
ononc = (A-1) // C
ononbc = (A-1) // lcm(D,C)

onon = A-1-(ononb + ononc - ononbc)

print(hoge-onon)

Submission status: Accepted

Solution code:
def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]

# n = int(input())
# a, b
# s = input()


# 最小公倍数
# ユークリッドの互除法
def lcm(a, b): 
    def gcd(a, b):  # a >= b
        if b == 0:
            return a
        else:
            return gcd(b, a % b)
    
    return a * b // gcd(a, b)



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

E = lcm(C, D)

l = A - (A // C + A // D - A // E)
r = B - (B // C + B // D - B // E)

ans = r - l
print(ans)

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

print(ans)

Submission status: Accepted

Solution code:
import fractions

def solve():
    a,b,c,d = map(int,input().split())
    a_c = a // c
    b_c = b // c
    a_d = a // d
    b_d = b // d
    a_cd = a // (c * d // fractions.gcd(c,d))
    b_cd = b // (c * d // fractions.gcd(c,d))

    divc = b_c - a_c
    divd = b_d - a_d
    divcd = b_cd - a_cd
    
    if a % c == 0:
        divc += 1
    if a % d == 0:
        divd += 1
    if a % (c * d // fractions.gcd(c,d)) == 0:
        divcd += 1

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

    return 0
  
solve()

Submission status: Accepted

Solution code:
import fractions

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

C_count = B // C - (A - 1) // C
D_count = B // D - (A - 1) // D
CD = C * D // fractions.gcd(C, D)
CD_count = B // CD - (A - 1) // CD
# print(C_count, D_count, CD_count)
print(B - A + 1 + CD_count - C_count - D_count)

Submission status: Accepted

Solution code:
import fractions
A, B, C, D = tuple([int(x) for x in input().split(' ')])

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

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

cd = B // x - A // x

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

Submission status: Accepted

Solution code:
from fractions import gcd

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

def devisor(x):
    n1 = A//x
    n2 = B//x
    cnt = n2 - n1
    if A%x == 0:
        cnt += 1
    return cnt

num_all = B-A+1
num_c = devisor(C)
num_d = devisor(D)

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

num_cd = devisor(lcm_cd)
 
print(num_all - num_c - num_d + num_cd)

Submission status: Accepted

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

cd = int(c*d/gcd(c,d))
numcd = (b//cd)-((a-1)//cd)

print(b-a+1-numc-numd+numcd)

Submission status: Accepted

Solution code:
def gcd(x,y):
	x, y = max(x,y), min(x,y)
	while(y):
		x, y = y, x%y
	return x
A,B,C,D = map(int,input().split())
A -= 1
CD = C*D // gcd(C,D)
a = A - (A//C + A//D - A//CD)
b = B - (B//C + B//D - B//CD)
print(b-a)

Submission status: Accepted

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

def gcd(x, y):

    if x <= y:
        if y % x == 0:
            return x
        else:
            return gcd(y % x, x)
    else:
        if x % y == 0:
            return y
        else:
            return gcd(x % y, y)

    return 0

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

e = lcm(c, d)

ans = (b - a + 1) - (((b // c) + (b // d) - (b // e)) - (((a - 1) // c) + ((a - 1) // d) - ((a - 1) // e)))

print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd
from functools import reduce
def gcds(*numbers):
    return reduce(gcd, numbers)

def resolve():
    (A, B, C, D) = list(map(int, input().split(" ")))
    
    xC = (B//C) - ((A-1)//C)
    xD = (B//D) - ((A-1)//D)
    CD = int(C * D / gcds(C, D))
    xCD = (B//CD) - ((A-1)//CD)
    ALL = B - A + 1
    print(ALL - xC - xD + xCD)


if '__main__' == __name__:
    resolve()

Submission status: Accepted

Solution code:
import fractions

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

f=fractions.gcd(c,d)
f2=c*d//f

if a%c ==0:
  c_shita=a//c
else:
  c_shita=a//c +1
  
c_num=b//c-c_shita+1

#print(c_num)  
  
if a%d ==0:
  d_shita=a//d
else:
  d_shita=a//d +1
  
d_num=b//d-d_shita+1

#print(d_num)  

if a%f2 ==0:
  f2_shita=a//f2
else:
  f2_shita=a//f2 +1
  
f2_num=b//f2-f2_shita+1

#print(f2)
#print(f2_num)  
  
ans=b-a+1 -c_num -d_num +f2_num
print(ans)

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
ac = a // c
ad = a // d
acd = a // cd
bc = b // c
bd = b // d
bcd = b // cd

ans = (b - a) - (bc + bd - bcd - (ac + ad - acd))
print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:

import fractions
from functools import reduce

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

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

x=[C,D]

e=C*D//gcd_list(x)

cc=B%C
ccc=A%C
CB=B-cc
if ccc==0:
    key1=((CB-A)//C+1)
else:
    key1=((CB-A)//C+1)
#print(key1)

dd=B%D
ddd=A%D
DB=B-dd
key2=((DB-A)//D+1)
#print(key2)
ee=B%e
eee=A%e
eb=B-ee
key3=((eb-A)//e+1)
#print(key3)
print(B-A+1-(key1+key2-key3))

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

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

X1 = A - ((A // C) + (A // D) - (A // lcm(C, D)))
X2 = B - ((B // C) + (B // D) - (B // lcm(C, D)))
print(X2 - X1)

Submission status: Accepted

Solution code:
from fractions import gcd

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

def main():
    a,b,c,d=map(int,input().split())
    num=b-a+1
    moda1=(a-1)//c
    moda2=(a-1)//d
    moda3=(a-1)//(lcm(c,d))
    modat=moda1+moda2-moda3
    modb1=b//c
    modb2=b//d
    modb3=b//(lcm(c,d))
    modbt=modb1+modb2-modb3
    mod=modbt-modat
    return num-mod

print(main())

Submission status: Accepted

Solution code:


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

    return a, b, c, d

# baseの倍数がfrom_n ~ to_nにいくつあるか数える
def count_multiples(from_n, to_n, base):
    # from_nより大きい最小の倍数
    if from_n % base == 0:
        a = from_n // base
    else:
        a = from_n // base + 1
    left = a

    # to_nより小さい最大の倍数
    a = to_n // base
    right = a

    if left > right:
        return 0

    return right - left + 1

def euclidian(c, d):
    if c > d:
        p = c
        q = d
    else:
        p = d
        q = c

    while q:
        r = p % q
        p = q
        q = r

    return p


def submit():
    a, b, c, d = read_input()

    all_n = b - a + 1

    # cで割り切れる数
    c_div = count_multiples(a, b, c)

    # dで割り切れる数
    d_div = count_multiples(a, b, d)

    # cdの最小公倍数で割り切れる数
    min_mul = c * d // euclidian(c, d)
    cd_div = count_multiples(a, b, min_mul)


    print(all_n - c_div - d_div + cd_div)


if __name__ == '__main__':
    submit()

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)


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

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

print(ans)

Submission status: Accepted

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


a, b, c, d = map(int, input().split())
e = c * d // GCD(c, d)
f = b // c - (a - 1) // c
g = b // d - (a - 1) // d
h = b // e - (a - 1) // e
ans = b - a + 1 - (f + g - h)
print(ans)

Submission status: Accepted

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

def gcd(C,D):
    while D != 0:
        C,D = D,C%D
    return C

def lcm(C,D):
    return C*D//gcd(C,D)

CD_lcm = lcm(C,D)

C_A = (A-1) // C
C_B = B // C
div_C = C_B - C_A

D_A = (A-1) // D
D_B = B // D
div_D = D_B - D_A

lcm_A = (A-1) // CD_lcm
lcm_B = B // CD_lcm
div_lcm = lcm_B - lcm_A

print(B-A-(div_C+div_D-div_lcm)+1)

Submission status: Accepted

Solution code:

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


def gcd(arr):
    g = gcd_core(arr[0], arr[1])
    for i in range(2, len(arr)):
        g = gcd_core(g, arr[i])
    return g

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

def count(n,c,d):
    cn = n//c
    dn = n//d
    lcm = c*d//(gcd_core(c,d))
    both = n // lcm
    if lcm == c:
        # print("1")
        # print(cn, dn, both)
        return dn
    elif lcm == d:
        # print("2")
        # print(cn, dn, both)
        return cn
    else:
        # print("3")
        # print(cn,dn,both)
        return cn+dn-both

l = a-1-count(a-1,c,d)
r = b-count(b,c,d)
# print(l)
# print(r)

print(r-l)

Submission status: Accepted

Solution code:
import sys, itertools, collections, fractions
sys.setrecursionlimit(10**7)
def lmi(): return list(map(int, input().split()))

a, b, c, d = lmi()
p = b//c - (a-1)//c
q = b//d - (a-1)//d
g = c * d // fractions.gcd(c,d)
u = b//g - (a-1)//g
# print(p,q,u,g)
print(b-a+1-p-q+u)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
a,b,c,d=map(int,input().split())
a-=1
bIka=b-b//c-b//d+b//(c*d//gcd(c,d))
aMiman=a-a//c-a//d+a//(c*d//gcd(c,d))
print(bIka-aMiman)

Submission status: Accepted

Solution code:
#
import sys
import math
import numpy as np
import itertools

def gcd(m,n):
    p=max(m,n)
    q=min(m,n)
    if p % q == 0:
        return q
    else:
        return gcd(q,p%q)



# いくつか入力
a,b,c,d = (int(i) for i in input().split())  

x = b // c - (a-1) // c
y = b // d - (a-1) // d
e = c * d // gcd(c,d)
z = b // e - (a-1) // e

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

Submission status: Accepted

Solution code:
import fractions

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

C_div = B//C - (A-1)//C
D_div = B//D - (A-1)//D
CandD = int(C*D/fractions.gcd(C, D))
CandD_div = B//CandD - (A-1)//CandD

print(B-(A-1)-C_div-D_div+CandD_div)