/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
#define ff first
#define ss second
const int N = 1e6+100, M = 1e5+10, K = 52, MX = 30;
ll n, a, b;
vector<pair<ll, ll>> v;
void solve(){
cin >> n >> a >> b;
ll g = gcd(b+1, a);
ll val = a / g;
if(log2(val) + log2(b) > 60){
ll tot = 0;
for(int i = 1; i <= n; ++i){
ll t1, t2; cin >> t1 >> t2;
tot += t2 - t1 + 1;
}
cout << tot;
return;
}
ll rep = b*val;
ll ans = 0;
for(int i = 1; i <= n; ++i){
ll t1, t2; cin >> t1 >> t2;
if(t1 + rep <= t2){
ans = rep;
}else{
if(t2 % rep < t1 % rep){
v.pb({0, t2 % rep});
v.pb({t1 % rep, rep - 1});
}else{
v.pb({t1%rep, t2%rep});
}
}
}
if(ans == rep) cout << ans;
else{
sort(all(v));
ll ans = rep;
if(v.size() && v[0].ff > 0) ans -= v[0].ff;
if(v.size() && v.back().ss < rep - 1) ans -= rep - v.back().ss - 1;
ll mx = v[0].ss;
for(int i = 1; i < v.size(); ++i){
if(v[i].ff > mx) ans -= v[i].ff - mx - 1;
mx = max(mx, v[i].ss);
}
// for(auto p: v) cout << p.ff << ' ' << p.ss << '\n';
cout << ans;
}
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message
strange_device.cpp: In function 'void solve()':
strange_device.cpp:52:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int i = 1; i < v.size(); ++i){
| ~~^~~~~~~~~~
strange_device.cpp: In function 'int main()':
strange_device.cpp:64:15: warning: unused variable 'aa' [-Wunused-variable]
64 | int tt = 1, aa;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
7 ms |
1104 KB |
Output is correct |
3 |
Correct |
4 ms |
1104 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
4 ms |
1104 KB |
Output is correct |
17 |
Correct |
33 ms |
6304 KB |
Output is correct |
18 |
Correct |
1 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
199 ms |
42152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
336 ms |
54068 KB |
Output is correct |
3 |
Correct |
359 ms |
54184 KB |
Output is correct |
4 |
Correct |
290 ms |
54096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
336 ms |
54068 KB |
Output is correct |
3 |
Correct |
359 ms |
54184 KB |
Output is correct |
4 |
Correct |
290 ms |
54096 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
319 ms |
54028 KB |
Output is correct |
7 |
Correct |
292 ms |
54184 KB |
Output is correct |
8 |
Correct |
262 ms |
54184 KB |
Output is correct |
9 |
Correct |
299 ms |
53952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
336 ms |
54068 KB |
Output is correct |
3 |
Correct |
359 ms |
54184 KB |
Output is correct |
4 |
Correct |
290 ms |
54096 KB |
Output is correct |
5 |
Correct |
1 ms |
504 KB |
Output is correct |
6 |
Correct |
28 ms |
6348 KB |
Output is correct |
7 |
Correct |
29 ms |
6360 KB |
Output is correct |
8 |
Correct |
40 ms |
6264 KB |
Output is correct |
9 |
Correct |
31 ms |
6348 KB |
Output is correct |
10 |
Correct |
27 ms |
6348 KB |
Output is correct |
11 |
Correct |
29 ms |
6340 KB |
Output is correct |
12 |
Correct |
27 ms |
6340 KB |
Output is correct |
13 |
Correct |
30 ms |
6340 KB |
Output is correct |
14 |
Correct |
27 ms |
6348 KB |
Output is correct |
15 |
Correct |
30 ms |
6348 KB |
Output is correct |
16 |
Correct |
30 ms |
6348 KB |
Output is correct |
17 |
Correct |
29 ms |
6348 KB |
Output is correct |
18 |
Correct |
266 ms |
54200 KB |
Output is correct |
19 |
Correct |
320 ms |
54244 KB |
Output is correct |
20 |
Correct |
309 ms |
54140 KB |
Output is correct |
21 |
Correct |
32 ms |
6348 KB |
Output is correct |
22 |
Correct |
30 ms |
6348 KB |
Output is correct |
23 |
Correct |
89 ms |
20668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
44 ms |
6596 KB |
Output is correct |
3 |
Correct |
34 ms |
6292 KB |
Output is correct |
4 |
Correct |
319 ms |
54208 KB |
Output is correct |
5 |
Correct |
42 ms |
6348 KB |
Output is correct |
6 |
Correct |
31 ms |
6288 KB |
Output is correct |
7 |
Correct |
32 ms |
6348 KB |
Output is correct |
8 |
Correct |
32 ms |
6348 KB |
Output is correct |
9 |
Correct |
30 ms |
6352 KB |
Output is correct |
10 |
Correct |
31 ms |
6148 KB |
Output is correct |
11 |
Correct |
32 ms |
6352 KB |
Output is correct |
12 |
Correct |
29 ms |
6348 KB |
Output is correct |
13 |
Correct |
31 ms |
6348 KB |
Output is correct |
14 |
Correct |
317 ms |
54080 KB |
Output is correct |
15 |
Correct |
32 ms |
6348 KB |
Output is correct |
16 |
Correct |
288 ms |
54196 KB |
Output is correct |
17 |
Correct |
274 ms |
54184 KB |
Output is correct |
18 |
Correct |
1 ms |
508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
7 ms |
1104 KB |
Output is correct |
3 |
Correct |
4 ms |
1104 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
4 ms |
1104 KB |
Output is correct |
17 |
Correct |
33 ms |
6304 KB |
Output is correct |
18 |
Correct |
1 ms |
504 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
592 KB |
Output is correct |
28 |
Correct |
199 ms |
42152 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
336 ms |
54068 KB |
Output is correct |
31 |
Correct |
359 ms |
54184 KB |
Output is correct |
32 |
Correct |
290 ms |
54096 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
319 ms |
54028 KB |
Output is correct |
35 |
Correct |
292 ms |
54184 KB |
Output is correct |
36 |
Correct |
262 ms |
54184 KB |
Output is correct |
37 |
Correct |
299 ms |
53952 KB |
Output is correct |
38 |
Correct |
1 ms |
504 KB |
Output is correct |
39 |
Correct |
28 ms |
6348 KB |
Output is correct |
40 |
Correct |
29 ms |
6360 KB |
Output is correct |
41 |
Correct |
40 ms |
6264 KB |
Output is correct |
42 |
Correct |
31 ms |
6348 KB |
Output is correct |
43 |
Correct |
27 ms |
6348 KB |
Output is correct |
44 |
Correct |
29 ms |
6340 KB |
Output is correct |
45 |
Correct |
27 ms |
6340 KB |
Output is correct |
46 |
Correct |
30 ms |
6340 KB |
Output is correct |
47 |
Correct |
27 ms |
6348 KB |
Output is correct |
48 |
Correct |
30 ms |
6348 KB |
Output is correct |
49 |
Correct |
30 ms |
6348 KB |
Output is correct |
50 |
Correct |
29 ms |
6348 KB |
Output is correct |
51 |
Correct |
266 ms |
54200 KB |
Output is correct |
52 |
Correct |
320 ms |
54244 KB |
Output is correct |
53 |
Correct |
309 ms |
54140 KB |
Output is correct |
54 |
Correct |
32 ms |
6348 KB |
Output is correct |
55 |
Correct |
30 ms |
6348 KB |
Output is correct |
56 |
Correct |
89 ms |
20668 KB |
Output is correct |
57 |
Correct |
1 ms |
336 KB |
Output is correct |
58 |
Correct |
44 ms |
6596 KB |
Output is correct |
59 |
Correct |
34 ms |
6292 KB |
Output is correct |
60 |
Correct |
319 ms |
54208 KB |
Output is correct |
61 |
Correct |
42 ms |
6348 KB |
Output is correct |
62 |
Correct |
31 ms |
6288 KB |
Output is correct |
63 |
Correct |
32 ms |
6348 KB |
Output is correct |
64 |
Correct |
32 ms |
6348 KB |
Output is correct |
65 |
Correct |
30 ms |
6352 KB |
Output is correct |
66 |
Correct |
31 ms |
6148 KB |
Output is correct |
67 |
Correct |
32 ms |
6352 KB |
Output is correct |
68 |
Correct |
29 ms |
6348 KB |
Output is correct |
69 |
Correct |
31 ms |
6348 KB |
Output is correct |
70 |
Correct |
317 ms |
54080 KB |
Output is correct |
71 |
Correct |
32 ms |
6348 KB |
Output is correct |
72 |
Correct |
288 ms |
54196 KB |
Output is correct |
73 |
Correct |
274 ms |
54184 KB |
Output is correct |
74 |
Correct |
1 ms |
508 KB |
Output is correct |
75 |
Correct |
1 ms |
336 KB |
Output is correct |
76 |
Correct |
1 ms |
336 KB |
Output is correct |
77 |
Correct |
1 ms |
336 KB |
Output is correct |
78 |
Correct |
1 ms |
336 KB |
Output is correct |
79 |
Correct |
3 ms |
1192 KB |
Output is correct |
80 |
Correct |
349 ms |
54000 KB |
Output is correct |
81 |
Correct |
303 ms |
54184 KB |
Output is correct |
82 |
Correct |
315 ms |
54076 KB |
Output is correct |
83 |
Correct |
334 ms |
54184 KB |
Output is correct |
84 |
Correct |
344 ms |
54192 KB |
Output is correct |
85 |
Correct |
310 ms |
54152 KB |
Output is correct |
86 |
Correct |
88 ms |
20804 KB |
Output is correct |
87 |
Correct |
1 ms |
336 KB |
Output is correct |