#pragma GCC optimize("trapv")
#include<bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using vi = vector<ll>;
using pi = pair<ll, ll>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll n, a, b, s = 0, c;
vector<pi> seg;
ll solve1() {
set<pair<int, int>> have;
for(auto &i : seg) {
for(int j = i.first; j <= i.second; j++) {
have.insert({j%b, (j + j/b)%a});
}
}
int ans = 0;
for(auto &i : have)
ans++;
return ans;
}
ll solve2() {
return min(s, c);
}
ll solve() {
vector<pair<ll, ll>> a;
for(auto i : seg) {
if(i.second-i.first+1>=c) return c;
ll x = i.first%c, y = i.second%c;
// cout << x << " " << y << "\n";
if(x<=y) {
a.pb({x, 1});
a.pb({y+1, 0});
// cout << x << " " << y+1 << ")\n";
} else {
a.pb({0, 1});
a.pb({y+1, 0});
a.pb({x, 1});
a.pb({c, 0});
}
}
sort(all(a));
ll ans = 0, op = 0;
for(int i = 0; i +1 < a.size(); i++) {
op += (a[i].second?1:-1);
if(op) ans += a[i+1].first-a[i].first;
// cout << a[i].first << " " << a[i].second << "\n";// << op << " " << ans << "\n";
}
return ans;
}
void tst() {
for(int i = 0; i < 100; i++) {
int l = rng()%100000, r = l + (rng()%100000);
n = 1, a = rng()%100000, b = rng()%100000;
seg = {{l, r}};
s = r-l+1;
assert(solve1()==solve());
// cout << solve1() << " " << solve2() << "\n";
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> a >> b;
seg.resize(n);
for(auto &i : seg) {
cin >> i.first >> i.second;
s += i.second-i.first+1;
}
if((a/__gcd(a, b)) > 2000000000000000000/b) return cout << s, 0;
c = b*(a/__gcd(a, b+1));
return cout << solve(), 0;
if(s<=1000000) return cout << solve1(), 0;
if(n==1) return cout << solve2(), 0;
}
Compilation message
strange_device.cpp: In function 'll solve1()':
strange_device.cpp:20:12: warning: unused variable 'i' [-Wunused-variable]
for(auto &i : have)
^
strange_device.cpp: In function 'll solve()':
strange_device.cpp:46:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i +1 < a.size(); i++) {
~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
10 ms |
1528 KB |
Output is correct |
3 |
Correct |
11 ms |
1528 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
416 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
420 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
380 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
10 ms |
1528 KB |
Output is correct |
17 |
Correct |
107 ms |
9876 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
504 KB |
Output is correct |
3 |
Correct |
15 ms |
504 KB |
Output is correct |
4 |
Correct |
3 ms |
440 KB |
Output is correct |
5 |
Correct |
570 ms |
74096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
893 ms |
86188 KB |
Output is correct |
3 |
Correct |
960 ms |
86204 KB |
Output is correct |
4 |
Correct |
905 ms |
86136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
893 ms |
86188 KB |
Output is correct |
3 |
Correct |
960 ms |
86204 KB |
Output is correct |
4 |
Correct |
905 ms |
86136 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
942 ms |
86316 KB |
Output is correct |
7 |
Correct |
887 ms |
86216 KB |
Output is correct |
8 |
Correct |
918 ms |
86164 KB |
Output is correct |
9 |
Correct |
1102 ms |
86084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
893 ms |
86188 KB |
Output is correct |
3 |
Correct |
960 ms |
86204 KB |
Output is correct |
4 |
Correct |
905 ms |
86136 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
82 ms |
9808 KB |
Output is correct |
7 |
Correct |
91 ms |
9836 KB |
Output is correct |
8 |
Correct |
85 ms |
9836 KB |
Output is correct |
9 |
Correct |
91 ms |
9964 KB |
Output is correct |
10 |
Correct |
78 ms |
9836 KB |
Output is correct |
11 |
Correct |
89 ms |
9836 KB |
Output is correct |
12 |
Correct |
82 ms |
9836 KB |
Output is correct |
13 |
Correct |
103 ms |
9812 KB |
Output is correct |
14 |
Correct |
82 ms |
9804 KB |
Output is correct |
15 |
Correct |
106 ms |
9836 KB |
Output is correct |
16 |
Correct |
103 ms |
9908 KB |
Output is correct |
17 |
Correct |
90 ms |
9836 KB |
Output is correct |
18 |
Correct |
1034 ms |
85908 KB |
Output is correct |
19 |
Correct |
871 ms |
86204 KB |
Output is correct |
20 |
Correct |
1145 ms |
86076 KB |
Output is correct |
21 |
Correct |
110 ms |
9836 KB |
Output is correct |
22 |
Correct |
76 ms |
9908 KB |
Output is correct |
23 |
Correct |
230 ms |
34772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
100 ms |
9912 KB |
Output is correct |
3 |
Correct |
101 ms |
9836 KB |
Output is correct |
4 |
Correct |
1215 ms |
85792 KB |
Output is correct |
5 |
Correct |
104 ms |
9836 KB |
Output is correct |
6 |
Correct |
103 ms |
9872 KB |
Output is correct |
7 |
Correct |
104 ms |
9836 KB |
Output is correct |
8 |
Correct |
107 ms |
9836 KB |
Output is correct |
9 |
Correct |
98 ms |
9968 KB |
Output is correct |
10 |
Correct |
108 ms |
9836 KB |
Output is correct |
11 |
Correct |
101 ms |
9964 KB |
Output is correct |
12 |
Correct |
87 ms |
9836 KB |
Output is correct |
13 |
Correct |
109 ms |
9836 KB |
Output is correct |
14 |
Correct |
1106 ms |
85896 KB |
Output is correct |
15 |
Correct |
109 ms |
9836 KB |
Output is correct |
16 |
Correct |
840 ms |
63568 KB |
Output is correct |
17 |
Correct |
908 ms |
62840 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
10 ms |
1528 KB |
Output is correct |
3 |
Correct |
11 ms |
1528 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
416 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
420 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
380 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
10 ms |
1528 KB |
Output is correct |
17 |
Correct |
107 ms |
9876 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
504 KB |
Output is correct |
26 |
Correct |
15 ms |
504 KB |
Output is correct |
27 |
Correct |
3 ms |
440 KB |
Output is correct |
28 |
Correct |
570 ms |
74096 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
893 ms |
86188 KB |
Output is correct |
31 |
Correct |
960 ms |
86204 KB |
Output is correct |
32 |
Correct |
905 ms |
86136 KB |
Output is correct |
33 |
Correct |
2 ms |
376 KB |
Output is correct |
34 |
Correct |
942 ms |
86316 KB |
Output is correct |
35 |
Correct |
887 ms |
86216 KB |
Output is correct |
36 |
Correct |
918 ms |
86164 KB |
Output is correct |
37 |
Correct |
1102 ms |
86084 KB |
Output is correct |
38 |
Correct |
2 ms |
376 KB |
Output is correct |
39 |
Correct |
82 ms |
9808 KB |
Output is correct |
40 |
Correct |
91 ms |
9836 KB |
Output is correct |
41 |
Correct |
85 ms |
9836 KB |
Output is correct |
42 |
Correct |
91 ms |
9964 KB |
Output is correct |
43 |
Correct |
78 ms |
9836 KB |
Output is correct |
44 |
Correct |
89 ms |
9836 KB |
Output is correct |
45 |
Correct |
82 ms |
9836 KB |
Output is correct |
46 |
Correct |
103 ms |
9812 KB |
Output is correct |
47 |
Correct |
82 ms |
9804 KB |
Output is correct |
48 |
Correct |
106 ms |
9836 KB |
Output is correct |
49 |
Correct |
103 ms |
9908 KB |
Output is correct |
50 |
Correct |
90 ms |
9836 KB |
Output is correct |
51 |
Correct |
1034 ms |
85908 KB |
Output is correct |
52 |
Correct |
871 ms |
86204 KB |
Output is correct |
53 |
Correct |
1145 ms |
86076 KB |
Output is correct |
54 |
Correct |
110 ms |
9836 KB |
Output is correct |
55 |
Correct |
76 ms |
9908 KB |
Output is correct |
56 |
Correct |
230 ms |
34772 KB |
Output is correct |
57 |
Correct |
2 ms |
376 KB |
Output is correct |
58 |
Correct |
100 ms |
9912 KB |
Output is correct |
59 |
Correct |
101 ms |
9836 KB |
Output is correct |
60 |
Correct |
1215 ms |
85792 KB |
Output is correct |
61 |
Correct |
104 ms |
9836 KB |
Output is correct |
62 |
Correct |
103 ms |
9872 KB |
Output is correct |
63 |
Correct |
104 ms |
9836 KB |
Output is correct |
64 |
Correct |
107 ms |
9836 KB |
Output is correct |
65 |
Correct |
98 ms |
9968 KB |
Output is correct |
66 |
Correct |
108 ms |
9836 KB |
Output is correct |
67 |
Correct |
101 ms |
9964 KB |
Output is correct |
68 |
Correct |
87 ms |
9836 KB |
Output is correct |
69 |
Correct |
109 ms |
9836 KB |
Output is correct |
70 |
Correct |
1106 ms |
85896 KB |
Output is correct |
71 |
Correct |
109 ms |
9836 KB |
Output is correct |
72 |
Correct |
840 ms |
63568 KB |
Output is correct |
73 |
Correct |
908 ms |
62840 KB |
Output is correct |
74 |
Correct |
2 ms |
376 KB |
Output is correct |
75 |
Correct |
2 ms |
380 KB |
Output is correct |
76 |
Correct |
2 ms |
376 KB |
Output is correct |
77 |
Correct |
2 ms |
376 KB |
Output is correct |
78 |
Correct |
2 ms |
376 KB |
Output is correct |
79 |
Correct |
9 ms |
1528 KB |
Output is correct |
80 |
Correct |
1188 ms |
62276 KB |
Output is correct |
81 |
Correct |
1266 ms |
62716 KB |
Output is correct |
82 |
Correct |
1100 ms |
86184 KB |
Output is correct |
83 |
Correct |
1097 ms |
68048 KB |
Output is correct |
84 |
Correct |
1114 ms |
85780 KB |
Output is correct |
85 |
Correct |
1109 ms |
67732 KB |
Output is correct |
86 |
Correct |
236 ms |
34720 KB |
Output is correct |
87 |
Correct |
2 ms |
376 KB |
Output is correct |