#include <bits/stdc++.h>
using namespace std;
const long long MAXMOD = 1e18 + 7;
int n;
long long a, b;
void fastIO()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main()
{
fastIO();
cin >> n >> a >> b;
long long MOD = a / __gcd(a, b + 1);
if (MOD > MAXMOD / b)
{
MOD = MAXMOD;
}
else
{
MOD *= b; // a * b / gcd(a, b + 1)
}
vector<pair<long long, long long>> intervals;
long long ans = 0;
bool all = false;
for (int i = 1; i <= n; ++i)
{
long long l, r;
cin >> l >> r;
long long newL = l % MOD;
long long newR = r % MOD;
if (r - l + 1 >= MOD)
{
cout << MOD << endl;
return 0;
}
if (newL <= newR)
{
intervals.push_back({newL, newR});
}
else
{
intervals.push_back({newL, MOD - 1});
intervals.push_back({0, newR});
}
}
sort(intervals.begin(), intervals.end());
long long from = intervals[0].first, to = intervals[0].second;
for (int i = 1; i < intervals.size(); ++i)
{
if (intervals[i].first <= to)
{
to = max(to, intervals[i].second);
}
else
{
ans += (to - from + 1);
from = intervals[i].first;
to = intervals[i].second;
}
}
ans += (to - from + 1);
cout << ans << endl;
return 0;
}
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:67:23: 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]
67 | for (int i = 1; i < intervals.size(); ++i)
| ~~^~~~~~~~~~~~~~~~~~
strange_device.cpp:36:10: warning: unused variable 'all' [-Wunused-variable]
36 | bool all = false;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
4 ms |
724 KB |
Output is correct |
3 |
Correct |
4 ms |
1104 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
5 ms |
1104 KB |
Output is correct |
17 |
Correct |
35 ms |
6196 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
508 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
680 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
222 ms |
42156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
291 ms |
46324 KB |
Output is correct |
3 |
Correct |
263 ms |
20172 KB |
Output is correct |
4 |
Correct |
296 ms |
51720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
291 ms |
46324 KB |
Output is correct |
3 |
Correct |
263 ms |
20172 KB |
Output is correct |
4 |
Correct |
296 ms |
51720 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
275 ms |
18936 KB |
Output is correct |
7 |
Correct |
260 ms |
35752 KB |
Output is correct |
8 |
Correct |
255 ms |
18860 KB |
Output is correct |
9 |
Correct |
273 ms |
18844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
291 ms |
46324 KB |
Output is correct |
3 |
Correct |
263 ms |
20172 KB |
Output is correct |
4 |
Correct |
296 ms |
51720 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
28 ms |
4728 KB |
Output is correct |
7 |
Correct |
36 ms |
3540 KB |
Output is correct |
8 |
Correct |
26 ms |
3532 KB |
Output is correct |
9 |
Correct |
34 ms |
5056 KB |
Output is correct |
10 |
Correct |
27 ms |
6084 KB |
Output is correct |
11 |
Correct |
29 ms |
6348 KB |
Output is correct |
12 |
Correct |
26 ms |
3704 KB |
Output is correct |
13 |
Correct |
30 ms |
6204 KB |
Output is correct |
14 |
Correct |
30 ms |
5028 KB |
Output is correct |
15 |
Correct |
36 ms |
4948 KB |
Output is correct |
16 |
Correct |
30 ms |
6520 KB |
Output is correct |
17 |
Correct |
28 ms |
6348 KB |
Output is correct |
18 |
Correct |
303 ms |
54188 KB |
Output is correct |
19 |
Correct |
289 ms |
54180 KB |
Output is correct |
20 |
Correct |
305 ms |
54200 KB |
Output is correct |
21 |
Correct |
31 ms |
6356 KB |
Output is correct |
22 |
Correct |
33 ms |
6368 KB |
Output is correct |
23 |
Correct |
84 ms |
20540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
28 ms |
3628 KB |
Output is correct |
3 |
Correct |
29 ms |
3532 KB |
Output is correct |
4 |
Correct |
329 ms |
47692 KB |
Output is correct |
5 |
Correct |
34 ms |
5336 KB |
Output is correct |
6 |
Correct |
34 ms |
6100 KB |
Output is correct |
7 |
Correct |
29 ms |
3532 KB |
Output is correct |
8 |
Correct |
32 ms |
4808 KB |
Output is correct |
9 |
Correct |
37 ms |
3632 KB |
Output is correct |
10 |
Correct |
31 ms |
6336 KB |
Output is correct |
11 |
Correct |
40 ms |
3524 KB |
Output is correct |
12 |
Correct |
28 ms |
3604 KB |
Output is correct |
13 |
Correct |
35 ms |
5836 KB |
Output is correct |
14 |
Correct |
303 ms |
33380 KB |
Output is correct |
15 |
Correct |
38 ms |
6348 KB |
Output is correct |
16 |
Correct |
292 ms |
54192 KB |
Output is correct |
17 |
Correct |
268 ms |
54140 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
4 ms |
724 KB |
Output is correct |
3 |
Correct |
4 ms |
1104 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
5 ms |
1104 KB |
Output is correct |
17 |
Correct |
35 ms |
6196 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
508 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
680 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
222 ms |
42156 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
291 ms |
46324 KB |
Output is correct |
31 |
Correct |
263 ms |
20172 KB |
Output is correct |
32 |
Correct |
296 ms |
51720 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
275 ms |
18936 KB |
Output is correct |
35 |
Correct |
260 ms |
35752 KB |
Output is correct |
36 |
Correct |
255 ms |
18860 KB |
Output is correct |
37 |
Correct |
273 ms |
18844 KB |
Output is correct |
38 |
Correct |
1 ms |
336 KB |
Output is correct |
39 |
Correct |
28 ms |
4728 KB |
Output is correct |
40 |
Correct |
36 ms |
3540 KB |
Output is correct |
41 |
Correct |
26 ms |
3532 KB |
Output is correct |
42 |
Correct |
34 ms |
5056 KB |
Output is correct |
43 |
Correct |
27 ms |
6084 KB |
Output is correct |
44 |
Correct |
29 ms |
6348 KB |
Output is correct |
45 |
Correct |
26 ms |
3704 KB |
Output is correct |
46 |
Correct |
30 ms |
6204 KB |
Output is correct |
47 |
Correct |
30 ms |
5028 KB |
Output is correct |
48 |
Correct |
36 ms |
4948 KB |
Output is correct |
49 |
Correct |
30 ms |
6520 KB |
Output is correct |
50 |
Correct |
28 ms |
6348 KB |
Output is correct |
51 |
Correct |
303 ms |
54188 KB |
Output is correct |
52 |
Correct |
289 ms |
54180 KB |
Output is correct |
53 |
Correct |
305 ms |
54200 KB |
Output is correct |
54 |
Correct |
31 ms |
6356 KB |
Output is correct |
55 |
Correct |
33 ms |
6368 KB |
Output is correct |
56 |
Correct |
84 ms |
20540 KB |
Output is correct |
57 |
Correct |
1 ms |
336 KB |
Output is correct |
58 |
Correct |
28 ms |
3628 KB |
Output is correct |
59 |
Correct |
29 ms |
3532 KB |
Output is correct |
60 |
Correct |
329 ms |
47692 KB |
Output is correct |
61 |
Correct |
34 ms |
5336 KB |
Output is correct |
62 |
Correct |
34 ms |
6100 KB |
Output is correct |
63 |
Correct |
29 ms |
3532 KB |
Output is correct |
64 |
Correct |
32 ms |
4808 KB |
Output is correct |
65 |
Correct |
37 ms |
3632 KB |
Output is correct |
66 |
Correct |
31 ms |
6336 KB |
Output is correct |
67 |
Correct |
40 ms |
3524 KB |
Output is correct |
68 |
Correct |
28 ms |
3604 KB |
Output is correct |
69 |
Correct |
35 ms |
5836 KB |
Output is correct |
70 |
Correct |
303 ms |
33380 KB |
Output is correct |
71 |
Correct |
38 ms |
6348 KB |
Output is correct |
72 |
Correct |
292 ms |
54192 KB |
Output is correct |
73 |
Correct |
268 ms |
54140 KB |
Output is correct |
74 |
Correct |
1 ms |
340 KB |
Output is correct |
75 |
Correct |
1 ms |
336 KB |
Output is correct |
76 |
Correct |
1 ms |
336 KB |
Output is correct |
77 |
Correct |
1 ms |
336 KB |
Output is correct |
78 |
Correct |
1 ms |
336 KB |
Output is correct |
79 |
Correct |
3 ms |
1116 KB |
Output is correct |
80 |
Correct |
310 ms |
54124 KB |
Output is correct |
81 |
Correct |
315 ms |
54124 KB |
Output is correct |
82 |
Correct |
293 ms |
54188 KB |
Output is correct |
83 |
Correct |
298 ms |
54252 KB |
Output is correct |
84 |
Correct |
291 ms |
54196 KB |
Output is correct |
85 |
Correct |
296 ms |
54188 KB |
Output is correct |
86 |
Correct |
82 ms |
20668 KB |
Output is correct |
87 |
Correct |
1 ms |
336 KB |
Output is correct |