Problem:


Score : 300 points

Problem Statement

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

Constraints

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

1\leq C,D\leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

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

Sample Input 1

4 9 2 3

Sample Output 1

2

5 and 7 satisfy the condition.

Sample Input 2

10 40 6 8

Sample Output 2

23

Sample Input 3

314159265358979323 846264338327950288 419716939 937510582

Sample Output 3

532105071133627368



Sample input:
4 9 2 3


Sample output:
2

Submission status: Wrong Answer

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

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

def lcm(m,n):
    return m*n/gcd(m,n)
def f2(temp3):
    return (int(B/temp3)-int((A-1)/temp3))

temp=lcm(C,D)

print((B-A+1)-(f2(C)+f2(D)-f2(temp)))

Submission status: Wrong Answer

Solution code:
#S=input()
import fractions

lis=list(map(int,input().split()))
gCD=fractions.gcd(lis[2],lis[3])
CD=int(lis[2]*lis[3]/gCD)
AmodC=lis[0]%lis[2]
if AmodC==0:
    AC=lis[0]
else:
    AC=lis[0]+(lis[2]-AmodC)
AmodD=lis[0]%lis[3]
if AmodD==0:
    AD=lis[0]
else:
    AD=lis[0]+(lis[3]-AmodD)
BC=lis[1]-lis[1]%lis[2]
BD=lis[1]-lis[1]%lis[3]
AmodCD=lis[0]%CD
if AmodCD==0:
    ACD=lis[0]
else:
    ACD=lis[0]+(CD-AmodCD)
BCD=lis[1]-lis[1]%CD
ABC=(BC-AC)/lis[2]+1
ABD=(BD-AD)/lis[3]+1
ABCD=(BCD-ACD)/CD+1
ans=lis[1]-lis[0]+1-int(ABC+ABD-ABCD)

print(ans)
#print(AC,AD,BC,BD,ACD,BCD,ABC,ABD,ABCD)

Submission status: Wrong Answer

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


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

#lst = [i for i in range(a, b + 1)]

