#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <deque>
#include <map>
#include <set>
#include <complex>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <chrono>
#include <ctime>
#define ft first
#define sc second
#define pb push_back
#define len(v) (int)v.size()
#define int ll
using namespace std;
typedef long long ll;
typedef long double ld;
typedef __int128 bigint;
mt19937 rnd(chrono::steady_clock().now().time_since_epoch().count());
bigint gcd(bigint a, bigint b) {
if(a == 0 || b == 0)
return a + b;
return gcd(b, a % b);
}
signed main() {
#ifdef PC
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, a, b, ans = 0;
cin >> n >> a >> b;
bigint a1 = a, b1 = b;
bigint k = ((b1 + 1) * a1 / gcd(b1 + 1, a1)) / (b1 + 1) * b1;
// cout << k << endl;
// for (int t = 0; t <= 10; t++) {
// cout << "T " << t << " " << (t + (t / b)) % a << " " << t % b << endl;
// }
vector<pair<int, int>> have;
for (int i = 0; i < n; i++) {
int l, r;
cin >> l >> r;
if(r - l >= k) {
have.pb({0, k - 1});
continue;
}
int p1 = l % k, p2 = r % k;
if(p2 >= p1) {
have.pb({p1, p2});
}
else {
have.pb({p1, k - 1});
have.pb({0, p2});
}
}
sort(have.begin(), have.end());
int l = have[0].ft, r = have[0].sc;
for (int i = 1; i < len(have); i++) {
if(have[i].ft <= r) {
r = max(r, have[i].sc);
continue;
}
else {
ans += (r - l + 1);
l = have[i].ft, r = have[i].sc;
}
}
ans += (r - l + 1);
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
10 ms |
896 KB |
Output is correct |
3 |
Correct |
10 ms |
896 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
348 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
9 ms |
896 KB |
Output is correct |
17 |
Correct |
63 ms |
2800 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
377 ms |
17244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
516 ms |
16980 KB |
Output is correct |
3 |
Correct |
516 ms |
17180 KB |
Output is correct |
4 |
Correct |
525 ms |
16900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
516 ms |
16980 KB |
Output is correct |
3 |
Correct |
516 ms |
17180 KB |
Output is correct |
4 |
Correct |
525 ms |
16900 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
509 ms |
16956 KB |
Output is correct |
7 |
Correct |
739 ms |
16980 KB |
Output is correct |
8 |
Correct |
519 ms |
16980 KB |
Output is correct |
9 |
Correct |
571 ms |
16980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
516 ms |
16980 KB |
Output is correct |
3 |
Correct |
516 ms |
17180 KB |
Output is correct |
4 |
Correct |
525 ms |
16900 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
58 ms |
2800 KB |
Output is correct |
7 |
Correct |
60 ms |
2800 KB |
Output is correct |
8 |
Correct |
53 ms |
2804 KB |
Output is correct |
9 |
Correct |
57 ms |
2800 KB |
Output is correct |
10 |
Correct |
57 ms |
2768 KB |
Output is correct |
11 |
Correct |
56 ms |
2804 KB |
Output is correct |
12 |
Correct |
53 ms |
2800 KB |
Output is correct |
13 |
Correct |
59 ms |
2800 KB |
Output is correct |
14 |
Correct |
55 ms |
2808 KB |
Output is correct |
15 |
Correct |
59 ms |
2800 KB |
Output is correct |
16 |
Correct |
58 ms |
2796 KB |
Output is correct |
17 |
Correct |
56 ms |
2808 KB |
Output is correct |
18 |
Correct |
521 ms |
16980 KB |
Output is correct |
19 |
Correct |
501 ms |
16980 KB |
Output is correct |
20 |
Correct |
578 ms |
16980 KB |
Output is correct |
21 |
Correct |
59 ms |
2800 KB |
Output is correct |
22 |
Correct |
53 ms |
2800 KB |
Output is correct |
23 |
Correct |
164 ms |
8932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
56 ms |
2800 KB |
Output is correct |
3 |
Correct |
57 ms |
2800 KB |
Output is correct |
4 |
Correct |
611 ms |
17112 KB |
Output is correct |
5 |
Correct |
59 ms |
2800 KB |
Output is correct |
6 |
Correct |
57 ms |
2800 KB |
Output is correct |
7 |
Correct |
59 ms |
2800 KB |
Output is correct |
8 |
Correct |
63 ms |
2792 KB |
Output is correct |
9 |
Correct |
60 ms |
2800 KB |
Output is correct |
10 |
Correct |
58 ms |
2844 KB |
Output is correct |
11 |
Correct |
60 ms |
2800 KB |
Output is correct |
12 |
Correct |
54 ms |
2808 KB |
Output is correct |
13 |
Correct |
58 ms |
2800 KB |
Output is correct |
14 |
Correct |
564 ms |
17236 KB |
Output is correct |
15 |
Correct |
58 ms |
2800 KB |
Output is correct |
16 |
Correct |
508 ms |
16980 KB |
Output is correct |
17 |
Correct |
489 ms |
16984 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
10 ms |
896 KB |
Output is correct |
3 |
Correct |
10 ms |
896 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
348 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
9 ms |
896 KB |
Output is correct |
17 |
Correct |
63 ms |
2800 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
512 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
377 ms |
17244 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
516 ms |
16980 KB |
Output is correct |
31 |
Correct |
516 ms |
17180 KB |
Output is correct |
32 |
Correct |
525 ms |
16900 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
509 ms |
16956 KB |
Output is correct |
35 |
Correct |
739 ms |
16980 KB |
Output is correct |
36 |
Correct |
519 ms |
16980 KB |
Output is correct |
37 |
Correct |
571 ms |
16980 KB |
Output is correct |
38 |
Correct |
5 ms |
384 KB |
Output is correct |
39 |
Correct |
58 ms |
2800 KB |
Output is correct |
40 |
Correct |
60 ms |
2800 KB |
Output is correct |
41 |
Correct |
53 ms |
2804 KB |
Output is correct |
42 |
Correct |
57 ms |
2800 KB |
Output is correct |
43 |
Correct |
57 ms |
2768 KB |
Output is correct |
44 |
Correct |
56 ms |
2804 KB |
Output is correct |
45 |
Correct |
53 ms |
2800 KB |
Output is correct |
46 |
Correct |
59 ms |
2800 KB |
Output is correct |
47 |
Correct |
55 ms |
2808 KB |
Output is correct |
48 |
Correct |
59 ms |
2800 KB |
Output is correct |
49 |
Correct |
58 ms |
2796 KB |
Output is correct |
50 |
Correct |
56 ms |
2808 KB |
Output is correct |
51 |
Correct |
521 ms |
16980 KB |
Output is correct |
52 |
Correct |
501 ms |
16980 KB |
Output is correct |
53 |
Correct |
578 ms |
16980 KB |
Output is correct |
54 |
Correct |
59 ms |
2800 KB |
Output is correct |
55 |
Correct |
53 ms |
2800 KB |
Output is correct |
56 |
Correct |
164 ms |
8932 KB |
Output is correct |
57 |
Correct |
5 ms |
384 KB |
Output is correct |
58 |
Correct |
56 ms |
2800 KB |
Output is correct |
59 |
Correct |
57 ms |
2800 KB |
Output is correct |
60 |
Correct |
611 ms |
17112 KB |
Output is correct |
61 |
Correct |
59 ms |
2800 KB |
Output is correct |
62 |
Correct |
57 ms |
2800 KB |
Output is correct |
63 |
Correct |
59 ms |
2800 KB |
Output is correct |
64 |
Correct |
63 ms |
2792 KB |
Output is correct |
65 |
Correct |
60 ms |
2800 KB |
Output is correct |
66 |
Correct |
58 ms |
2844 KB |
Output is correct |
67 |
Correct |
60 ms |
2800 KB |
Output is correct |
68 |
Correct |
54 ms |
2808 KB |
Output is correct |
69 |
Correct |
58 ms |
2800 KB |
Output is correct |
70 |
Correct |
564 ms |
17236 KB |
Output is correct |
71 |
Correct |
58 ms |
2800 KB |
Output is correct |
72 |
Correct |
508 ms |
16980 KB |
Output is correct |
73 |
Correct |
489 ms |
16984 KB |
Output is correct |
74 |
Correct |
5 ms |
384 KB |
Output is correct |
75 |
Correct |
5 ms |
384 KB |
Output is correct |
76 |
Correct |
5 ms |
384 KB |
Output is correct |
77 |
Correct |
5 ms |
384 KB |
Output is correct |
78 |
Correct |
5 ms |
384 KB |
Output is correct |
79 |
Correct |
10 ms |
1152 KB |
Output is correct |
80 |
Correct |
615 ms |
16900 KB |
Output is correct |
81 |
Correct |
598 ms |
17108 KB |
Output is correct |
82 |
Correct |
579 ms |
16980 KB |
Output is correct |
83 |
Correct |
562 ms |
17836 KB |
Output is correct |
84 |
Correct |
594 ms |
17748 KB |
Output is correct |
85 |
Correct |
556 ms |
17172 KB |
Output is correct |
86 |
Correct |
166 ms |
9664 KB |
Output is correct |
87 |
Correct |
5 ms |
384 KB |
Output is correct |