#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define pb push_back
#define vl vector<ll>
#define fi first
#define se second
#define in insert
#define all(v) v.begin(), v.end()
#define bpc(x) __builtin_popcount(x)
#define endl "\n"
using namespace std;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const int sz = 4e5 + 5;
const int LG = 21;
// using namespace __gnu_pbds;
// tree <ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> rbt;
ll gcd(ll a, ll b){
return __gcd(a, b);
}
ll get(ll a, ll b){
ll c = (a / gcd(a, b + 1)), hi = inf / b;
if(c > hi){
c = inf;
}
else{
c *= b;
}
return c;
}
void solve(){
ll n, a, b, i, l, r;
cin >> n >> a >> b;
if(a == 1 && b == 1){
cout << 1 << endl;
return ;
}
ll x = get(a, b);
vector<array<ll, 2>> v;
bool ok = 1;
for(i = 1; i <= n; i++){
cin >> l >> r;
ok &= ((r - l + 1) >= x);
l %= x;
r %= x;
if(r < l){
v.pb({l, x - 1});
v.pb({0, r});
}
else{
v.pb({l, r});
}
}
if(ok){
cout << x << endl;
return ;
}
sort(all(v));
ll s = 0, hi = -1;
for(i = 0; i < v.size(); i++){
// cout << v[i][0] << ' ' << v[i][1] << endl;
if(v[i][0] > hi){
s += (v[i][1] - v[i][0] + 1);
hi = v[i][1];
}
else if(v[i][1] > hi){
s += (v[i][1] - hi);
hi = v[i][1];
}
}
cout << s << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t = 1;
// cin >> t;
while(t--){
solve();
}
}
/*
3 3 3
4 4
7 9
17 18
*/
Compilation message
strange_device.cpp: In function 'void solve()':
strange_device.cpp:65:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(i = 0; i < v.size(); i++){
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
4 ms |
696 KB |
Output is correct |
3 |
Correct |
5 ms |
836 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
3 ms |
732 KB |
Output is correct |
17 |
Correct |
35 ms |
2520 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
230 ms |
17380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
313 ms |
18736 KB |
Output is correct |
3 |
Correct |
315 ms |
18384 KB |
Output is correct |
4 |
Correct |
301 ms |
17740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
313 ms |
18736 KB |
Output is correct |
3 |
Correct |
315 ms |
18384 KB |
Output is correct |
4 |
Correct |
301 ms |
17740 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
307 ms |
18392 KB |
Output is correct |
7 |
Correct |
314 ms |
18116 KB |
Output is correct |
8 |
Correct |
307 ms |
18876 KB |
Output is correct |
9 |
Correct |
441 ms |
18060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
313 ms |
18736 KB |
Output is correct |
3 |
Correct |
315 ms |
18384 KB |
Output is correct |
4 |
Correct |
301 ms |
17740 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
30 ms |
2476 KB |
Output is correct |
7 |
Correct |
33 ms |
2608 KB |
Output is correct |
8 |
Correct |
32 ms |
2520 KB |
Output is correct |
9 |
Correct |
31 ms |
2520 KB |
Output is correct |
10 |
Correct |
31 ms |
2520 KB |
Output is correct |
11 |
Correct |
32 ms |
2516 KB |
Output is correct |
12 |
Correct |
29 ms |
2520 KB |
Output is correct |
13 |
Correct |
33 ms |
2516 KB |
Output is correct |
14 |
Correct |
30 ms |
2520 KB |
Output is correct |
15 |
Correct |
35 ms |
2520 KB |
Output is correct |
16 |
Correct |
34 ms |
2520 KB |
Output is correct |
17 |
Correct |
32 ms |
2384 KB |
Output is correct |
18 |
Correct |
319 ms |
17116 KB |
Output is correct |
19 |
Correct |
311 ms |
18724 KB |
Output is correct |
20 |
Correct |
364 ms |
17308 KB |
Output is correct |
21 |
Correct |
35 ms |
2516 KB |
Output is correct |
22 |
Correct |
28 ms |
2520 KB |
Output is correct |
23 |
Correct |
94 ms |
10180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
33 ms |
2588 KB |
Output is correct |
3 |
Correct |
34 ms |
2520 KB |
Output is correct |
4 |
Correct |
357 ms |
17332 KB |
Output is correct |
5 |
Correct |
33 ms |
2508 KB |
Output is correct |
6 |
Correct |
34 ms |
2520 KB |
Output is correct |
7 |
Correct |
34 ms |
2520 KB |
Output is correct |
8 |
Correct |
34 ms |
2448 KB |
Output is correct |
9 |
Correct |
35 ms |
2520 KB |
Output is correct |
10 |
Correct |
33 ms |
2520 KB |
Output is correct |
11 |
Correct |
34 ms |
2520 KB |
Output is correct |
12 |
Correct |
30 ms |
2520 KB |
Output is correct |
13 |
Correct |
34 ms |
2520 KB |
Output is correct |
14 |
Correct |
352 ms |
18324 KB |
Output is correct |
15 |
Correct |
33 ms |
2768 KB |
Output is correct |
16 |
Correct |
310 ms |
17076 KB |
Output is correct |
17 |
Correct |
328 ms |
17396 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
4 ms |
696 KB |
Output is correct |
3 |
Correct |
5 ms |
836 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
3 ms |
732 KB |
Output is correct |
17 |
Correct |
35 ms |
2520 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 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 |
230 ms |
17380 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
313 ms |
18736 KB |
Output is correct |
31 |
Correct |
315 ms |
18384 KB |
Output is correct |
32 |
Correct |
301 ms |
17740 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
307 ms |
18392 KB |
Output is correct |
35 |
Correct |
314 ms |
18116 KB |
Output is correct |
36 |
Correct |
307 ms |
18876 KB |
Output is correct |
37 |
Correct |
441 ms |
18060 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
30 ms |
2476 KB |
Output is correct |
40 |
Correct |
33 ms |
2608 KB |
Output is correct |
41 |
Correct |
32 ms |
2520 KB |
Output is correct |
42 |
Correct |
31 ms |
2520 KB |
Output is correct |
43 |
Correct |
31 ms |
2520 KB |
Output is correct |
44 |
Correct |
32 ms |
2516 KB |
Output is correct |
45 |
Correct |
29 ms |
2520 KB |
Output is correct |
46 |
Correct |
33 ms |
2516 KB |
Output is correct |
47 |
Correct |
30 ms |
2520 KB |
Output is correct |
48 |
Correct |
35 ms |
2520 KB |
Output is correct |
49 |
Correct |
34 ms |
2520 KB |
Output is correct |
50 |
Correct |
32 ms |
2384 KB |
Output is correct |
51 |
Correct |
319 ms |
17116 KB |
Output is correct |
52 |
Correct |
311 ms |
18724 KB |
Output is correct |
53 |
Correct |
364 ms |
17308 KB |
Output is correct |
54 |
Correct |
35 ms |
2516 KB |
Output is correct |
55 |
Correct |
28 ms |
2520 KB |
Output is correct |
56 |
Correct |
94 ms |
10180 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
33 ms |
2588 KB |
Output is correct |
59 |
Correct |
34 ms |
2520 KB |
Output is correct |
60 |
Correct |
357 ms |
17332 KB |
Output is correct |
61 |
Correct |
33 ms |
2508 KB |
Output is correct |
62 |
Correct |
34 ms |
2520 KB |
Output is correct |
63 |
Correct |
34 ms |
2520 KB |
Output is correct |
64 |
Correct |
34 ms |
2448 KB |
Output is correct |
65 |
Correct |
35 ms |
2520 KB |
Output is correct |
66 |
Correct |
33 ms |
2520 KB |
Output is correct |
67 |
Correct |
34 ms |
2520 KB |
Output is correct |
68 |
Correct |
30 ms |
2520 KB |
Output is correct |
69 |
Correct |
34 ms |
2520 KB |
Output is correct |
70 |
Correct |
352 ms |
18324 KB |
Output is correct |
71 |
Correct |
33 ms |
2768 KB |
Output is correct |
72 |
Correct |
310 ms |
17076 KB |
Output is correct |
73 |
Correct |
328 ms |
17396 KB |
Output is correct |
74 |
Correct |
1 ms |
348 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
344 KB |
Output is correct |
77 |
Correct |
0 ms |
348 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
3 ms |
736 KB |
Output is correct |
80 |
Correct |
355 ms |
18360 KB |
Output is correct |
81 |
Correct |
363 ms |
18616 KB |
Output is correct |
82 |
Correct |
343 ms |
17224 KB |
Output is correct |
83 |
Correct |
333 ms |
18888 KB |
Output is correct |
84 |
Correct |
336 ms |
17292 KB |
Output is correct |
85 |
Correct |
341 ms |
18944 KB |
Output is correct |
86 |
Correct |
94 ms |
8788 KB |
Output is correct |
87 |
Correct |
0 ms |
344 KB |
Output is correct |