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 ll unsigned long long
#define pii pair<ll, ll>
const ll inf = 1e18+5;
ll n, a, b, loop, l, r;
vector<pii> v;
ll gcd(ll c1, ll c2) {
if (!(max(c1,c2)%min(c1,c2))) return min(c1, c2);
return gcd(min(c1,c2), max(c1,c2)%min(c1,c2));
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> a >> b;
a/=gcd(a,b+1);
while (a>0) {
loop += (a%10)*b;
if (loop>inf) break;
a/=10;
}
while (n--) {
cin >> l >> r, l%=loop, r%=loop;
if (l>r) v.push_back({0,r}), v.push_back({l,loop-1});
else v.push_back({l,r});
}
sort(v.begin(), v.end());
ll out=v[0].second-v[0].first+1, ma=v[0].second;
for (auto [x,y]:v) out += (ma<x ? y-x+1 : max((ll)0,y-ma)), ma=max(ma,y);
cout << out;
return 0;
}
/*
3 3 3
4 4
7 9
17 18
ans: 4
3 5 10
1 20
50 68
89 98
ans: 31
1 1000000000000000 1000000000000000000
100000 1000000
*/
# | 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... |