n = abs((b // c) - ((a - 1) // c))
m = abs((b // d) - ((a - 1) // d))
cd = c * d
l = abs((b // cd) - ((a - 1) // cd))

g_cd = gcd(c, d)
if g_cd != 1:
    l = l + 1


nml = n + m - l

print(n, m, l, nml)

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

Submission status: Wrong Answer

Solution code:
#https://atcoder.jp/contests/abc131/tasks/abc131_c

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()]
C = 0
D = 0
E = 0
'''
for i in range(int(a / max(c,d,lcm(c,d))),b-a):
    print(i)
    if c * i >= a and c * i <= b and C == 0:
        C = c * i
    if d * i >= a and d * i <= b and D == 0:
        D = d * i
    if lcm(c,d) * i >= a and lcm(c,d) * i <= b and E == 0:
        E = lcm(c,d) * i
    if C != 0 and D != 0 and E != 0:
        break
'''
for i in range(int(a / lcm(c,d)) - 1,int(b / lcm(c,d))+2):
    if lcm(c,d) * i >= a and lcm(c,d) * i <= b and E == 0:
        E = lcm(c,d) * i
        break

for i in range(int(a / c) - 1,int(b / c)+2):
    if c * i >= a and c * i <= b and C == 0:
        C = c * i
        break

for i in range(int(a / d) - 1,int(b / d)+2):
    if d * i >= a and d * i <= b and D == 0:
        D = d * i
        break
#print(lcm(c,d))
#print('')
#print(C,D,E)
c2 = int(b / c) - int(C / c)+1
d2 = int(b / d) - int(D / d)+1
e2 = int(b / lcm(c,d)) - int(E / lcm(c,d))+1
if E == 0:
	e2 = 0
if C == 0:
	c2 = 0
if D == 0:
	d2 = 0

#print(b-a+1,c2,d2,e2)
'''
c1 = list(range(C,b+1,c))
d1 = list(range(D,b+1,d))
e1 = list(range(E,b+1,lcm(c,d)))
print(b-a+1,len(c1),len(d1),len(e1))

print(c1,d1,e1)

print(b-a+1-len(c1)-len(d1)+len(e1))
'''
print(int(b-a+1 - c2 - d2 + e2))

Submission status: Wrong Answer

Solution code:
import fractions
import math


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


abcd = input().split(" ")
a = int(abcd[0])
b = int(abcd[1])
c = int(abcd[2])
d = int(abcd[3])
cd = lcm(c, d)
print(b-a-(math.ceil(b//c)-math.ceil(a/c))-(math.ceil(b//d)-math.ceil(a/d))+(math.ceil(b//cd)-math.ceil(a/cd)))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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
from operator import itemgetter, mul
from copy import deepcopy,copy
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

def main():
    a, b, c, d = MAP()
    g=gcd(c,d)
    l=int(c*d/g)
    a -= 1

    kukan1 = b - int(b / c) - int(b / d) + int(b / l)
    kukan2 = a - int(a / c) - int(a / d) + int(a / l)

    print(kukan1 - kukan2)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

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

result = 0

if A%C == 0 or A%D == 0:
    A += 1

result += (B-A+1) - (B//C - A//C) - (B//D - A//D)
l = lcm(C, D)
result += (B//l - A//l)

print("{}".format(result))

Submission status: Wrong Answer

Solution code:
a,b,c,d=map(int,input().split())
import math
import fractions
nc=math.floor((b-a)/c)
nd=math.floor((b-a)/d)
lcd=c*d//fractions.gcd(c,d)
ncd=math.floor((b-a)/lcd)
sum=nc+nd-ncd
print(b-a-sum)

Submission status: Wrong Answer

Solution code:
import sys
import fractions
A, B, C, D = map(int, sys.stdin.readline().split())
multi = C * D
multiGcd = int((C * D) // fractions.gcd(C, D))

nC = int((A - 1) // C)
nD = int((A - 1) // D)
mC = int(B // C)
mD = int(B // D)

nM = int((A - 1) // multi)
mM = int(B // multi)

wC = mC - nC
wD = mD - nD
wM = mM - nM
wCD = wC + wD - wM

nGcd = int((A - 1) // multiGcd)
mGcd = int(B // multiGcd)
wGcD = mGcd - nGcd

ans = B - (A - 1) - wCD
if multiGcd != multi:
    ans += wGcD
print(ans)

Submission status: Wrong Answer

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

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)
l = gcd(c,d)
if c == 1 or d == 1:
  print(0)
  exit()
if a == b:
  if a % c == 0 or a % d == 0:
    print(0)
    exit()
  else:
    print(1)
    exit()

if b < c and b < d:
  print(b-a + 1)
  exit()    
if l == 1:
  x =c * d
  if a == 1:
    print(int(b-a+1 - (b//c - (a)//c + b//d - (a) // d) + (b//x - (a)//x)))
    exit()
  print(int( b-a+1 - (b//c - (a-1)//c + b//d - (a-1) // d) + (b//x - (a-1)//x)))
  
  exit()
else:
  x = (c * d/l)
  if a == 1:
    print(int(b-a+1 - (b//c - (a)//c + b//d - (a) // d) + (b//x - (a)//x)))
    exit()
  print(int( b-a+1 - (b//c - (a-1)//c + b//d - (a-1) // d) + (b//x - (a-1)//x)))

Submission status: Wrong Answer

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

gcd = c
t = d
while t:
    gcd, t = t, gcd % t

lcm = c * d // gcd
e = b + 1 - a

dif += b // c - (a - 1) // c
dif += b // d - (a - 1) // d
if c != d:
    plus += b // lcm - (a -1) // lcm

print(e - dif + plus)

Submission status: Wrong Answer

Solution code:
import fractions
A,B,C,D = map(int, input().split())
x = abs(A-B)//C + 1
y = abs(A-B)//D + 1
if B%((C*D)//fractions.gcd(C,D)) > B-A:
    z = 0
else:
    z = abs(A-B)//((C*D)//fractions.gcd(C,D)) + 1
print(B-A+1-x-y+z)

Submission status: Wrong Answer

Solution code:
from fractions import gcd

a,b,c,d=map(int, input().split())
tisai = c*d/gcd(c,d)
n=[c,d]
count=0
for k in range(2):
    count+= ((b+1)//n[k])-(a//n[k])
    # print(count)
count-= ((b+1)//tisai)-(a//tisai)
# print(count)
print(int((b-a+1)-count))

Submission status: Wrong Answer

Solution code:
#c.py
def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    c = a % b
    return gcd(b, c)
def lcm(a, b):
    return a * b / gcd(a,b)
a,b,c,d = map(int,input().split())
canC = int(b//c) -int((a-1)//c)
canD = int(b//d) - int((a-1)//d)
dbCD = int(b//lcm(c,d)) -int((a-1)//lcm(c,d))
print((b-a+1) -canC - canD + dbCD)

Submission status: Wrong Answer

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

def getAns(a, b, c):
  if a % c == 0:
    start = a
  else:
    start = a + c - (a % c)
  if b % c == 0:
    end = b
  else:
    end = b - c + (b % c)
  num_c = max((end - start) // c + 1, 0)
  return num_c

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

# cについて
num_c =getAns(a,b,c)
#print(num_c)

# dについて
num_d =getAns(a,b,d)
#print(num_d)

# cとdの公倍数について
cd = c * d // gcd(c,d)
num_cd =getAns(a,b,cd)
#print(num_cd)

print(b - a - (num_c + num_d - num_cd))

Submission status: Wrong Answer

Solution code:
from math import ceil

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

A,B,C,D=map(int,input().split())
__C = ceil((B-A)/C)
__D = ceil((B-A)/D)
min_mul = lcm(C,D)
B__CD = ceil(B//min_mul)
A__CD = ceil(A//min_mul)
C__D = B__CD - A__CD
print( (B-A+1) - (__C+__D) + C__D )

Submission status: Wrong Answer

Solution code:
import math
A, B, C, D = map(int, input().split())
N = B - A + 1  #整数列の長さ

if C == D:
    tmp = 0   #最後にNから引く
    mod = A % C
    tmp += math.ceil((N-mod)/C)
    print(N-tmp)
else:
    tmp = 0   #最後にNから引く
    mod = A % C
    tmp += math.ceil((N-mod)/C)
    mod = A % D
    tmp += math.ceil((N-mod)/D)
    mod = A % (D*C)
    tmp -= math.ceil((N-mod)/(D*C))
    print(N-tmp)

Submission status: Wrong Answer

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=input().split()
a=int(a)
b=int(b)
c=int(c)
d=int(d)
x=a+a%c
y=a+a%d
e=lcm(c,d)
z=a+a%e
x2=(b-x)//c+1
y2=(b-y)//d+1
z2=(b-z)//e+1
if c>b and d>b:
    print(b-a+1)
elif d>b:
    print(b-a+1-x2)
elif c>b:
    print(b-a+1-y2)
elif e>b:
    print(b-a-x2-y2+1)
else:
    print(b-a+1-x2-y2+z2)

Submission status: Wrong Answer

Solution code:
a,b,c,d=map(int, input().split())
sub=b-a+1
tmpc=sub//c 
tmpd=sub//d 
import fractions
def lcm(x,y):
    return (x*y) // fractions.gcd(x,y)
tmplcm=sub//lcm(c,d)
ans = sub-(tmpc+tmpd-tmplcm)
if a%c!=0 and a%d!=0: ans-=1
print(ans)

Submission status: Wrong Answer

Solution code:
lista = input().split(" ")
a, b, c, d = lista
a = int(a)
b = int(b)
c = int(c)
d = int(d)
p1=1
p2=0
p3=0
p4=0
#minc
a1=a
a2=a
b1=b
b2=b
while(a1<=b):
    if(a1%c==0):
        p1=a1
        break
    a1+=1
#maxc
while (b1 >= a):
    if (b1 % c==0):
        p3 = b1
        break
    b1 -= 1
#mind

while(a2<=b):
    if(a2%d==0):
        p2=a2
        break
    a2+= 1
#maxd
while(b2>=a):
    if(b2%d==0):
        p4=b2
        break
    b2 -= 1
mc=((p3-p1)/c)
md=((p4-p2)/d)
print((b-a)-int(md+mc))

Submission status: Wrong Answer

Solution code:
import numpy as np
import fractions

def main():
    abcd = list(map(int, input().split(' ')))
    al = abcd[1] - abcd[0] + 1
    cc = abcd[1] // abcd[2] - abcd[0] // abcd[2]
    dd = abcd[1] // abcd[3] - abcd[0] // abcd[3]
    skbs = (abcd[2] * abcd[3]) / fractions.gcd(abcd[2], abcd[3])
    dis = cc + dd - (abcd[1] // skbs - abcd[0] // skbs)
    print(int(al - dis))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

def main():
    A,B,C,D=input().split()
    A=int(A)
    B=int(B)
    C=int(C)
    D=int(D)
    if A%C!=0:
        C_min=A-A%C+C
    else:
        C_min=A
    C_max=B-B%C
    if A%D!=0:
        D_min=A-A%D+D
    else:
        D_min=A
    D_max=B-B%D
    if A%(C*D/gcd(C,D))!=0:
        CD_min=A-A%(C*D/gcd(C,D))+(C*D/gcd(C,D))
    else:
        CD_min=A
    CD_max=B-B%(C*D/gcd(C,D))
    C_sum=(C_max-C_min)/C+1
    D_sum=(D_max-D_min)/D+1
    CD_sum=(CD_max-CD_min)/(C*D/gcd(C,D))+1
    sum= B-A+1-(C_sum+D_sum-CD_sum)
    return sum

sum=main()
sum_down=sum-sum%(10**8)
if (int(sum_down/10**8))!=0:
    print(str(int(sum_down/10**8))+str(int(sum%(10**8))))
else:
    print(str(int(sum%(10**8))))

Submission status: Wrong Answer

Solution code:
import fractions
import math

A,B,C,D = map(int,input().split())
lcm = C*D/fractions.gcd(C,D)
def c_in_a2b(c):
    return (math.floor(B/c) - math.ceil(A/c) + 1)
print(int((B-A+1)- c_in_a2b(C)-c_in_a2b(D)+c_in_a2b(lcm)))

Submission status: Wrong Answer

Solution code:
import fractions

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

def custom_round(number, ndigits=0):
    if type(number) == int:
        return number
    d_point = len(str(number).split('.')[1])
    if ndigits >= d_point:
        return number
    c = (10 ** d_point) * 2
    return round((number * c + 1) / c, ndigits)

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

x =int(b/c)-int((a/c))
y =int(b/d)-int((a/d))
n =lcm(c,d)
xy=int(b/n)-int((a/n))

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

Submission status: Wrong Answer

Solution code:
import fractions

num = input().split()
a, b, c, d = int(num[0])-1, int(num[1]), int(num[2]), int(num[3])

lcd = c*d/fractions.gcd(c, d)
warikire = (b//c-a//c)+(b//d-a//d)-(b//lcd-a//lcd)

print(int(b-a-warikire))

Submission status: Wrong Answer

Solution code:
import fractions

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

s = B - A + 1

if A % C == 0 or A % D == 0:
    A -= 1

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

E = lcm(C, D)

val1 = A // C
val2 = B // C
val3 = A // D
val4 = B // D
val5 = A // E
val6 = B // E

print(s - (val2 - val1 + val4 - val3 - (val6 - val5)))

print("val1 : " + str(val1))
print("val2 : " + str(val2))
print("val3 : " + str(val3))
print("val4 : " + str(val4))
print("val5 : " + str(val5))
print("val6 : " + str(val6))

Submission status: Wrong Answer

Solution code:
import fractions

parents = list(map(int,input().split(' ')))
All = parents[1] - parents[0]
min_2 = (parents[2]*parents[3]) // fractions.gcd(parents[2], parents[3])

All = All-All//parents[2]-All//parents[3]+All//min_2
if parents[2]>=parents[0] and parents[2]<=parents[1]:
    All = All-1
if parents[3]>=parents[0] and parents[3]<=parents[1]:
    All = All-1

if All<0:
    All = 0
print(All)

Submission status: Wrong Answer

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

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

if a == b:
  if a % c == 0 or a % d == 0:
    print(0)
    exit()
  else:
    print(1)
    exit()

if b < c and b < d:
  print(b-a + 1)
  exit()    
if l == 1:
  x =c * d
  if a == 1:
    print(int(b-a+1 - (b//c - (a)//c + b//d - (a) // d) + (b//x - (a)//x)))
    exit()
  print(int( b-a+1 - (b//c - (a-1)//c + b//d - (a-1) // d) + (b//x - (a-1)//x)))
  
  exit()
else:
  x = (c * d/l)
  if a == 1:
    print(int(b-a+1 - (b//c - (a)//c + b//d - (a) // d) + (b//x - (a)//x)))
    exit()
  print(int( b-a+1 - (b//c - (a-1)//c + b//d - (a-1) // d) + (b//x - (a-1)//x)))

Submission status: Wrong Answer

Solution code:
A, B, C, D = map(int, input().split())
count = 0
if C!= D:
    c_offset = (A-1) // C 
    c_ = B // C
    c = c_ - c_offset

    d_offset = (A-1) // D
    d_ = B // D
    d = d_- d_offset

    cd_offset = (A-1) // (D*C)
    cd_ = B // (D*C)
    cd = cd_- cd_offset

    count = B - A - (c + d - cd) +1
else:
    c_offset = (A-1) // C 
    c_ = B // C
    c = c_ - c_offset
    
    count = B - A - c +1

print(count)

Submission status: Wrong Answer

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

if c == 1 or d == 1:
    print(0)
    exit()

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

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

e = lcm(c, d)

if a % c != 0:
    c1 = (a // c + 1) * c
else:
    c1 = a
if b % c != 0:
    c2 = (b // c) * c
else:
    c2 = b
cCount = (c2 - c1) // c + 1

if a % d != 0:
    d1 = (a // d + 1) * d
else:
    d1 = a
if b % d != 0:
    d2 = (b // d) * d
else:
    d2 = b
dCount = (d2 - d1) // d + 1

if a % e != 0:
    e1 = (a // e + 1) * e
else:
    e1 = a
if b % d != 0:
    e2 = (b // e) * e
else:
    e2 = e
eCount = (e2 - e1) // e + 1
print(b - a - (cCount + dCount - eCount) + 1)

Submission status: Wrong Answer

Solution code:
import fractions
import math

def main():
    buf = input()
    buflist = buf.split()
    A = int(buflist[0])
    B = int(buflist[1])
    C = int(buflist[2])
    D = int(buflist[3])
    l = (C * D) // fractions.gcd(C, D)
    cA = math.ceil(A / C) * C
    cB = B // C * C
    c_count = max(0, (cB - cA) // C + 1)
    dA = math.ceil(A / D) * D
    dB = B // D * D
    d_count = max(0, (dB - dA) // D + 1)
    lA = math.ceil(A / l) * l
    lB = B // l * l
    l_count = max(0, (lB - lA) // l + 1)
    selected = c_count + d_count - l_count
    unselected = (B - A + 1) - selected
    print(unselected)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

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

num=B-A+1

X=num/C#3
Y=num/D#2
Z=num/(C*D)

print(int(round(num-(X+Y-Z),0)))

Submission status: Wrong Answer

Solution code:
#c.py
def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    c = a % b
    return gcd(b, c)
def lcm(a, b):
    return a * b / gcd(a,b)
a,b,c,d = map(int,input().split())
canC = int(b//c - (a-1)//c)
canD = int(b//d - (a-1)//d)
dbCD = int(b//lcm(c,d)) -int((a-1)//lcm(c,d))
print((b-a+1) -canC - canD + dbCD)

Submission status: Wrong Answer

Solution code:
a, b, c, d = (int(i) for i in input().split())
e = 0
f = 0
g = 0
h = 0
j = 0
k = 0
l = 0
m = 0
n = 0
o = 0
count = 0

e = -(-a//c)
f = b // c 
g = f - e + 1 #cの倍数の個数

h = -(-a//d)
j = b // d 
k = j - h + 1 #dの倍数の個数

o = c*d
l = -(-a//o)
m = b // o 
n = m - l + 1 #c*dの倍数の個数 

print(b-a+1-g-k+n)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def saidaikouyaku(a, b):
    if b > a:
        a, b = b, a

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

A, B, C, D = map(int, input().split())
amari1 = 0
amari2 = 0
amari3 = 0
hani = B - A + 1
if B > C:
    amari1 = A % C
F = hani - amari1
Cnum = int(F / C) + 1
if B > D:
    amari2 = A % D
G = hani - amari2
Dnum = int(G / D) + 1
saisyoukoubai = int(C * D / saidaikouyaku(C, D))
if B > saisyoukoubai:
    amari3 = A % saisyoukoubai
H = hani - amari3
koubainum = int(H / saisyoukoubai) + 1
#print(hani)
#print(Cnum, Dnum, koubainum)
#print(amari1, amari2, amari3)
print(hani - Cnum - Dnum + koubainum)

Submission status: Wrong Answer

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

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

if a == b:
  if a % c == 0 or a % d == 0:
    print(0)
    exit()
  else:
    print(1)
    exit()

if b < c and b < d:
  print(b-a + 1)
  exit()    
if l == 1:
  x =c * d
  if a == 0:
    print(int(b-a+1 - (b//c - (a)//c + b//d - (a) // d) + (b//x - (a)//x)))
    exit()
  print(int( b-a+1 - (b//c - (a-1)//c + b//d - (a-1) // d) + (b//x - (a-1)//x)))
  
  exit()
else:
  x = (c * d/l)
  if a == 0:
    print(int(b-a+1 - (b//c - (a)//c + b//d - (a) // d) + (b//x - (a)//x)))
    exit()
  print(int( b-a+1 - (b//c - (a-1)//c + b//d - (a-1) // d) + (b//x - (a-1)//x)))

Submission status: Wrong Answer

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)

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

a, b, c, d = map(int, input().split())
b1 = b // c
a1 = a // c
b2 = b // d
a2 = a // d
a3 = a // lcm(c,d)
b3 = b // lcm(c,d)
#print(a1, a2, a3)
#print(b1, b2, b3)
a_ans = a1 + a2 - a3
b_ans = b1 + b2 - b3
diff = b - a
ans = diff - (b_ans - a_ans)
if (lcm(c,d) != d*c and d % c != 0 and c % d != 0):
    ans += 1
print(ans)

Submission status: Wrong Answer

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

Anum = A - 1 - (int((A-1)/C) + int((A-1)/D) - int((A-1)/(C*D)))
Bnum = B - (int((B)/C) + int((B)/D) - int((B)/(C*D)))

print(Bnum - Anum)

Submission status: Wrong Answer

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

from fractions import gcd
c_d=c*d/gcd(c,d)
if a%c==0:
    wari_c=int(b//c-a//c+1)
else:
    wari_c=int(b//c-a//c)
if a%d==0:
    wari_d=int(b//d-a//d+1)
else:
    wari_d=int(b//d-a//d)
if a%c_d==0:
    wari_c_d=int(b//c_d-a//c_d+1)
else:
    wari_c_d=int(b//c_d-a//c_d)

print(b-a+1-(wari_c+wari_d-wari_c_d))

Submission status: Wrong Answer

Solution code:
from fractions import gcd

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

def f(x, c, d):
    return max(x-int(x/c)-int(x/d)+int(x/lcm(c,d)), 0)

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

Submission status: Wrong Answer

Solution code:
import math


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

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


if(A%C!=0):
    # 切り上げ
    C_min = math.ceil(A/C)
else:
    C_min = A/C

if(B%C!=0):
    #切り捨て
    C_max = math.floor(B/C)
else:
    C_max = B/C

# 割り切れる個数
C_num = C_max - C_min + 1
#print("C_num",C_num)


if(A%D!=0):
    # 切り上げ
    D_min = math.ceil(A/D)
else:
    D_min = A/D
    
if(B%D!=0):
    #切り捨て
    D_max = math.floor(B/D)
else:
    D_max = B/D
#print("Dmax",D_max)

# 割り切れる個数
D_num = D_max - D_min + 1
#print("D_num",D_num)

lcm = C * D / (gcd(C, D))
if(A%(lcm)!=0):
    # 切り上げ
    CD_min = math.ceil(A/lcm)
else:
    CD_min = A/lcm

if(B%lcm!=0):
    #切り捨て
    CD_max = math.floor(B/lcm)
else:
    CD_max = B/lcm
    
# 割り切れる個数
CD_num = CD_max - CD_min + 1
#print("CD_num",CD_num)

# AからBまで
num = B - A + 1
#print("num",num)

result = num - (C_num + D_num - CD_num)
print(int(result))

Submission status: Wrong Answer

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

A,B,C,D=map(int,input().split())
E=lcm(C,D)
for i in range(A,A+C):
	if i%C==0:
		p=i
q=int((B-p)/C)
for j in range(A,A+D):
	if j%D==0:
		r=j
s=int((B-r)/D)
for k in range(A,A+E):
	if k%E==0:
		t=k
k=int((B-t)/E)
print((B-A+1)-(q+s-k+1))

Submission status: Wrong Answer

Solution code:
import fractions as f
a,b,c,d=map(int,input().split())
x=b-a+1
l=c*d//f.gcd(c,d)
i=(b//c)-(a//c)
j=(b//d)-(a//d)
k=(b//l)-(a//l)
if d%c==0 or c%d==0:
  print(x-max(i,j))
else:
  print(x-i-j+k)

Submission status: Wrong Answer

Solution code:
import fractions

def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
  
A, B, C, D = map(int, input().split())

v = B - A

x = v // C
y = v // D
z = v // lcm(C, D)
num = (v - (x + y - z))
    
print(num)

Submission status: Wrong Answer

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

a,b,c,d=(int(x) for x in input().split())
bs=int(lcm(c,d))
kihon=int(bs)+1-int(bs/c)-int(bs/d)
if a%bs==0:
    saisyo=a
else:
    saisyo=((a//bs)+1)*bs
saidai=(b//bs)*bs
count=int(kihon)*int((saidai-saisyo)/bs)
fro=saisyo-a
bac=b-saidai
ans=fro-((fro//c)+(fro//d))+bac-((bac//c)+(bac//d))
print(math.floor(ans+count))

Submission status: Wrong Answer

Solution code:
import fractions
from math import ceil

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

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

rng = B - A

count = B - A + 1

if A <= ceil(A / C) * C and ceil(A / C) * C <= B:
  count -= max(B - ceil(A / C) * C, 0) // C + 1

if A <= ceil(A / D) * D and ceil(A / D) * D <= B:
  count -= max(B - ceil(A / D) * D, 0) // D + 1

C_D_lcm = lcm(C, D)

if A <= ceil(A / C_D_lcm) * C_D_lcm and ceil(A / C_D_lcm) * C_D_lcm <= B:
  count += max(B - ceil(A / C_D_lcm) * C_D_lcm, 0) // C_D_lcm + 1

print(count)

Submission status: Wrong Answer

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

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

a, b, c, d = map(int, input().split())
count = 0
if a%c==0:
    lower1 = a//c
else:
    lower1 = a//c + 1
if a%d==0:
    lower2 = a//d
else:
    lower2 = a//d + 1
if a%lcm(c,d)==0:
    lower3 = a//lcm(c,d)
else:
    lower3 = a//lcm(c,d) + 1
higher1 = b//c
higher2 = b//d
higher3 = b//lcm(c,d)

count = higher1 - lower1 + 1 + higher2 - lower2 + 1 - higher3 - lower3 + 1

print(b - a + 1 - count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

MofD = 0
MofCD = 0

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

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


def sten(st, en, x):
    st = math.ceil(st/x)*x
    en = math.floor(en/x)*x
    if en >= st:
        n = (en - st) // x + 1
    else:
        n = 0
    return n


MofC = sten(A, B, C)
if C != D:
    MofD = sten(A, B, D)
    MofCD = sten(A, B, lcm(C, D))

print(U-MofC-MofD+MofCD)

Submission status: Wrong Answer

Solution code:

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

def c(begin, end, num):
    r = end - begin + 1
    if r < num:
        if begin <= num and num <= end:
            return 1
        else:
            return 0
    if num == r:
        return 1
    if num < r:
        begin = ((begin // num) + 1) * num
        r = end - begin + 1
        ans = 1 + (r // num)
        return ans

A, B, C, D = [int(x) for x in input().split(' ')]
C, D = (C, D) if C < D else (D, C)
CD = (C * D) // gcd(C, D)

count = 0

if A == B:
    if A % C == 0 or A % D == 0:
        print(0)
    else:
        print(1)
    exit(0)
else:
    if C == D:
        c_count = c(A, B, C)
        count = c_count
    elif D == CD:
        c_count = c(A, B, C)
        d_count = c(A, B, D)
        count = c_count + d_count
    else:
        c_count = c(A, B, C)
        d_count = c(A, B, D)
        cd_count = c(A, B, CD)
        count = c_count + d_count - cd_count

print((B - A + 1) - count)

Submission status: Wrong Answer

Solution code:
import numpy as np
a = input()
a, b, c, d = [int(i) for i in a.split(" ")]
count = 0
for i in range(a, b):
	if i % c != 0:
		if i % d != 0:
			count += 1
	else:
		continue
print(count)

Submission status: Wrong Answer

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

def main():
    A,B,C,D=input().split()
    A=int(A)
    B=int(B)
    C=int(C)
    D=int(D)
    if A%C!=0:
        C_min=A-A%C+C
    else:
        C_min=A
    C_max=B-B%C
    if A%D!=0:
        D_min=A-A%D+D
    else:
        D_min=A
    D_max=B-B%D
    if A%(C*D/gcd(C,D))!=0:
        CD_min=A-A%(C*D/gcd(C,D))+(C*D/gcd(C,D))
    else:
        CD_min=A
    CD_max=B-B%(C*D/gcd(C,D))
    C_sum=(C_max-C_min)/C+1
    D_sum=(D_max-D_min)/D+1
    CD_sum=(CD_max-CD_min)/(C*D/gcd(C,D))+1
    sum= B-A+1-(C_sum+D_sum-CD_sum)
    return sum

sum=main()
print(int(sum))

Submission status: Wrong Answer

Solution code:
from fractions import gcd
#from math import gcd
a,b,c,d=map(int,input().split())
ans=b-a+1
#for i in range(a,b+1):
#  if not(i%c==0 or i%d==0):
#    ans+=1
minac=c*(-(-a//c))
minad=d*(-(-a//d))
maxbc=c*(b//c)
maxbd=d*(b//d)
cn=(maxbc-minac)//c+1
dn=(maxbd-minad)//d+1
cd=c*d//gcd(c,d)
cdn=(-(-((max(maxbc,maxbd)-min(minac,minad)))//(cd)))
ans-=(cn+dn-cdn)
if ans<0:
    ans=0
print(ans)

Submission status: Wrong Answer

Solution code:
import fractions
A, B, C, D=list(map(int, input().split()))
counter = 0
lcm = (C*D)/fractions.gcd(C,D)
cda = (A-1)//C
dda = (A-1)//D
cdb = B//C
ddb = B//D
cdda = int((A-1)//lcm)
cddb = int(B//lcm)
counter = (cdb - cda) + (ddb - dda) - (cddb - cdda)
print(int(B-A+1-counter))

Submission status: Wrong Answer

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

ans = 0

c_n_min = math.ceil(A / C)
c_n_max = math.floor(B / C)
if (C <= B and c_n_min <= c_n_max):
    c_n = c_n_max - c_n_min + 1
else:
    c_n = 0

d_n_min = math.ceil(A / D)
d_n_max = math.floor(B / D)
if (D <= B and d_n_min <= d_n_max):
    d_n = d_n_max - d_n_min + 1
else:
    d_n =0

cd = C * D // fractions.gcd(C,D)
cd_n_min = math.ceil(A / cd)
cd_n_max = math.floor(B / cd)
if (cd <= B and cd_n_min <= cd_n_max):
    cd_n = cd_n_max - cd_n_min + 1
else:
    cd_n = 0

ans = (B - A + 1) - (c_n + d_n - cd_n)
print(ans)

Submission status: Wrong Answer

Solution code:
import math

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

# Cで割り切れる数
div_c_min = math.ceil(A/C)
div_c_max = math.floor(B/C)
div_c_num = div_c_max - div_c_min + 1
#print(div_c_min, div_c_max)
#print(div_c_num)

# Dで割り切れる数
div_d_min = math.ceil(A/D)
div_d_max = math.floor(B/D)
div_d_num = div_d_max - div_d_min + 1
#print(div_d_min, div_d_max)
#print(div_d_num)

# 最大公約数
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
    return z

# 最小公倍数
def lcm(x, y):
    return (x * y) / gcd(x, y)

# CとDの公倍数で割り切れる数
div_cd_min = math.ceil(A / lcm(C, D))
div_cd_max = math.floor(B / lcm(C, D))
div_cd_num = div_cd_max - div_cd_min + 1
#print(div_cd_min, div_cd_max)
#print(div_cd_num)

numAtoB = B - A + 1
ans = numAtoB - (div_c_num + div_d_num - div_cd_num)
if ans >= 0:
    print(ans)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
# 2019 6/22
# ABC 131 C Anti Division

INF = 10 ** 20

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

numerator = [A, B]

print(numerator)

up_to = B - A + 1


def solve(key):
    if key <= A:
        return up_to // key
    elif key <= B:
        is_on = False
        width = up_to % key
        for i in range(width):
            num = A + i
            if num % key == 0:
                is_on = True
                break

        if is_on:
            return up_to // key + 1
        else:
            return up_to // key
    else:
        return 0


def gcd(x, y):
    a = max(x, y)
    b = min(x, y)
    c = a
    d = b

    while b > 0:
        r = a % b
        a = b
        b = r

    mod = c / a

    return int(d * mod)


GCD = gcd(C, D)

C_div = solve(C)
D_div = solve(D)
CD_div = solve(GCD)

print(up_to, C_div, D_div, CD_div)

print(up_to - (C_div + D_div - CD_div))

Submission status: Wrong Answer

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

def main():
    A,B,C,D=input().split()
    A=int(A)
    B=int(B)
    C=int(C)
    D=int(D)
    if A%C!=0:
        C_min=A-A%C+C
    else:
        C_min=A
    C_max=B-B%C
    if A%D!=0:
        D_min=A-A%D+D
    else:
        D_min=A
    D_max=B-B%D
    if A%(C*D/gcd(C,D))!=0:
        CD_min=A-A%(C*D/gcd(C,D))+(C*D/gcd(C,D))
    else:
        CD_min=A
    CD_max=B-B%(C*D/gcd(C,D))
    C_sum=(C_max-C_min)/C+1
    D_sum=(D_max-D_min)/D+1
    CD_sum=(CD_max-CD_min)/(C*D/gcd(C,D))+1
    sum= B-A+1-(C_sum+D_sum-CD_sum)
    return sum

sum=main()
print(sum)