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 math import gcd
def solve():
n,a,b=map(int,input().split())
ls=[]
for i in range(n):
x,y=map(int,input().split())
ls.append([x,y])
a/=gcd(a,b+1)
a*=b
for i in ls:
if i[1]-i[0]+1>=a:
print(a)
return
q=[]
for i in ls:
f=i[0]%a
s=i[1]%a
if(f<s or i[0]==i[1]):
q.append([f,s])
else:
q.append([f,a-1])
q.append([0,s])
q.sort()
ans=a-q[0][0]
maxi=q[0][1]
for i in range(1,len(q)):
if q[i-1][1]<q[i][0]:
ans-=q[i][0]-q[i-1][1]-1
maxi=max(maxi,q[i][1])
print(int(ans-a+maxi+1))
solve()
# | 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... |