#include <bits/stdc++.h>
#define dbg() cerr <<
#define name(x) (#x) << ": " << (x) << ' ' <<
// 0(B+1) 1 2 3 ... B-2 B-1
// 1(B+1) B+2 B+3 B+4 ... 2B-1 2B
// 2(B+1) 2B+3 ....
// ---
// X(B+1)
// vreau ca X(B+1) = 0 (mod A) => X(B+1) multiplu de A => X(B + 1) = lcm(A, B + 1)
// X(B + 1) = A * (B + 1) / GCD(A, B + 1) => X = A / GCD(A, B + 1)
using namespace std;
const long long INF = numeric_limits<long long>::max();
long long GCD(long long a, long long b) {
if (!b) return a;
return GCD(b, a % b);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
auto Print = [&](long long ans) {
cout << ans << endl;
exit(0);
};
long long n, a, b; cin >> n >> a >> b;
// for (int t = 0; t < 20; ++t) {
// dbg() (t + t / b) % a << ' ' << t % b << endl;
// }
vector<pair<long long, long long>> segm(n);
long long total = 0;
for (auto &p : segm) {
cin >> p.first >> p.second;
total += p.second - p.first + 1;
}
long long x = a / GCD(a, b + 1);
if (x >= INF / b) { // x e ft mare => in prima perioada
Print(total);
}
long long t = x * b; // perioada
// dbg() "perioada este " << name(t) endl;
vector<pair<long long, long long>> v; v.reserve(2 * n);
for (auto &p : segm) {
if (p.second - p.first + 1 >= t) {
Print(t);
}
p.first %= t; p.second %= t;
if (p.first <= p.second) {
v.emplace_back(p);
} else {
v.emplace_back(p.first, t - 1);
v.emplace_back(0LL, p.second);
}
}
sort(v.begin(), v.end());
long long rgt = -1, ans = 0LL;
for (auto &p : v) {
// dbg() name(p.first) name(p.second) endl;
if (p.second > rgt) {
ans += p.second - max(p.first, rgt + 1) + 1;
rgt = p.second;
}
}
Print(ans);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
8 ms |
1016 KB |
Output is correct |
3 |
Correct |
8 ms |
1016 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
380 KB |
Output is correct |
9 |
Correct |
2 ms |
372 KB |
Output is correct |
10 |
Correct |
8 ms |
376 KB |
Output is correct |
11 |
Correct |
1 ms |
376 KB |
Output is correct |
12 |
Correct |
0 ms |
376 KB |
Output is correct |
13 |
Correct |
0 ms |
376 KB |
Output is correct |
14 |
Correct |
4 ms |
344 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
8 ms |
1016 KB |
Output is correct |
17 |
Correct |
70 ms |
7288 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
472 ms |
56868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
380 KB |
Output is correct |
2 |
Correct |
623 ms |
68828 KB |
Output is correct |
3 |
Correct |
626 ms |
69020 KB |
Output is correct |
4 |
Correct |
650 ms |
68988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
380 KB |
Output is correct |
2 |
Correct |
623 ms |
68828 KB |
Output is correct |
3 |
Correct |
626 ms |
69020 KB |
Output is correct |
4 |
Correct |
650 ms |
68988 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
618 ms |
68896 KB |
Output is correct |
7 |
Correct |
618 ms |
68856 KB |
Output is correct |
8 |
Correct |
638 ms |
68856 KB |
Output is correct |
9 |
Correct |
681 ms |
68824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
380 KB |
Output is correct |
2 |
Correct |
623 ms |
68828 KB |
Output is correct |
3 |
Correct |
626 ms |
69020 KB |
Output is correct |
4 |
Correct |
650 ms |
68988 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
61 ms |
7192 KB |
Output is correct |
7 |
Correct |
66 ms |
7288 KB |
Output is correct |
8 |
Correct |
62 ms |
7416 KB |
Output is correct |
9 |
Correct |
64 ms |
7160 KB |
Output is correct |
10 |
Correct |
62 ms |
7200 KB |
Output is correct |
11 |
Correct |
65 ms |
7288 KB |
Output is correct |
12 |
Correct |
61 ms |
7160 KB |
Output is correct |
13 |
Correct |
69 ms |
7200 KB |
Output is correct |
14 |
Correct |
61 ms |
7228 KB |
Output is correct |
15 |
Correct |
69 ms |
7224 KB |
Output is correct |
16 |
Correct |
67 ms |
7160 KB |
Output is correct |
17 |
Correct |
67 ms |
7196 KB |
Output is correct |
18 |
Correct |
621 ms |
67728 KB |
Output is correct |
19 |
Correct |
614 ms |
68848 KB |
Output is correct |
20 |
Correct |
685 ms |
68828 KB |
Output is correct |
21 |
Correct |
68 ms |
7160 KB |
Output is correct |
22 |
Correct |
61 ms |
7188 KB |
Output is correct |
23 |
Correct |
194 ms |
23632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
376 KB |
Output is correct |
2 |
Correct |
67 ms |
7160 KB |
Output is correct |
3 |
Correct |
69 ms |
7160 KB |
Output is correct |
4 |
Correct |
710 ms |
68936 KB |
Output is correct |
5 |
Correct |
68 ms |
7224 KB |
Output is correct |
6 |
Correct |
67 ms |
7288 KB |
Output is correct |
7 |
Correct |
66 ms |
7160 KB |
Output is correct |
8 |
Correct |
68 ms |
7160 KB |
Output is correct |
9 |
Correct |
66 ms |
7160 KB |
Output is correct |
10 |
Correct |
67 ms |
7220 KB |
Output is correct |
11 |
Correct |
67 ms |
7160 KB |
Output is correct |
12 |
Correct |
60 ms |
7160 KB |
Output is correct |
13 |
Correct |
70 ms |
7288 KB |
Output is correct |
14 |
Correct |
677 ms |
68828 KB |
Output is correct |
15 |
Correct |
66 ms |
7160 KB |
Output is correct |
16 |
Correct |
618 ms |
68768 KB |
Output is correct |
17 |
Correct |
608 ms |
68856 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
8 ms |
1016 KB |
Output is correct |
3 |
Correct |
8 ms |
1016 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
380 KB |
Output is correct |
9 |
Correct |
2 ms |
372 KB |
Output is correct |
10 |
Correct |
8 ms |
376 KB |
Output is correct |
11 |
Correct |
1 ms |
376 KB |
Output is correct |
12 |
Correct |
0 ms |
376 KB |
Output is correct |
13 |
Correct |
0 ms |
376 KB |
Output is correct |
14 |
Correct |
4 ms |
344 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
8 ms |
1016 KB |
Output is correct |
17 |
Correct |
70 ms |
7288 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
3 ms |
376 KB |
Output is correct |
27 |
Correct |
3 ms |
376 KB |
Output is correct |
28 |
Correct |
472 ms |
56868 KB |
Output is correct |
29 |
Correct |
2 ms |
380 KB |
Output is correct |
30 |
Correct |
623 ms |
68828 KB |
Output is correct |
31 |
Correct |
626 ms |
69020 KB |
Output is correct |
32 |
Correct |
650 ms |
68988 KB |
Output is correct |
33 |
Correct |
2 ms |
376 KB |
Output is correct |
34 |
Correct |
618 ms |
68896 KB |
Output is correct |
35 |
Correct |
618 ms |
68856 KB |
Output is correct |
36 |
Correct |
638 ms |
68856 KB |
Output is correct |
37 |
Correct |
681 ms |
68824 KB |
Output is correct |
38 |
Correct |
2 ms |
376 KB |
Output is correct |
39 |
Correct |
61 ms |
7192 KB |
Output is correct |
40 |
Correct |
66 ms |
7288 KB |
Output is correct |
41 |
Correct |
62 ms |
7416 KB |
Output is correct |
42 |
Correct |
64 ms |
7160 KB |
Output is correct |
43 |
Correct |
62 ms |
7200 KB |
Output is correct |
44 |
Correct |
65 ms |
7288 KB |
Output is correct |
45 |
Correct |
61 ms |
7160 KB |
Output is correct |
46 |
Correct |
69 ms |
7200 KB |
Output is correct |
47 |
Correct |
61 ms |
7228 KB |
Output is correct |
48 |
Correct |
69 ms |
7224 KB |
Output is correct |
49 |
Correct |
67 ms |
7160 KB |
Output is correct |
50 |
Correct |
67 ms |
7196 KB |
Output is correct |
51 |
Correct |
621 ms |
67728 KB |
Output is correct |
52 |
Correct |
614 ms |
68848 KB |
Output is correct |
53 |
Correct |
685 ms |
68828 KB |
Output is correct |
54 |
Correct |
68 ms |
7160 KB |
Output is correct |
55 |
Correct |
61 ms |
7188 KB |
Output is correct |
56 |
Correct |
194 ms |
23632 KB |
Output is correct |
57 |
Correct |
1 ms |
376 KB |
Output is correct |
58 |
Correct |
67 ms |
7160 KB |
Output is correct |
59 |
Correct |
69 ms |
7160 KB |
Output is correct |
60 |
Correct |
710 ms |
68936 KB |
Output is correct |
61 |
Correct |
68 ms |
7224 KB |
Output is correct |
62 |
Correct |
67 ms |
7288 KB |
Output is correct |
63 |
Correct |
66 ms |
7160 KB |
Output is correct |
64 |
Correct |
68 ms |
7160 KB |
Output is correct |
65 |
Correct |
66 ms |
7160 KB |
Output is correct |
66 |
Correct |
67 ms |
7220 KB |
Output is correct |
67 |
Correct |
67 ms |
7160 KB |
Output is correct |
68 |
Correct |
60 ms |
7160 KB |
Output is correct |
69 |
Correct |
70 ms |
7288 KB |
Output is correct |
70 |
Correct |
677 ms |
68828 KB |
Output is correct |
71 |
Correct |
66 ms |
7160 KB |
Output is correct |
72 |
Correct |
618 ms |
68768 KB |
Output is correct |
73 |
Correct |
608 ms |
68856 KB |
Output is correct |
74 |
Correct |
2 ms |
376 KB |
Output is correct |
75 |
Correct |
2 ms |
376 KB |
Output is correct |
76 |
Correct |
2 ms |
376 KB |
Output is correct |
77 |
Correct |
2 ms |
376 KB |
Output is correct |
78 |
Correct |
2 ms |
376 KB |
Output is correct |
79 |
Correct |
8 ms |
1016 KB |
Output is correct |
80 |
Correct |
700 ms |
68856 KB |
Output is correct |
81 |
Correct |
710 ms |
68900 KB |
Output is correct |
82 |
Correct |
674 ms |
68856 KB |
Output is correct |
83 |
Correct |
685 ms |
68856 KB |
Output is correct |
84 |
Correct |
676 ms |
68856 KB |
Output is correct |
85 |
Correct |
686 ms |
68892 KB |
Output is correct |
86 |
Correct |
195 ms |
23564 KB |
Output is correct |
87 |
Correct |
2 ms |
376 KB |
Output is correct |