#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e6 + 5;
const ll oo = 1e18 + 10;
int n;
ll A, B;
ll l[N], r[N];
ll GCD(ll A, ll B) {
if (A < B) swap(A, B);
while (B) {
A %= B;
swap(A, B);
}
return A;
}
int main() {
cin.tie(0) -> sync_with_stdio(0);
cin >> n >> A >> B;
for (int i = 1; i <= n; i++) cin >> l[i] >> r[i];
ll temp = (B + 1) % A;
ll diff;
if (!temp) diff = B;
else {
ll gcd = GCD(temp, A);
temp = A / gcd;
if (oo / temp < B) diff = oo;
else diff = temp * B;
}
if (diff == oo) {
ll ans = 0;
for (int i = 1; i <= n; i++) ans += r[i] - l[i] + 1;
cout << ans;
}
else {
#define pa pair<ll, ll>
#define fi first
#define se second
vector<pa> v;
for (int i = 1; i <= n; i++) {
ll L = l[i] % diff, R = r[i] % diff;
ll hL = l[i] / diff, hR = r[i] / diff;
if (hR - hL > 2 || (hR - hL == 1 && R >= L - 1))
return cout << diff, 0;
if (hR == hL) v.emplace_back(L, R);
else {
v.emplace_back(L, diff - 1);
v.emplace_back(0, R);
}
}
sort(begin(v), end(v));
ll curL = 0, curR = -1;
ll ans = 0;
for (auto i: v) {
if (i.fi > curR) {
ans += curR - curL + 1;
curR = i.se;
curL = i.fi;
}
else curR = max(curR, i.se);
}
ans += curR - curL + 1;
cout << ans;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
4 ms |
3164 KB |
Output is correct |
3 |
Correct |
4 ms |
3164 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2512 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2400 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
4 ms |
3164 KB |
Output is correct |
17 |
Correct |
57 ms |
12500 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2508 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
212 ms |
58336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
324 ms |
71640 KB |
Output is correct |
3 |
Correct |
327 ms |
70624 KB |
Output is correct |
4 |
Correct |
300 ms |
70748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
324 ms |
71640 KB |
Output is correct |
3 |
Correct |
327 ms |
70624 KB |
Output is correct |
4 |
Correct |
300 ms |
70748 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
323 ms |
71656 KB |
Output is correct |
7 |
Correct |
339 ms |
69656 KB |
Output is correct |
8 |
Correct |
341 ms |
69828 KB |
Output is correct |
9 |
Correct |
345 ms |
69780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
324 ms |
71640 KB |
Output is correct |
3 |
Correct |
327 ms |
70624 KB |
Output is correct |
4 |
Correct |
300 ms |
70748 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
32 ms |
12496 KB |
Output is correct |
7 |
Correct |
35 ms |
12516 KB |
Output is correct |
8 |
Correct |
45 ms |
12644 KB |
Output is correct |
9 |
Correct |
31 ms |
12496 KB |
Output is correct |
10 |
Correct |
46 ms |
12516 KB |
Output is correct |
11 |
Correct |
32 ms |
12516 KB |
Output is correct |
12 |
Correct |
30 ms |
12496 KB |
Output is correct |
13 |
Correct |
36 ms |
12496 KB |
Output is correct |
14 |
Correct |
34 ms |
12496 KB |
Output is correct |
15 |
Correct |
34 ms |
12448 KB |
Output is correct |
16 |
Correct |
37 ms |
12528 KB |
Output is correct |
17 |
Correct |
31 ms |
12496 KB |
Output is correct |
18 |
Correct |
312 ms |
69856 KB |
Output is correct |
19 |
Correct |
334 ms |
70752 KB |
Output is correct |
20 |
Correct |
354 ms |
71132 KB |
Output is correct |
21 |
Correct |
34 ms |
12496 KB |
Output is correct |
22 |
Correct |
31 ms |
12768 KB |
Output is correct |
23 |
Correct |
108 ms |
31716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
36 ms |
12524 KB |
Output is correct |
3 |
Correct |
55 ms |
12512 KB |
Output is correct |
4 |
Correct |
362 ms |
69664 KB |
Output is correct |
5 |
Correct |
40 ms |
13008 KB |
Output is correct |
6 |
Correct |
33 ms |
12904 KB |
Output is correct |
7 |
Correct |
42 ms |
12496 KB |
Output is correct |
8 |
Correct |
36 ms |
12496 KB |
Output is correct |
9 |
Correct |
34 ms |
12652 KB |
Output is correct |
10 |
Correct |
42 ms |
12652 KB |
Output is correct |
11 |
Correct |
34 ms |
12664 KB |
Output is correct |
12 |
Correct |
30 ms |
12480 KB |
Output is correct |
13 |
Correct |
36 ms |
7844 KB |
Output is correct |
14 |
Correct |
356 ms |
69700 KB |
Output is correct |
15 |
Correct |
34 ms |
7816 KB |
Output is correct |
16 |
Correct |
342 ms |
69480 KB |
Output is correct |
17 |
Correct |
337 ms |
70108 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
4 ms |
3164 KB |
Output is correct |
3 |
Correct |
4 ms |
3164 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2512 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2400 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
4 ms |
3164 KB |
Output is correct |
17 |
Correct |
57 ms |
12500 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2508 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
212 ms |
58336 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
324 ms |
71640 KB |
Output is correct |
31 |
Correct |
327 ms |
70624 KB |
Output is correct |
32 |
Correct |
300 ms |
70748 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
323 ms |
71656 KB |
Output is correct |
35 |
Correct |
339 ms |
69656 KB |
Output is correct |
36 |
Correct |
341 ms |
69828 KB |
Output is correct |
37 |
Correct |
345 ms |
69780 KB |
Output is correct |
38 |
Correct |
0 ms |
2396 KB |
Output is correct |
39 |
Correct |
32 ms |
12496 KB |
Output is correct |
40 |
Correct |
35 ms |
12516 KB |
Output is correct |
41 |
Correct |
45 ms |
12644 KB |
Output is correct |
42 |
Correct |
31 ms |
12496 KB |
Output is correct |
43 |
Correct |
46 ms |
12516 KB |
Output is correct |
44 |
Correct |
32 ms |
12516 KB |
Output is correct |
45 |
Correct |
30 ms |
12496 KB |
Output is correct |
46 |
Correct |
36 ms |
12496 KB |
Output is correct |
47 |
Correct |
34 ms |
12496 KB |
Output is correct |
48 |
Correct |
34 ms |
12448 KB |
Output is correct |
49 |
Correct |
37 ms |
12528 KB |
Output is correct |
50 |
Correct |
31 ms |
12496 KB |
Output is correct |
51 |
Correct |
312 ms |
69856 KB |
Output is correct |
52 |
Correct |
334 ms |
70752 KB |
Output is correct |
53 |
Correct |
354 ms |
71132 KB |
Output is correct |
54 |
Correct |
34 ms |
12496 KB |
Output is correct |
55 |
Correct |
31 ms |
12768 KB |
Output is correct |
56 |
Correct |
108 ms |
31716 KB |
Output is correct |
57 |
Correct |
1 ms |
2396 KB |
Output is correct |
58 |
Correct |
36 ms |
12524 KB |
Output is correct |
59 |
Correct |
55 ms |
12512 KB |
Output is correct |
60 |
Correct |
362 ms |
69664 KB |
Output is correct |
61 |
Correct |
40 ms |
13008 KB |
Output is correct |
62 |
Correct |
33 ms |
12904 KB |
Output is correct |
63 |
Correct |
42 ms |
12496 KB |
Output is correct |
64 |
Correct |
36 ms |
12496 KB |
Output is correct |
65 |
Correct |
34 ms |
12652 KB |
Output is correct |
66 |
Correct |
42 ms |
12652 KB |
Output is correct |
67 |
Correct |
34 ms |
12664 KB |
Output is correct |
68 |
Correct |
30 ms |
12480 KB |
Output is correct |
69 |
Correct |
36 ms |
7844 KB |
Output is correct |
70 |
Correct |
356 ms |
69700 KB |
Output is correct |
71 |
Correct |
34 ms |
7816 KB |
Output is correct |
72 |
Correct |
342 ms |
69480 KB |
Output is correct |
73 |
Correct |
337 ms |
70108 KB |
Output is correct |
74 |
Correct |
1 ms |
2396 KB |
Output is correct |
75 |
Correct |
1 ms |
2396 KB |
Output is correct |
76 |
Correct |
1 ms |
2396 KB |
Output is correct |
77 |
Correct |
1 ms |
2396 KB |
Output is correct |
78 |
Correct |
1 ms |
2396 KB |
Output is correct |
79 |
Correct |
4 ms |
3164 KB |
Output is correct |
80 |
Correct |
374 ms |
70336 KB |
Output is correct |
81 |
Correct |
366 ms |
70084 KB |
Output is correct |
82 |
Correct |
356 ms |
71392 KB |
Output is correct |
83 |
Correct |
345 ms |
71848 KB |
Output is correct |
84 |
Correct |
333 ms |
69828 KB |
Output is correct |
85 |
Correct |
342 ms |
71500 KB |
Output is correct |
86 |
Correct |
110 ms |
32712 KB |
Output is correct |
87 |
Correct |
1 ms |
2396 KB |
Output is correct |