//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,abm,popcnt,mmx")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef complex<double> cd;
constexpr ll INF64 = 1'000'000'000'000'000'000, INF32 = 2'000'000'000, MOD = 1'000'000'007;
constexpr db PI = acos(-1);
constexpr bool IS_FILE = false, IS_TEST_CASES = false;
random_device rd;
mt19937 rnd32(rd());
mt19937_64 rnd64(rd());
template<typename T>
bool assign_max(T& a, T b) {
if (b > a) {
a = b;
return true;
}
return false;
}
template<typename T>
bool assign_min(T& a, T b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<typename T>
T square(T a) {
return a * a;
}
template<>
struct std::hash<pair<ll, ll>> {
ll operator() (pair<ll, ll> p) const {
return ((__int128)p.first * MOD + p.second) % INF64;
}
};
ll gcd(ll a, ll b) {
return (min(a, b) == 0 ? max(a, b) : gcd(min(a, b), max(a, b) % min(a, b)));
}
void solve() {
ll n, a, b;
cin >> n >> a >> b;
ll x = gcd(a, b + 1);
ll mod = min((__int128)INF64, (__int128)a * b / x);
map<ll, ll> add;
for (ll i = 0; i < n; i++) {
ll l, r;
cin >> l >> r;
r++;
if (r - l >= mod) {
cout << mod << '\n';
return;
}
l %= mod;
r %= mod;
if (l <= r) {
add[l]++;
add[r]--;
} else {
add[0]++;
add[mod]--;
add[l]++;
add[r]--;
}
}
ll now = 0;
ll ans = 0;
ll lst = 0;
for (auto[a, b] : add) {
if (now) {
ans += a - lst;
}
lst = a;
now += b;
}
cout << ans << '\n';
}
int main() {
if (IS_FILE) {
freopen("", "r", stdin);
freopen("", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t = 1;
if (IS_TEST_CASES) {
cin >> t;
}
for (ll i = 0; i < t; i++) {
solve();
}
}
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:95:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | freopen("", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~
strange_device.cpp:96:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | freopen("", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
1116 KB |
Output is correct |
3 |
Correct |
5 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 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 |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
1116 KB |
Output is correct |
17 |
Correct |
52 ms |
6620 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
174 ms |
456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
561 ms |
62892 KB |
Output is correct |
3 |
Correct |
552 ms |
63020 KB |
Output is correct |
4 |
Correct |
583 ms |
63076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
561 ms |
62892 KB |
Output is correct |
3 |
Correct |
552 ms |
63020 KB |
Output is correct |
4 |
Correct |
583 ms |
63076 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
555 ms |
63000 KB |
Output is correct |
7 |
Correct |
560 ms |
63192 KB |
Output is correct |
8 |
Correct |
531 ms |
63044 KB |
Output is correct |
9 |
Correct |
639 ms |
63024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
561 ms |
62892 KB |
Output is correct |
3 |
Correct |
552 ms |
63020 KB |
Output is correct |
4 |
Correct |
583 ms |
63076 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
51 ms |
6484 KB |
Output is correct |
7 |
Correct |
63 ms |
6588 KB |
Output is correct |
8 |
Correct |
59 ms |
6720 KB |
Output is correct |
9 |
Correct |
52 ms |
6488 KB |
Output is correct |
10 |
Correct |
53 ms |
6736 KB |
Output is correct |
11 |
Correct |
45 ms |
6484 KB |
Output is correct |
12 |
Correct |
54 ms |
6484 KB |
Output is correct |
13 |
Correct |
69 ms |
6716 KB |
Output is correct |
14 |
Correct |
42 ms |
6744 KB |
Output is correct |
15 |
Correct |
55 ms |
6704 KB |
Output is correct |
16 |
Correct |
60 ms |
6484 KB |
Output is correct |
17 |
Correct |
52 ms |
6488 KB |
Output is correct |
18 |
Correct |
537 ms |
63080 KB |
Output is correct |
19 |
Correct |
575 ms |
63108 KB |
Output is correct |
20 |
Correct |
590 ms |
63072 KB |
Output is correct |
21 |
Correct |
45 ms |
6484 KB |
Output is correct |
22 |
Correct |
45 ms |
6472 KB |
Output is correct |
23 |
Correct |
74 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
51 ms |
6484 KB |
Output is correct |
3 |
Correct |
59 ms |
6740 KB |
Output is correct |
4 |
Correct |
566 ms |
63060 KB |
Output is correct |
5 |
Correct |
63 ms |
6484 KB |
Output is correct |
6 |
Correct |
52 ms |
6484 KB |
Output is correct |
7 |
Correct |
51 ms |
6708 KB |
Output is correct |
8 |
Correct |
51 ms |
6492 KB |
Output is correct |
9 |
Correct |
48 ms |
6484 KB |
Output is correct |
10 |
Correct |
47 ms |
6644 KB |
Output is correct |
11 |
Correct |
48 ms |
6480 KB |
Output is correct |
12 |
Correct |
48 ms |
6484 KB |
Output is correct |
13 |
Correct |
51 ms |
6636 KB |
Output is correct |
14 |
Correct |
623 ms |
63060 KB |
Output is correct |
15 |
Correct |
43 ms |
6500 KB |
Output is correct |
16 |
Correct |
516 ms |
63060 KB |
Output is correct |
17 |
Correct |
537 ms |
63088 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
1116 KB |
Output is correct |
3 |
Correct |
5 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 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 |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
1116 KB |
Output is correct |
17 |
Correct |
52 ms |
6620 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
174 ms |
456 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
561 ms |
62892 KB |
Output is correct |
31 |
Correct |
552 ms |
63020 KB |
Output is correct |
32 |
Correct |
583 ms |
63076 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
555 ms |
63000 KB |
Output is correct |
35 |
Correct |
560 ms |
63192 KB |
Output is correct |
36 |
Correct |
531 ms |
63044 KB |
Output is correct |
37 |
Correct |
639 ms |
63024 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
51 ms |
6484 KB |
Output is correct |
40 |
Correct |
63 ms |
6588 KB |
Output is correct |
41 |
Correct |
59 ms |
6720 KB |
Output is correct |
42 |
Correct |
52 ms |
6488 KB |
Output is correct |
43 |
Correct |
53 ms |
6736 KB |
Output is correct |
44 |
Correct |
45 ms |
6484 KB |
Output is correct |
45 |
Correct |
54 ms |
6484 KB |
Output is correct |
46 |
Correct |
69 ms |
6716 KB |
Output is correct |
47 |
Correct |
42 ms |
6744 KB |
Output is correct |
48 |
Correct |
55 ms |
6704 KB |
Output is correct |
49 |
Correct |
60 ms |
6484 KB |
Output is correct |
50 |
Correct |
52 ms |
6488 KB |
Output is correct |
51 |
Correct |
537 ms |
63080 KB |
Output is correct |
52 |
Correct |
575 ms |
63108 KB |
Output is correct |
53 |
Correct |
590 ms |
63072 KB |
Output is correct |
54 |
Correct |
45 ms |
6484 KB |
Output is correct |
55 |
Correct |
45 ms |
6472 KB |
Output is correct |
56 |
Correct |
74 ms |
344 KB |
Output is correct |
57 |
Correct |
1 ms |
344 KB |
Output is correct |
58 |
Correct |
51 ms |
6484 KB |
Output is correct |
59 |
Correct |
59 ms |
6740 KB |
Output is correct |
60 |
Correct |
566 ms |
63060 KB |
Output is correct |
61 |
Correct |
63 ms |
6484 KB |
Output is correct |
62 |
Correct |
52 ms |
6484 KB |
Output is correct |
63 |
Correct |
51 ms |
6708 KB |
Output is correct |
64 |
Correct |
51 ms |
6492 KB |
Output is correct |
65 |
Correct |
48 ms |
6484 KB |
Output is correct |
66 |
Correct |
47 ms |
6644 KB |
Output is correct |
67 |
Correct |
48 ms |
6480 KB |
Output is correct |
68 |
Correct |
48 ms |
6484 KB |
Output is correct |
69 |
Correct |
51 ms |
6636 KB |
Output is correct |
70 |
Correct |
623 ms |
63060 KB |
Output is correct |
71 |
Correct |
43 ms |
6500 KB |
Output is correct |
72 |
Correct |
516 ms |
63060 KB |
Output is correct |
73 |
Correct |
537 ms |
63088 KB |
Output is correct |
74 |
Correct |
1 ms |
348 KB |
Output is correct |
75 |
Correct |
1 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
1 ms |
344 KB |
Output is correct |
78 |
Correct |
1 ms |
348 KB |
Output is correct |
79 |
Correct |
5 ms |
1372 KB |
Output is correct |
80 |
Correct |
597 ms |
100232 KB |
Output is correct |
81 |
Correct |
643 ms |
100424 KB |
Output is correct |
82 |
Correct |
711 ms |
100252 KB |
Output is correct |
83 |
Correct |
745 ms |
100032 KB |
Output is correct |
84 |
Correct |
757 ms |
100180 KB |
Output is correct |
85 |
Correct |
659 ms |
100252 KB |
Output is correct |
86 |
Correct |
81 ms |
12984 KB |
Output is correct |
87 |
Correct |
1 ms |
348 KB |
Output is correct |