/* 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 |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
736 KB |
Output is correct |
3 |
Correct |
3 ms |
708 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 |
344 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 |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 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 |
3 ms |
736 KB |
Output is correct |
17 |
Correct |
25 ms |
2520 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
488 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
155 ms |
17984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
245 ms |
20140 KB |
Output is correct |
3 |
Correct |
255 ms |
30820 KB |
Output is correct |
4 |
Correct |
309 ms |
31144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
245 ms |
20140 KB |
Output is correct |
3 |
Correct |
255 ms |
30820 KB |
Output is correct |
4 |
Correct |
309 ms |
31144 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
272 ms |
31440 KB |
Output is correct |
7 |
Correct |
218 ms |
31216 KB |
Output is correct |
8 |
Correct |
253 ms |
30768 KB |
Output is correct |
9 |
Correct |
294 ms |
30944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
245 ms |
20140 KB |
Output is correct |
3 |
Correct |
255 ms |
30820 KB |
Output is correct |
4 |
Correct |
309 ms |
31144 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
23 ms |
4012 KB |
Output is correct |
7 |
Correct |
30 ms |
4056 KB |
Output is correct |
8 |
Correct |
24 ms |
4172 KB |
Output is correct |
9 |
Correct |
23 ms |
4312 KB |
Output is correct |
10 |
Correct |
27 ms |
4056 KB |
Output is correct |
11 |
Correct |
23 ms |
4280 KB |
Output is correct |
12 |
Correct |
24 ms |
4056 KB |
Output is correct |
13 |
Correct |
28 ms |
4052 KB |
Output is correct |
14 |
Correct |
21 ms |
4040 KB |
Output is correct |
15 |
Correct |
25 ms |
4120 KB |
Output is correct |
16 |
Correct |
43 ms |
4056 KB |
Output is correct |
17 |
Correct |
22 ms |
4056 KB |
Output is correct |
18 |
Correct |
311 ms |
30700 KB |
Output is correct |
19 |
Correct |
268 ms |
30708 KB |
Output is correct |
20 |
Correct |
236 ms |
30744 KB |
Output is correct |
21 |
Correct |
27 ms |
4048 KB |
Output is correct |
22 |
Correct |
37 ms |
4048 KB |
Output is correct |
23 |
Correct |
75 ms |
14340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
23 ms |
2872 KB |
Output is correct |
3 |
Correct |
41 ms |
2528 KB |
Output is correct |
4 |
Correct |
299 ms |
30964 KB |
Output is correct |
5 |
Correct |
24 ms |
4048 KB |
Output is correct |
6 |
Correct |
38 ms |
4044 KB |
Output is correct |
7 |
Correct |
27 ms |
4052 KB |
Output is correct |
8 |
Correct |
26 ms |
4308 KB |
Output is correct |
9 |
Correct |
25 ms |
4056 KB |
Output is correct |
10 |
Correct |
38 ms |
4000 KB |
Output is correct |
11 |
Correct |
24 ms |
4004 KB |
Output is correct |
12 |
Correct |
21 ms |
3976 KB |
Output is correct |
13 |
Correct |
30 ms |
4128 KB |
Output is correct |
14 |
Correct |
258 ms |
31736 KB |
Output is correct |
15 |
Correct |
38 ms |
4184 KB |
Output is correct |
16 |
Correct |
273 ms |
30612 KB |
Output is correct |
17 |
Correct |
236 ms |
31676 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
736 KB |
Output is correct |
3 |
Correct |
3 ms |
708 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 |
344 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 |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 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 |
3 ms |
736 KB |
Output is correct |
17 |
Correct |
25 ms |
2520 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
488 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
155 ms |
17984 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
245 ms |
20140 KB |
Output is correct |
31 |
Correct |
255 ms |
30820 KB |
Output is correct |
32 |
Correct |
309 ms |
31144 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
272 ms |
31440 KB |
Output is correct |
35 |
Correct |
218 ms |
31216 KB |
Output is correct |
36 |
Correct |
253 ms |
30768 KB |
Output is correct |
37 |
Correct |
294 ms |
30944 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
23 ms |
4012 KB |
Output is correct |
40 |
Correct |
30 ms |
4056 KB |
Output is correct |
41 |
Correct |
24 ms |
4172 KB |
Output is correct |
42 |
Correct |
23 ms |
4312 KB |
Output is correct |
43 |
Correct |
27 ms |
4056 KB |
Output is correct |
44 |
Correct |
23 ms |
4280 KB |
Output is correct |
45 |
Correct |
24 ms |
4056 KB |
Output is correct |
46 |
Correct |
28 ms |
4052 KB |
Output is correct |
47 |
Correct |
21 ms |
4040 KB |
Output is correct |
48 |
Correct |
25 ms |
4120 KB |
Output is correct |
49 |
Correct |
43 ms |
4056 KB |
Output is correct |
50 |
Correct |
22 ms |
4056 KB |
Output is correct |
51 |
Correct |
311 ms |
30700 KB |
Output is correct |
52 |
Correct |
268 ms |
30708 KB |
Output is correct |
53 |
Correct |
236 ms |
30744 KB |
Output is correct |
54 |
Correct |
27 ms |
4048 KB |
Output is correct |
55 |
Correct |
37 ms |
4048 KB |
Output is correct |
56 |
Correct |
75 ms |
14340 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
23 ms |
2872 KB |
Output is correct |
59 |
Correct |
41 ms |
2528 KB |
Output is correct |
60 |
Correct |
299 ms |
30964 KB |
Output is correct |
61 |
Correct |
24 ms |
4048 KB |
Output is correct |
62 |
Correct |
38 ms |
4044 KB |
Output is correct |
63 |
Correct |
27 ms |
4052 KB |
Output is correct |
64 |
Correct |
26 ms |
4308 KB |
Output is correct |
65 |
Correct |
25 ms |
4056 KB |
Output is correct |
66 |
Correct |
38 ms |
4000 KB |
Output is correct |
67 |
Correct |
24 ms |
4004 KB |
Output is correct |
68 |
Correct |
21 ms |
3976 KB |
Output is correct |
69 |
Correct |
30 ms |
4128 KB |
Output is correct |
70 |
Correct |
258 ms |
31736 KB |
Output is correct |
71 |
Correct |
38 ms |
4184 KB |
Output is correct |
72 |
Correct |
273 ms |
30612 KB |
Output is correct |
73 |
Correct |
236 ms |
31676 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
0 ms |
348 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
2 ms |
1196 KB |
Output is correct |
80 |
Correct |
270 ms |
30936 KB |
Output is correct |
81 |
Correct |
330 ms |
31928 KB |
Output is correct |
82 |
Correct |
267 ms |
30900 KB |
Output is correct |
83 |
Correct |
268 ms |
30892 KB |
Output is correct |
84 |
Correct |
318 ms |
31412 KB |
Output is correct |
85 |
Correct |
292 ms |
31928 KB |
Output is correct |
86 |
Correct |
75 ms |
15036 KB |
Output is correct |
87 |
Correct |
1 ms |
344 KB |
Output is correct |