/*
Author : DeMen100ns (a.k.a Vo Khac Trieu)
School : VNU-HCM High school for the Gifted
fuck you adhoc
*/
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5;
const long long INF = 5e18 + 7;
const int MAXA = 1e9;
const int B = sqrt(N) + 5;
int lcm(int a, int b){
int ans = a / __gcd(a, b);
if (INF / ans < b){
return INF;
}
return ans * b;
}
int calperiod(int A, int B){
/*for(int i = 1; i <= 1000; ++i){
if ((i + i / B) % A == 0 && i % B == 0) return i;
}*/
//lcm(B + 1, A)...
int g = lcm(B + 1, A);
if (g == INF) return INF;
return (g / (B + 1)) * B;
}
void solve()
{
int n, A, B; cin >> n >> A >> B;
int g = calperiod(A, B);
vector <pair<int, int>> range;
for(int i = 1; i <= n; ++i){
int x, y; cin >> x >> y;
if (y - x + 1 >= g){
cout << g << endl;
return;
}
x %= g; y %= g;
if (x <= y) range.push_back({x, y});
else{
range.push_back({x, g - 1});
range.push_back({0, y});
}
}
sort(range.begin(), range.end());
int ans = range[0].second - range[0].first + 1;
int r = range[0].second;
for(int i = 1; i < (int)range.size(); ++i){
if (range[i].second > r){
if (range[i].first > r){
ans += range[i].second - range[i].first + 1;
} else {
ans += range[i].second - r;
}
r = range[i].second;
}
}
cout << ans;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("codeforces.inp","r",stdin);
// freopen("codeforces.out","w",stdout);
int t = 1; // cin >> t;
while (t--)
{
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
6 ms |
1088 KB |
Output is correct |
3 |
Correct |
6 ms |
992 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
6 ms |
1056 KB |
Output is correct |
17 |
Correct |
42 ms |
3528 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
273 ms |
16900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
380 ms |
16864 KB |
Output is correct |
3 |
Correct |
375 ms |
17280 KB |
Output is correct |
4 |
Correct |
373 ms |
17364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
380 ms |
16864 KB |
Output is correct |
3 |
Correct |
375 ms |
17280 KB |
Output is correct |
4 |
Correct |
373 ms |
17364 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
382 ms |
17412 KB |
Output is correct |
7 |
Correct |
369 ms |
16820 KB |
Output is correct |
8 |
Correct |
370 ms |
16988 KB |
Output is correct |
9 |
Correct |
419 ms |
17272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
380 ms |
16864 KB |
Output is correct |
3 |
Correct |
375 ms |
17280 KB |
Output is correct |
4 |
Correct |
373 ms |
17364 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
37 ms |
3524 KB |
Output is correct |
7 |
Correct |
41 ms |
3464 KB |
Output is correct |
8 |
Correct |
38 ms |
3524 KB |
Output is correct |
9 |
Correct |
39 ms |
3428 KB |
Output is correct |
10 |
Correct |
39 ms |
3372 KB |
Output is correct |
11 |
Correct |
38 ms |
3340 KB |
Output is correct |
12 |
Correct |
37 ms |
3328 KB |
Output is correct |
13 |
Correct |
40 ms |
3408 KB |
Output is correct |
14 |
Correct |
44 ms |
3300 KB |
Output is correct |
15 |
Correct |
42 ms |
3280 KB |
Output is correct |
16 |
Correct |
41 ms |
3380 KB |
Output is correct |
17 |
Correct |
38 ms |
2836 KB |
Output is correct |
18 |
Correct |
380 ms |
17152 KB |
Output is correct |
19 |
Correct |
383 ms |
17048 KB |
Output is correct |
20 |
Correct |
425 ms |
17068 KB |
Output is correct |
21 |
Correct |
45 ms |
3272 KB |
Output is correct |
22 |
Correct |
34 ms |
3268 KB |
Output is correct |
23 |
Correct |
124 ms |
9300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
45 ms |
3068 KB |
Output is correct |
3 |
Correct |
42 ms |
3132 KB |
Output is correct |
4 |
Correct |
445 ms |
17036 KB |
Output is correct |
5 |
Correct |
40 ms |
3016 KB |
Output is correct |
6 |
Correct |
45 ms |
3124 KB |
Output is correct |
7 |
Correct |
41 ms |
3024 KB |
Output is correct |
8 |
Correct |
41 ms |
3144 KB |
Output is correct |
9 |
Correct |
40 ms |
3120 KB |
Output is correct |
10 |
Correct |
40 ms |
3016 KB |
Output is correct |
11 |
Correct |
40 ms |
2940 KB |
Output is correct |
12 |
Correct |
35 ms |
2968 KB |
Output is correct |
13 |
Correct |
40 ms |
3016 KB |
Output is correct |
14 |
Correct |
396 ms |
16812 KB |
Output is correct |
15 |
Correct |
41 ms |
2888 KB |
Output is correct |
16 |
Correct |
365 ms |
16920 KB |
Output is correct |
17 |
Correct |
356 ms |
16812 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
6 ms |
1088 KB |
Output is correct |
3 |
Correct |
6 ms |
992 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
6 ms |
1056 KB |
Output is correct |
17 |
Correct |
42 ms |
3528 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
316 KB |
Output is correct |
23 |
Correct |
1 ms |
320 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
320 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
273 ms |
16900 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
380 ms |
16864 KB |
Output is correct |
31 |
Correct |
375 ms |
17280 KB |
Output is correct |
32 |
Correct |
373 ms |
17364 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
382 ms |
17412 KB |
Output is correct |
35 |
Correct |
369 ms |
16820 KB |
Output is correct |
36 |
Correct |
370 ms |
16988 KB |
Output is correct |
37 |
Correct |
419 ms |
17272 KB |
Output is correct |
38 |
Correct |
0 ms |
320 KB |
Output is correct |
39 |
Correct |
37 ms |
3524 KB |
Output is correct |
40 |
Correct |
41 ms |
3464 KB |
Output is correct |
41 |
Correct |
38 ms |
3524 KB |
Output is correct |
42 |
Correct |
39 ms |
3428 KB |
Output is correct |
43 |
Correct |
39 ms |
3372 KB |
Output is correct |
44 |
Correct |
38 ms |
3340 KB |
Output is correct |
45 |
Correct |
37 ms |
3328 KB |
Output is correct |
46 |
Correct |
40 ms |
3408 KB |
Output is correct |
47 |
Correct |
44 ms |
3300 KB |
Output is correct |
48 |
Correct |
42 ms |
3280 KB |
Output is correct |
49 |
Correct |
41 ms |
3380 KB |
Output is correct |
50 |
Correct |
38 ms |
2836 KB |
Output is correct |
51 |
Correct |
380 ms |
17152 KB |
Output is correct |
52 |
Correct |
383 ms |
17048 KB |
Output is correct |
53 |
Correct |
425 ms |
17068 KB |
Output is correct |
54 |
Correct |
45 ms |
3272 KB |
Output is correct |
55 |
Correct |
34 ms |
3268 KB |
Output is correct |
56 |
Correct |
124 ms |
9300 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
45 ms |
3068 KB |
Output is correct |
59 |
Correct |
42 ms |
3132 KB |
Output is correct |
60 |
Correct |
445 ms |
17036 KB |
Output is correct |
61 |
Correct |
40 ms |
3016 KB |
Output is correct |
62 |
Correct |
45 ms |
3124 KB |
Output is correct |
63 |
Correct |
41 ms |
3024 KB |
Output is correct |
64 |
Correct |
41 ms |
3144 KB |
Output is correct |
65 |
Correct |
40 ms |
3120 KB |
Output is correct |
66 |
Correct |
40 ms |
3016 KB |
Output is correct |
67 |
Correct |
40 ms |
2940 KB |
Output is correct |
68 |
Correct |
35 ms |
2968 KB |
Output is correct |
69 |
Correct |
40 ms |
3016 KB |
Output is correct |
70 |
Correct |
396 ms |
16812 KB |
Output is correct |
71 |
Correct |
41 ms |
2888 KB |
Output is correct |
72 |
Correct |
365 ms |
16920 KB |
Output is correct |
73 |
Correct |
356 ms |
16812 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
320 KB |
Output is correct |
76 |
Correct |
0 ms |
212 KB |
Output is correct |
77 |
Correct |
1 ms |
324 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
4 ms |
988 KB |
Output is correct |
80 |
Correct |
444 ms |
53208 KB |
Output is correct |
81 |
Correct |
468 ms |
53152 KB |
Output is correct |
82 |
Correct |
421 ms |
53132 KB |
Output is correct |
83 |
Correct |
423 ms |
53112 KB |
Output is correct |
84 |
Correct |
420 ms |
53104 KB |
Output is correct |
85 |
Correct |
418 ms |
53220 KB |
Output is correct |
86 |
Correct |
139 ms |
18492 KB |
Output is correct |
87 |
Correct |
0 ms |
212 KB |
Output is correct |