#include <bits/stdc++.h>
using namespace std;
template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl
typedef long long ll;
const ll mod = 1e9+7;
const int maxn = 1e6 + 5;
ll n, a, b;
ll getx(ll t) {
return (t+t/b)%a;
}
ll gety(ll t) {
return t%b;
}
ll merge(vector<pair<ll,ll>> v) {
sort(v.begin(), v.end());
// for (auto p: v) {
// cout<<p.first<<" "<<p.second<<endl;
// }
ll res = 0;
n = v.size();
auto cur = v.front();
for (int i=1; i<n; i++) {
ll x=v[i].first;
ll y=v[i].second;
if (x>cur.second) {
res += cur.second-cur.first+1;
cur = v[i];
} else {
cur.second=max(cur.second,y);
}
}
res += cur.second-cur.first+1;
return res;
}
vector<pair<ll,ll>> v;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n>>a>>b;
v.resize(n);
for (int i=0; i<n; i++) {
cin>>v[i].first>>v[i].second;
}
auto safe_lcm = [](ll x, ll y) {
ll tmp = x / __gcd(x,y+1);
if (tmp > 1e18/y) return (ll)1e18 + 1;
return tmp*y;
};
ll lcm = safe_lcm(a, b);
if (lcm>1e18) {
//merge intervals
ll res = merge(v);
out(res);
}
vector<pair<ll,ll>> cv;
for (int i=0; i<n; i++) {
if (v[i].second-v[i].first+1>=lcm) out(lcm);
v[i].first %= lcm;
v[i].second %= lcm;
if (v[i].second>=v[i].first) {
cv.push_back(v[i]);
} else {
assert(v[i].first>v[i].second);
cv.push_back({v[i].first,lcm-1});
cv.push_back({0,v[i].second});
}
}
ll ans = merge(cv);
out(ans);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
10 ms |
896 KB |
Output is correct |
3 |
Correct |
10 ms |
896 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
10 ms |
896 KB |
Output is correct |
17 |
Correct |
61 ms |
5100 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
378 ms |
47456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
524 ms |
47456 KB |
Output is correct |
3 |
Correct |
512 ms |
47560 KB |
Output is correct |
4 |
Correct |
511 ms |
47564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
524 ms |
47456 KB |
Output is correct |
3 |
Correct |
512 ms |
47560 KB |
Output is correct |
4 |
Correct |
511 ms |
47564 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
529 ms |
47572 KB |
Output is correct |
7 |
Correct |
527 ms |
47576 KB |
Output is correct |
8 |
Correct |
516 ms |
47444 KB |
Output is correct |
9 |
Correct |
561 ms |
47580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
524 ms |
47456 KB |
Output is correct |
3 |
Correct |
512 ms |
47560 KB |
Output is correct |
4 |
Correct |
511 ms |
47564 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
55 ms |
5232 KB |
Output is correct |
7 |
Correct |
58 ms |
5228 KB |
Output is correct |
8 |
Correct |
54 ms |
5100 KB |
Output is correct |
9 |
Correct |
54 ms |
5232 KB |
Output is correct |
10 |
Correct |
56 ms |
5228 KB |
Output is correct |
11 |
Correct |
56 ms |
5228 KB |
Output is correct |
12 |
Correct |
53 ms |
5100 KB |
Output is correct |
13 |
Correct |
57 ms |
5100 KB |
Output is correct |
14 |
Correct |
53 ms |
5228 KB |
Output is correct |
15 |
Correct |
60 ms |
5228 KB |
Output is correct |
16 |
Correct |
59 ms |
5100 KB |
Output is correct |
17 |
Correct |
56 ms |
5236 KB |
Output is correct |
18 |
Correct |
529 ms |
47596 KB |
Output is correct |
19 |
Correct |
511 ms |
47444 KB |
Output is correct |
20 |
Correct |
562 ms |
47500 KB |
Output is correct |
21 |
Correct |
58 ms |
5100 KB |
Output is correct |
22 |
Correct |
53 ms |
5228 KB |
Output is correct |
23 |
Correct |
167 ms |
16488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
58 ms |
5100 KB |
Output is correct |
3 |
Correct |
60 ms |
5228 KB |
Output is correct |
4 |
Correct |
584 ms |
47560 KB |
Output is correct |
5 |
Correct |
57 ms |
5100 KB |
Output is correct |
6 |
Correct |
61 ms |
5220 KB |
Output is correct |
7 |
Correct |
57 ms |
5100 KB |
Output is correct |
8 |
Correct |
64 ms |
5228 KB |
Output is correct |
9 |
Correct |
60 ms |
5228 KB |
Output is correct |
10 |
Correct |
59 ms |
5104 KB |
Output is correct |
11 |
Correct |
58 ms |
5100 KB |
Output is correct |
12 |
Correct |
55 ms |
5100 KB |
Output is correct |
13 |
Correct |
58 ms |
5100 KB |
Output is correct |
14 |
Correct |
557 ms |
47584 KB |
Output is correct |
15 |
Correct |
59 ms |
5100 KB |
Output is correct |
16 |
Correct |
524 ms |
47520 KB |
Output is correct |
17 |
Correct |
509 ms |
47428 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
10 ms |
896 KB |
Output is correct |
3 |
Correct |
10 ms |
896 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
10 ms |
896 KB |
Output is correct |
17 |
Correct |
61 ms |
5100 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
4 ms |
384 KB |
Output is correct |
20 |
Correct |
4 ms |
384 KB |
Output is correct |
21 |
Correct |
4 ms |
384 KB |
Output is correct |
22 |
Correct |
4 ms |
384 KB |
Output is correct |
23 |
Correct |
4 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
378 ms |
47456 KB |
Output is correct |
29 |
Correct |
4 ms |
384 KB |
Output is correct |
30 |
Correct |
524 ms |
47456 KB |
Output is correct |
31 |
Correct |
512 ms |
47560 KB |
Output is correct |
32 |
Correct |
511 ms |
47564 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
529 ms |
47572 KB |
Output is correct |
35 |
Correct |
527 ms |
47576 KB |
Output is correct |
36 |
Correct |
516 ms |
47444 KB |
Output is correct |
37 |
Correct |
561 ms |
47580 KB |
Output is correct |
38 |
Correct |
5 ms |
384 KB |
Output is correct |
39 |
Correct |
55 ms |
5232 KB |
Output is correct |
40 |
Correct |
58 ms |
5228 KB |
Output is correct |
41 |
Correct |
54 ms |
5100 KB |
Output is correct |
42 |
Correct |
54 ms |
5232 KB |
Output is correct |
43 |
Correct |
56 ms |
5228 KB |
Output is correct |
44 |
Correct |
56 ms |
5228 KB |
Output is correct |
45 |
Correct |
53 ms |
5100 KB |
Output is correct |
46 |
Correct |
57 ms |
5100 KB |
Output is correct |
47 |
Correct |
53 ms |
5228 KB |
Output is correct |
48 |
Correct |
60 ms |
5228 KB |
Output is correct |
49 |
Correct |
59 ms |
5100 KB |
Output is correct |
50 |
Correct |
56 ms |
5236 KB |
Output is correct |
51 |
Correct |
529 ms |
47596 KB |
Output is correct |
52 |
Correct |
511 ms |
47444 KB |
Output is correct |
53 |
Correct |
562 ms |
47500 KB |
Output is correct |
54 |
Correct |
58 ms |
5100 KB |
Output is correct |
55 |
Correct |
53 ms |
5228 KB |
Output is correct |
56 |
Correct |
167 ms |
16488 KB |
Output is correct |
57 |
Correct |
5 ms |
384 KB |
Output is correct |
58 |
Correct |
58 ms |
5100 KB |
Output is correct |
59 |
Correct |
60 ms |
5228 KB |
Output is correct |
60 |
Correct |
584 ms |
47560 KB |
Output is correct |
61 |
Correct |
57 ms |
5100 KB |
Output is correct |
62 |
Correct |
61 ms |
5220 KB |
Output is correct |
63 |
Correct |
57 ms |
5100 KB |
Output is correct |
64 |
Correct |
64 ms |
5228 KB |
Output is correct |
65 |
Correct |
60 ms |
5228 KB |
Output is correct |
66 |
Correct |
59 ms |
5104 KB |
Output is correct |
67 |
Correct |
58 ms |
5100 KB |
Output is correct |
68 |
Correct |
55 ms |
5100 KB |
Output is correct |
69 |
Correct |
58 ms |
5100 KB |
Output is correct |
70 |
Correct |
557 ms |
47584 KB |
Output is correct |
71 |
Correct |
59 ms |
5100 KB |
Output is correct |
72 |
Correct |
524 ms |
47520 KB |
Output is correct |
73 |
Correct |
509 ms |
47428 KB |
Output is correct |
74 |
Correct |
4 ms |
384 KB |
Output is correct |
75 |
Correct |
4 ms |
384 KB |
Output is correct |
76 |
Correct |
4 ms |
384 KB |
Output is correct |
77 |
Correct |
4 ms |
384 KB |
Output is correct |
78 |
Correct |
4 ms |
384 KB |
Output is correct |
79 |
Correct |
10 ms |
1280 KB |
Output is correct |
80 |
Correct |
587 ms |
69164 KB |
Output is correct |
81 |
Correct |
582 ms |
67924 KB |
Output is correct |
82 |
Correct |
572 ms |
67876 KB |
Output is correct |
83 |
Correct |
551 ms |
68036 KB |
Output is correct |
84 |
Correct |
573 ms |
67908 KB |
Output is correct |
85 |
Correct |
561 ms |
68180 KB |
Output is correct |
86 |
Correct |
167 ms |
28900 KB |
Output is correct |
87 |
Correct |
5 ms |
384 KB |
Output is correct |