#include <bits/stdc++.h>
typedef long long ll;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
ll n, a, b;
std::cin >> n >> a >> b;
if (a <= b || b == 1) {
// (2x % a, 0)
// 2x % a == 0
// if a is odd, then the cycle is of length a (the 2 will never
// contribute)
// if a is even, then the cycle of of length a/2
ll cycle = a % 2 == 0 ? a / 2 : a;
if (a <= b) {
// w (B + 1) mod A = 0
if (b / std::gcd(a, b + 1) >= (ll(1e18) + 1) / a) {
cycle = ll(1e18) + 1;
} else {
cycle = a / std::gcd(a, b + 1) * b;
}
}
std::vector<std::pair<ll, ll>> ranges;
std::map<ll, ll> mp;
while (n--) {
ll l, r;
std::cin >> l >> r;
ll _l = l % cycle, _r = r % cycle;
if (r - l + 1 >= cycle) {
std::cout << cycle << "\n";
return 0;
}
if (_l > _r) {
ranges.push_back({_l, cycle - 1});
ranges.push_back({0, _r});
} else {
ranges.push_back({_l, _r});
}
}
for (auto &[l, r] : ranges) {
mp[l] = std::max(mp[l], r);
}
ll right = 0;
ll ans = 0;
for (auto &i : mp) {
if (i.second < right) {
continue;
}
right = std::max(right, i.first);
ans += i.second - right + 1;
right = i.second + 1;
}
std::cout << ans << "\n";
return 0;
}
std::set<std::pair<ll, ll>> st;
while (n--) {
ll l, r;
std::cin >> l >> r;
for (ll x = l; x <= r; ++x) {
st.insert({(x + x / b) % a, x % b});
// std::cout << "(" << (x + x / b) % a << " " << x % b << "), ";
// std::cout << st.size() << "\n";
}
}
std::cout << st.size() << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
4 ms |
1248 KB |
Output is correct |
3 |
Correct |
5 ms |
1088 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
456 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
21 ms |
7256 KB |
Output is correct |
17 |
Correct |
44 ms |
11744 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
99 ms |
32196 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
77 ms |
30696 KB |
Output is correct |
5 |
Correct |
180 ms |
41288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
465 ms |
78524 KB |
Output is correct |
3 |
Correct |
486 ms |
78504 KB |
Output is correct |
4 |
Correct |
503 ms |
78500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
465 ms |
78524 KB |
Output is correct |
3 |
Correct |
486 ms |
78504 KB |
Output is correct |
4 |
Correct |
503 ms |
78500 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Runtime error |
1814 ms |
524288 KB |
Execution killed with signal 9 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
465 ms |
78524 KB |
Output is correct |
3 |
Correct |
486 ms |
78504 KB |
Output is correct |
4 |
Correct |
503 ms |
78500 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
158 ms |
62888 KB |
Output is correct |
7 |
Runtime error |
1448 ms |
524288 KB |
Execution killed with signal 9 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
43 ms |
8136 KB |
Output is correct |
3 |
Correct |
47 ms |
8132 KB |
Output is correct |
4 |
Runtime error |
1563 ms |
524288 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
4 ms |
1248 KB |
Output is correct |
3 |
Correct |
5 ms |
1088 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
456 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
21 ms |
7256 KB |
Output is correct |
17 |
Correct |
44 ms |
11744 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
99 ms |
32196 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
77 ms |
30696 KB |
Output is correct |
28 |
Correct |
180 ms |
41288 KB |
Output is correct |
29 |
Correct |
0 ms |
344 KB |
Output is correct |
30 |
Correct |
465 ms |
78524 KB |
Output is correct |
31 |
Correct |
486 ms |
78504 KB |
Output is correct |
32 |
Correct |
503 ms |
78500 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Runtime error |
1814 ms |
524288 KB |
Execution killed with signal 9 |
35 |
Halted |
0 ms |
0 KB |
- |