This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,A,B;
int l[1000005],r[1000005];
signed main()
{
cin >> n >> A >> B;
for (int i = 1; i <= n; i++)
cin >> l[i] >> r[i];
__int128 AA = A, BB = B;
__int128 FF = AA * BB / __gcd(AA,BB + 1);
if (FF > r[n])
{
int sumlen = 0;
for (int i = 1; i <= n; i++)
sumlen += r[i] - l[i] + 1;
cout << sumlen;
}
else
{
int F = FF;
for (int i = 1; i <= n; i++)
{
if (r[i] - l[i] + 1 >= F)
{
cout << F;
return 0;
}
}
vector<pair<int,int>> h;
for (int i = 1; i <= n; i++)
{
if (r[i] % F >= l[i] % F)
h.push_back({l[i] % F,r[i] % F});
else
h.push_back({0,r[i] % F}),h.push_back({l[i] % F,F - 1});
}
sort(h.begin(),h.end());
int ans = F;
int rmax = -1;
if (h.back().second != F - 1)
ans -= F - 1 - h.back().second;
for (auto it : h)
{
if (it.first > rmax + 1)
ans -= (it.first - rmax - 1);
rmax = max(rmax,it.second);
}
cout << ans;
}
return 0;
}
///cate resturi modulo A * B / gcd(A,B + 1) gasesc?
# | 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... |