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;
using ll = long long;
const int mod = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(false);
cout.tie(0); cin.tie(0);
ll n, A, B;
cin >> n >> A >> B;
ll P = min( (__int128_t)A / (__int128_t)__gcd(A, B + 1) * (__int128_t)B, (__int128_t)1e18 + 1 );
vector<pair<ll, ll> > st;
for(int i=0; i<n; i++) {
ll L, R;
cin >> L >> R;
if(R - L + 1 >= P) {
cout << P << '\n';
return 0;
}
ll a = L % P, b = R % P;
if(L == R) {
st.push_back({ a, b });
} else if(a < b) {
st.push_back({ a, b });
} else {
st.push_back({ a, P - 1 });
st.push_back({ 0, b });
}
}
ll res = 0;
sort(st.begin(), st.end());
ll left_end=st[0].first, right_end=st[0].second;
for(int i=1; i<st.size(); i++) {
if(st[i].first > right_end) {
res += right_end - left_end + 1;
left_end = st[i].first;
right_end = st[i].second;
} else {
right_end = max(right_end, st[i].second);
}
}
cout << res + right_end - left_end + 1 << '\n';
return 0;
}
Compilation message (stderr)
strange_device.cpp: In function 'int main()':
strange_device.cpp:40:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int i=1; i<st.size(); i++) {
| ~^~~~~~~~~~
# | 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... |