#include "bits/stdc++.h"
using namespace std;
long long GCD(long long X, long long Y) {
return (Y == 0 ? X : GCD(Y, X % Y));
}
int main() {
int N;
long long A, B;
cin >> N >> A >> B;
vector<pair<long long, long long> > P(N);
for (int i = 0; i < N; i++) {
cin >> P[i].first >> P[i].second;
}
long long C = A / GCD(B + 1, A);
if (B > 2000000000000000000 / C) {
long long ANS = 0;
for (int i = 0; i < N; i++) {
ANS += P[i].second - P[i].first + 1;
}
cout << ANS << endl;
}
else {
long long D = B * C;
for (int i = 0; i < N; i++) {
if (P[i].first / D + 2 <= P[i].second / D) {
cout << D << endl;
return 0;
}
}
vector<pair<long long, long long> > W;
for (int i = 0; i < N; i++) {
if (P[i].first / D == P[i].second / D) W.push_back({ P[i].first % D, P[i].second % D });
else W.push_back({ P[i].first % D, D - 1 }), W.push_back({ 0, P[i].second % D });
}
sort(W.begin(), W.end());
long long ANS = 0;
pair<long long, long long> Cur = { 0, -1 };
for (int i = 0; i < W.size(); i++) {
if (Cur.second < W[i].first) ANS += Cur.second - Cur.first + 1, Cur = W[i];
else Cur.second = max(Cur.second, W[i].second);
}
ANS += Cur.second - Cur.first + 1;
cout << ANS << endl;
}
}
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:40:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < W.size(); i++) {
~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
22 ms |
1272 KB |
Output is correct |
3 |
Correct |
23 ms |
1272 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
22 ms |
1280 KB |
Output is correct |
17 |
Correct |
219 ms |
7788 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
1682 ms |
58112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
2197 ms |
69916 KB |
Output is correct |
3 |
Correct |
2133 ms |
69888 KB |
Output is correct |
4 |
Correct |
2143 ms |
70068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
2197 ms |
69916 KB |
Output is correct |
3 |
Correct |
2133 ms |
69888 KB |
Output is correct |
4 |
Correct |
2143 ms |
70068 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
2172 ms |
69732 KB |
Output is correct |
7 |
Correct |
2167 ms |
70152 KB |
Output is correct |
8 |
Correct |
2155 ms |
70364 KB |
Output is correct |
9 |
Correct |
2202 ms |
70108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
2197 ms |
69916 KB |
Output is correct |
3 |
Correct |
2133 ms |
69888 KB |
Output is correct |
4 |
Correct |
2143 ms |
70068 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
218 ms |
7788 KB |
Output is correct |
7 |
Correct |
216 ms |
7788 KB |
Output is correct |
8 |
Correct |
211 ms |
7788 KB |
Output is correct |
9 |
Correct |
213 ms |
7788 KB |
Output is correct |
10 |
Correct |
212 ms |
7788 KB |
Output is correct |
11 |
Correct |
218 ms |
7788 KB |
Output is correct |
12 |
Correct |
226 ms |
7792 KB |
Output is correct |
13 |
Correct |
220 ms |
7788 KB |
Output is correct |
14 |
Correct |
226 ms |
8044 KB |
Output is correct |
15 |
Correct |
223 ms |
7932 KB |
Output is correct |
16 |
Correct |
220 ms |
7944 KB |
Output is correct |
17 |
Correct |
215 ms |
7832 KB |
Output is correct |
18 |
Correct |
2171 ms |
70236 KB |
Output is correct |
19 |
Correct |
2132 ms |
70104 KB |
Output is correct |
20 |
Correct |
2241 ms |
69844 KB |
Output is correct |
21 |
Correct |
225 ms |
7788 KB |
Output is correct |
22 |
Correct |
216 ms |
7788 KB |
Output is correct |
23 |
Correct |
727 ms |
26852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
247 ms |
8060 KB |
Output is correct |
3 |
Correct |
225 ms |
7916 KB |
Output is correct |
4 |
Correct |
2265 ms |
70008 KB |
Output is correct |
5 |
Correct |
219 ms |
7788 KB |
Output is correct |
6 |
Correct |
217 ms |
7788 KB |
Output is correct |
7 |
Correct |
216 ms |
7788 KB |
Output is correct |
8 |
Correct |
226 ms |
7916 KB |
Output is correct |
9 |
Correct |
218 ms |
7788 KB |
Output is correct |
10 |
Correct |
224 ms |
7812 KB |
Output is correct |
11 |
Correct |
221 ms |
7788 KB |
Output is correct |
12 |
Correct |
211 ms |
7788 KB |
Output is correct |
13 |
Correct |
219 ms |
7916 KB |
Output is correct |
14 |
Correct |
2274 ms |
70080 KB |
Output is correct |
15 |
Correct |
220 ms |
7788 KB |
Output is correct |
16 |
Correct |
2200 ms |
70212 KB |
Output is correct |
17 |
Correct |
2172 ms |
70200 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
22 ms |
1272 KB |
Output is correct |
3 |
Correct |
23 ms |
1272 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
22 ms |
1280 KB |
Output is correct |
17 |
Correct |
219 ms |
7788 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
256 KB |
Output is correct |
20 |
Correct |
1 ms |
256 KB |
Output is correct |
21 |
Correct |
1 ms |
256 KB |
Output is correct |
22 |
Correct |
1 ms |
256 KB |
Output is correct |
23 |
Correct |
1 ms |
256 KB |
Output is correct |
24 |
Correct |
1 ms |
256 KB |
Output is correct |
25 |
Correct |
3 ms |
384 KB |
Output is correct |
26 |
Correct |
3 ms |
384 KB |
Output is correct |
27 |
Correct |
4 ms |
384 KB |
Output is correct |
28 |
Correct |
1682 ms |
58112 KB |
Output is correct |
29 |
Correct |
0 ms |
256 KB |
Output is correct |
30 |
Correct |
2197 ms |
69916 KB |
Output is correct |
31 |
Correct |
2133 ms |
69888 KB |
Output is correct |
32 |
Correct |
2143 ms |
70068 KB |
Output is correct |
33 |
Correct |
1 ms |
256 KB |
Output is correct |
34 |
Correct |
2172 ms |
69732 KB |
Output is correct |
35 |
Correct |
2167 ms |
70152 KB |
Output is correct |
36 |
Correct |
2155 ms |
70364 KB |
Output is correct |
37 |
Correct |
2202 ms |
70108 KB |
Output is correct |
38 |
Correct |
0 ms |
256 KB |
Output is correct |
39 |
Correct |
218 ms |
7788 KB |
Output is correct |
40 |
Correct |
216 ms |
7788 KB |
Output is correct |
41 |
Correct |
211 ms |
7788 KB |
Output is correct |
42 |
Correct |
213 ms |
7788 KB |
Output is correct |
43 |
Correct |
212 ms |
7788 KB |
Output is correct |
44 |
Correct |
218 ms |
7788 KB |
Output is correct |
45 |
Correct |
226 ms |
7792 KB |
Output is correct |
46 |
Correct |
220 ms |
7788 KB |
Output is correct |
47 |
Correct |
226 ms |
8044 KB |
Output is correct |
48 |
Correct |
223 ms |
7932 KB |
Output is correct |
49 |
Correct |
220 ms |
7944 KB |
Output is correct |
50 |
Correct |
215 ms |
7832 KB |
Output is correct |
51 |
Correct |
2171 ms |
70236 KB |
Output is correct |
52 |
Correct |
2132 ms |
70104 KB |
Output is correct |
53 |
Correct |
2241 ms |
69844 KB |
Output is correct |
54 |
Correct |
225 ms |
7788 KB |
Output is correct |
55 |
Correct |
216 ms |
7788 KB |
Output is correct |
56 |
Correct |
727 ms |
26852 KB |
Output is correct |
57 |
Correct |
1 ms |
256 KB |
Output is correct |
58 |
Correct |
247 ms |
8060 KB |
Output is correct |
59 |
Correct |
225 ms |
7916 KB |
Output is correct |
60 |
Correct |
2265 ms |
70008 KB |
Output is correct |
61 |
Correct |
219 ms |
7788 KB |
Output is correct |
62 |
Correct |
217 ms |
7788 KB |
Output is correct |
63 |
Correct |
216 ms |
7788 KB |
Output is correct |
64 |
Correct |
226 ms |
7916 KB |
Output is correct |
65 |
Correct |
218 ms |
7788 KB |
Output is correct |
66 |
Correct |
224 ms |
7812 KB |
Output is correct |
67 |
Correct |
221 ms |
7788 KB |
Output is correct |
68 |
Correct |
211 ms |
7788 KB |
Output is correct |
69 |
Correct |
219 ms |
7916 KB |
Output is correct |
70 |
Correct |
2274 ms |
70080 KB |
Output is correct |
71 |
Correct |
220 ms |
7788 KB |
Output is correct |
72 |
Correct |
2200 ms |
70212 KB |
Output is correct |
73 |
Correct |
2172 ms |
70200 KB |
Output is correct |
74 |
Correct |
1 ms |
256 KB |
Output is correct |
75 |
Correct |
0 ms |
256 KB |
Output is correct |
76 |
Correct |
1 ms |
256 KB |
Output is correct |
77 |
Correct |
1 ms |
256 KB |
Output is correct |
78 |
Correct |
0 ms |
256 KB |
Output is correct |
79 |
Correct |
23 ms |
1280 KB |
Output is correct |
80 |
Correct |
2329 ms |
69656 KB |
Output is correct |
81 |
Correct |
2311 ms |
69816 KB |
Output is correct |
82 |
Correct |
2214 ms |
69520 KB |
Output is correct |
83 |
Correct |
2208 ms |
69384 KB |
Output is correct |
84 |
Correct |
2233 ms |
69328 KB |
Output is correct |
85 |
Correct |
2250 ms |
69604 KB |
Output is correct |
86 |
Correct |
725 ms |
26596 KB |
Output is correct |
87 |
Correct |
1 ms |
256 KB |
Output is correct |