This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
from sys import stdin
def gcd(x, y):
if x == 0:
return y
if y == 0:
return x
if x > y:
x, y = y, x
return gcd(y % x, x)
n, a, b = map(int, stdin.readline().split())
pr = []
for i in range(n):
x, y = map(int, stdin.readline().split())
pr.append((x, y))
g = gcd(a, b+1)
num = a * b // g
ev = []
ans = 0
for p in pr:
l = p[0]
r = p[1]
if r - l + 1 >= num:
ans = num
l %= num
r %= num
if r < l:
ev += [(l, 1), (num, -1), (0, 1), (r+1, -1)]
else:
ev += [(l, 1), (r + 1, -1)]
ev.sort()
bal = 0
last = 0
for p in ev:
x = p[0]
tp = p[1]
if bal: ans += x - last
last = x
bal += tp
print min(num, ans)
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |