#include <bits/stdc++.h>
#define ll long long
#define lll __int128
#define endl "\n"
using namespace std;
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a % b);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
ll n, a, b;
cin >> n >> a >> b;
/*
for(int i = 1; i <= n; ++i) {
ll l, r;
cin >> l >> r;
for(int j = l; j <= r; ++j) {
cout << (j + j / b) % a << " " << j % b << endl;
}
}
*/
//return 0;
// a or b has certain pattern
// ab -> 1 cycle, no duplikat
// next cycle: sama persis
// maintain cycles in terms of ab, merge all segments
// find modulo ab
// exist more than one cycle long -> all possible
// size ab berarti diff >= ab - 1
vector<pair<lll, int>> sweep;
ll ans = -1;
lll tot;
if(a == b + 1)
tot = b;
else
tot = (lll)a * b / gcd(a, b + 1);
set<pair<ll, ll>> s;
// t + floor(t/b)
// every a * b reset (pasti)
//for(int j = 0; j <= a * b; ++j)
// s.insert(make_pair((ll)(j + j / b) % a, (ll)j % b));
//cout << s.size() << " " << (ll)tot << endl;
for(int i = 1; i <= n; ++i) {
ll l, r;
cin >> l >> r;
if(r - l >= tot - 1) {
ans = tot;
}
else {
if(tot <= 1e18) {
l = (l % tot);
r = (r % tot);
}
if(l <= r) {
sweep.push_back(make_pair(l, 0));
sweep.push_back(make_pair(r, 1));
}
else {
sweep.push_back(make_pair(l, 0));
sweep.push_back(make_pair(tot - 1, 1));
sweep.push_back(make_pair(0, 0));
sweep.push_back(make_pair(r, 1));
}
}
}
if(ans != -1)
cout << ans << endl, exit(0);
sort(sweep.begin(), sweep.end());
int cnt = 0;
ll st = -5;
ans = 0;
for(auto i : sweep) {
if(i.second) {
// end
--cnt;
if(cnt == 0)
ans += i.first - st;
}
else {
// start
// start itu + cnt
// set start kalo belum di set
if(cnt == 0)
st = i.first - 1;
++cnt;
}
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
6 ms |
1524 KB |
Output is correct |
3 |
Correct |
7 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
320 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 |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
6 ms |
1492 KB |
Output is correct |
17 |
Correct |
68 ms |
8740 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
418 ms |
66164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
630 ms |
66364 KB |
Output is correct |
3 |
Correct |
664 ms |
66452 KB |
Output is correct |
4 |
Correct |
641 ms |
68784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
630 ms |
66364 KB |
Output is correct |
3 |
Correct |
664 ms |
66452 KB |
Output is correct |
4 |
Correct |
641 ms |
68784 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
676 ms |
69200 KB |
Output is correct |
7 |
Correct |
611 ms |
68608 KB |
Output is correct |
8 |
Correct |
595 ms |
68612 KB |
Output is correct |
9 |
Correct |
747 ms |
68504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
630 ms |
66364 KB |
Output is correct |
3 |
Correct |
664 ms |
66452 KB |
Output is correct |
4 |
Correct |
641 ms |
68784 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
58 ms |
9896 KB |
Output is correct |
7 |
Correct |
66 ms |
10052 KB |
Output is correct |
8 |
Correct |
57 ms |
9932 KB |
Output is correct |
9 |
Correct |
59 ms |
9908 KB |
Output is correct |
10 |
Correct |
55 ms |
9888 KB |
Output is correct |
11 |
Correct |
60 ms |
9932 KB |
Output is correct |
12 |
Correct |
55 ms |
9872 KB |
Output is correct |
13 |
Correct |
63 ms |
9932 KB |
Output is correct |
14 |
Correct |
65 ms |
9940 KB |
Output is correct |
15 |
Correct |
88 ms |
9956 KB |
Output is correct |
16 |
Correct |
67 ms |
9908 KB |
Output is correct |
17 |
Correct |
56 ms |
9912 KB |
Output is correct |
18 |
Correct |
638 ms |
68408 KB |
Output is correct |
19 |
Correct |
586 ms |
67624 KB |
Output is correct |
20 |
Correct |
749 ms |
67696 KB |
Output is correct |
21 |
Correct |
70 ms |
9880 KB |
Output is correct |
22 |
Correct |
64 ms |
9952 KB |
Output is correct |
23 |
Correct |
171 ms |
34636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
64 ms |
8832 KB |
Output is correct |
3 |
Correct |
65 ms |
8732 KB |
Output is correct |
4 |
Correct |
818 ms |
66148 KB |
Output is correct |
5 |
Correct |
72 ms |
8800 KB |
Output is correct |
6 |
Correct |
68 ms |
8828 KB |
Output is correct |
7 |
Correct |
74 ms |
8820 KB |
Output is correct |
8 |
Correct |
73 ms |
8760 KB |
Output is correct |
9 |
Correct |
67 ms |
8816 KB |
Output is correct |
10 |
Correct |
74 ms |
8820 KB |
Output is correct |
11 |
Correct |
81 ms |
8888 KB |
Output is correct |
12 |
Correct |
64 ms |
8904 KB |
Output is correct |
13 |
Correct |
85 ms |
8788 KB |
Output is correct |
14 |
Correct |
780 ms |
66104 KB |
Output is correct |
15 |
Correct |
72 ms |
8900 KB |
Output is correct |
16 |
Correct |
582 ms |
67564 KB |
Output is correct |
17 |
Correct |
673 ms |
67732 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
6 ms |
1524 KB |
Output is correct |
3 |
Correct |
7 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
320 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 |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
6 ms |
1492 KB |
Output is correct |
17 |
Correct |
68 ms |
8740 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
324 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
418 ms |
66164 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
630 ms |
66364 KB |
Output is correct |
31 |
Correct |
664 ms |
66452 KB |
Output is correct |
32 |
Correct |
641 ms |
68784 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
676 ms |
69200 KB |
Output is correct |
35 |
Correct |
611 ms |
68608 KB |
Output is correct |
36 |
Correct |
595 ms |
68612 KB |
Output is correct |
37 |
Correct |
747 ms |
68504 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
58 ms |
9896 KB |
Output is correct |
40 |
Correct |
66 ms |
10052 KB |
Output is correct |
41 |
Correct |
57 ms |
9932 KB |
Output is correct |
42 |
Correct |
59 ms |
9908 KB |
Output is correct |
43 |
Correct |
55 ms |
9888 KB |
Output is correct |
44 |
Correct |
60 ms |
9932 KB |
Output is correct |
45 |
Correct |
55 ms |
9872 KB |
Output is correct |
46 |
Correct |
63 ms |
9932 KB |
Output is correct |
47 |
Correct |
65 ms |
9940 KB |
Output is correct |
48 |
Correct |
88 ms |
9956 KB |
Output is correct |
49 |
Correct |
67 ms |
9908 KB |
Output is correct |
50 |
Correct |
56 ms |
9912 KB |
Output is correct |
51 |
Correct |
638 ms |
68408 KB |
Output is correct |
52 |
Correct |
586 ms |
67624 KB |
Output is correct |
53 |
Correct |
749 ms |
67696 KB |
Output is correct |
54 |
Correct |
70 ms |
9880 KB |
Output is correct |
55 |
Correct |
64 ms |
9952 KB |
Output is correct |
56 |
Correct |
171 ms |
34636 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
64 ms |
8832 KB |
Output is correct |
59 |
Correct |
65 ms |
8732 KB |
Output is correct |
60 |
Correct |
818 ms |
66148 KB |
Output is correct |
61 |
Correct |
72 ms |
8800 KB |
Output is correct |
62 |
Correct |
68 ms |
8828 KB |
Output is correct |
63 |
Correct |
74 ms |
8820 KB |
Output is correct |
64 |
Correct |
73 ms |
8760 KB |
Output is correct |
65 |
Correct |
67 ms |
8816 KB |
Output is correct |
66 |
Correct |
74 ms |
8820 KB |
Output is correct |
67 |
Correct |
81 ms |
8888 KB |
Output is correct |
68 |
Correct |
64 ms |
8904 KB |
Output is correct |
69 |
Correct |
85 ms |
8788 KB |
Output is correct |
70 |
Correct |
780 ms |
66104 KB |
Output is correct |
71 |
Correct |
72 ms |
8900 KB |
Output is correct |
72 |
Correct |
582 ms |
67564 KB |
Output is correct |
73 |
Correct |
673 ms |
67732 KB |
Output is correct |
74 |
Correct |
1 ms |
340 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
1 ms |
340 KB |
Output is correct |
77 |
Correct |
1 ms |
212 KB |
Output is correct |
78 |
Correct |
1 ms |
328 KB |
Output is correct |
79 |
Correct |
6 ms |
1864 KB |
Output is correct |
80 |
Correct |
815 ms |
67668 KB |
Output is correct |
81 |
Correct |
1020 ms |
67720 KB |
Output is correct |
82 |
Correct |
784 ms |
67588 KB |
Output is correct |
83 |
Correct |
813 ms |
67716 KB |
Output is correct |
84 |
Correct |
851 ms |
67632 KB |
Output is correct |
85 |
Correct |
1066 ms |
67472 KB |
Output is correct |
86 |
Correct |
208 ms |
37116 KB |
Output is correct |
87 |
Correct |
1 ms |
320 KB |
Output is correct |