#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
using ll = long long;
using ld = long double;
ll const inf = 1000000000000000000;
ll period(ll a, ll b){
ll p = __gcd(a, b + 1);
if(a / p <= inf / b)
return a / p * b;
return inf + 1;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
ll a, b;
cin >> n >> a >> b;
ll pi = period(a, b);
if(inf < pi){
ll result = 0;
for(int i = 1;i <= n; i++){
ll x, y;
cin >> x >> y;
result += y - x + 1;
}
cout << result;
} else {
vector<pair<ll,int>> event;
for(int i = 1;i <= n; i++){
ll x, y;
cin >> x >> y;
if(y - x < pi){
if(x % pi <= y % pi){
event.push_back({x % pi, 1});
event.push_back({y % pi + 1, -1});
} else {
event.push_back({x % pi, 1});
event.push_back({pi, -1});
event.push_back({0, 1});
event.push_back({y % pi + 1, -1});
}
} else {
cout << pi;
return 0;
}
}
sort(event.begin(), event.end());
ll result = 0, last = 0, sum = 0;
for(int i = 0; i < event.size(); i++){
if(0 < sum)
result += event[i].first - last;
last = event[i].first;
sum += event[i].second;
}
cout << result;
}
return 0;
}
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:62:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < event.size(); i++){
~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
11 ms |
1016 KB |
Output is correct |
3 |
Correct |
11 ms |
1016 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
380 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
12 ms |
1400 KB |
Output is correct |
17 |
Correct |
83 ms |
7392 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
6 ms |
380 KB |
Output is correct |
5 |
Correct |
458 ms |
33320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
684 ms |
33344 KB |
Output is correct |
3 |
Correct |
702 ms |
47292 KB |
Output is correct |
4 |
Correct |
641 ms |
37024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
684 ms |
33344 KB |
Output is correct |
3 |
Correct |
702 ms |
47292 KB |
Output is correct |
4 |
Correct |
641 ms |
37024 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
683 ms |
37036 KB |
Output is correct |
7 |
Correct |
662 ms |
37552 KB |
Output is correct |
8 |
Correct |
685 ms |
36956 KB |
Output is correct |
9 |
Correct |
766 ms |
37532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
684 ms |
33344 KB |
Output is correct |
3 |
Correct |
702 ms |
47292 KB |
Output is correct |
4 |
Correct |
641 ms |
37024 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
71 ms |
7272 KB |
Output is correct |
7 |
Correct |
78 ms |
7368 KB |
Output is correct |
8 |
Correct |
68 ms |
7392 KB |
Output is correct |
9 |
Correct |
68 ms |
7272 KB |
Output is correct |
10 |
Correct |
69 ms |
7392 KB |
Output is correct |
11 |
Correct |
70 ms |
7272 KB |
Output is correct |
12 |
Correct |
68 ms |
7272 KB |
Output is correct |
13 |
Correct |
77 ms |
7392 KB |
Output is correct |
14 |
Correct |
71 ms |
7272 KB |
Output is correct |
15 |
Correct |
85 ms |
7416 KB |
Output is correct |
16 |
Correct |
80 ms |
7392 KB |
Output is correct |
17 |
Correct |
76 ms |
7272 KB |
Output is correct |
18 |
Correct |
721 ms |
37152 KB |
Output is correct |
19 |
Correct |
661 ms |
37404 KB |
Output is correct |
20 |
Correct |
801 ms |
37020 KB |
Output is correct |
21 |
Correct |
79 ms |
7272 KB |
Output is correct |
22 |
Correct |
67 ms |
7340 KB |
Output is correct |
23 |
Correct |
198 ms |
26708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
76 ms |
4712 KB |
Output is correct |
3 |
Correct |
77 ms |
4708 KB |
Output is correct |
4 |
Correct |
829 ms |
33472 KB |
Output is correct |
5 |
Correct |
73 ms |
4840 KB |
Output is correct |
6 |
Correct |
75 ms |
4840 KB |
Output is correct |
7 |
Correct |
75 ms |
4840 KB |
Output is correct |
8 |
Correct |
75 ms |
4840 KB |
Output is correct |
9 |
Correct |
79 ms |
4892 KB |
Output is correct |
10 |
Correct |
80 ms |
4840 KB |
Output is correct |
11 |
Correct |
77 ms |
4844 KB |
Output is correct |
12 |
Correct |
66 ms |
4840 KB |
Output is correct |
13 |
Correct |
77 ms |
4840 KB |
Output is correct |
14 |
Correct |
787 ms |
33668 KB |
Output is correct |
15 |
Correct |
78 ms |
4836 KB |
Output is correct |
16 |
Correct |
643 ms |
37020 KB |
Output is correct |
17 |
Correct |
668 ms |
36944 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
11 ms |
1016 KB |
Output is correct |
3 |
Correct |
11 ms |
1016 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
380 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
12 ms |
1400 KB |
Output is correct |
17 |
Correct |
83 ms |
7392 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
6 ms |
376 KB |
Output is correct |
26 |
Correct |
5 ms |
376 KB |
Output is correct |
27 |
Correct |
6 ms |
380 KB |
Output is correct |
28 |
Correct |
458 ms |
33320 KB |
Output is correct |
29 |
Correct |
5 ms |
256 KB |
Output is correct |
30 |
Correct |
684 ms |
33344 KB |
Output is correct |
31 |
Correct |
702 ms |
47292 KB |
Output is correct |
32 |
Correct |
641 ms |
37024 KB |
Output is correct |
33 |
Correct |
5 ms |
376 KB |
Output is correct |
34 |
Correct |
683 ms |
37036 KB |
Output is correct |
35 |
Correct |
662 ms |
37552 KB |
Output is correct |
36 |
Correct |
685 ms |
36956 KB |
Output is correct |
37 |
Correct |
766 ms |
37532 KB |
Output is correct |
38 |
Correct |
5 ms |
376 KB |
Output is correct |
39 |
Correct |
71 ms |
7272 KB |
Output is correct |
40 |
Correct |
78 ms |
7368 KB |
Output is correct |
41 |
Correct |
68 ms |
7392 KB |
Output is correct |
42 |
Correct |
68 ms |
7272 KB |
Output is correct |
43 |
Correct |
69 ms |
7392 KB |
Output is correct |
44 |
Correct |
70 ms |
7272 KB |
Output is correct |
45 |
Correct |
68 ms |
7272 KB |
Output is correct |
46 |
Correct |
77 ms |
7392 KB |
Output is correct |
47 |
Correct |
71 ms |
7272 KB |
Output is correct |
48 |
Correct |
85 ms |
7416 KB |
Output is correct |
49 |
Correct |
80 ms |
7392 KB |
Output is correct |
50 |
Correct |
76 ms |
7272 KB |
Output is correct |
51 |
Correct |
721 ms |
37152 KB |
Output is correct |
52 |
Correct |
661 ms |
37404 KB |
Output is correct |
53 |
Correct |
801 ms |
37020 KB |
Output is correct |
54 |
Correct |
79 ms |
7272 KB |
Output is correct |
55 |
Correct |
67 ms |
7340 KB |
Output is correct |
56 |
Correct |
198 ms |
26708 KB |
Output is correct |
57 |
Correct |
5 ms |
376 KB |
Output is correct |
58 |
Correct |
76 ms |
4712 KB |
Output is correct |
59 |
Correct |
77 ms |
4708 KB |
Output is correct |
60 |
Correct |
829 ms |
33472 KB |
Output is correct |
61 |
Correct |
73 ms |
4840 KB |
Output is correct |
62 |
Correct |
75 ms |
4840 KB |
Output is correct |
63 |
Correct |
75 ms |
4840 KB |
Output is correct |
64 |
Correct |
75 ms |
4840 KB |
Output is correct |
65 |
Correct |
79 ms |
4892 KB |
Output is correct |
66 |
Correct |
80 ms |
4840 KB |
Output is correct |
67 |
Correct |
77 ms |
4844 KB |
Output is correct |
68 |
Correct |
66 ms |
4840 KB |
Output is correct |
69 |
Correct |
77 ms |
4840 KB |
Output is correct |
70 |
Correct |
787 ms |
33668 KB |
Output is correct |
71 |
Correct |
78 ms |
4836 KB |
Output is correct |
72 |
Correct |
643 ms |
37020 KB |
Output is correct |
73 |
Correct |
668 ms |
36944 KB |
Output is correct |
74 |
Correct |
5 ms |
376 KB |
Output is correct |
75 |
Correct |
5 ms |
376 KB |
Output is correct |
76 |
Correct |
5 ms |
376 KB |
Output is correct |
77 |
Correct |
5 ms |
376 KB |
Output is correct |
78 |
Correct |
5 ms |
380 KB |
Output is correct |
79 |
Correct |
11 ms |
1400 KB |
Output is correct |
80 |
Correct |
825 ms |
36888 KB |
Output is correct |
81 |
Correct |
816 ms |
37108 KB |
Output is correct |
82 |
Correct |
776 ms |
37064 KB |
Output is correct |
83 |
Correct |
761 ms |
36912 KB |
Output is correct |
84 |
Correct |
762 ms |
37112 KB |
Output is correct |
85 |
Correct |
778 ms |
37688 KB |
Output is correct |
86 |
Correct |
192 ms |
26708 KB |
Output is correct |
87 |
Correct |
5 ms |
376 KB |
Output is correct |