#include <bits/stdc++.h>
using namespace std;
class strangeDevice{
private:
using ll=long long;
using i128=__int128;
public:
long long count(long long A, long long B, vector<long long> left, vector<long long> right) {
long long ans{0ll};
i128 F = i128(A / __gcd(A, B + 1)) * B;
vector<pair<ll,ll> > rgs, rgx;
for(int i{0}; i < (int)left.size(); ++i){
long long l{left[i]}, r{right[i]};
long long len{r - l + 1};
if(len>=F) return F;
l %= F, r %= F;
if(l <= r) rgs.emplace_back(l, r);
else rgs.emplace_back(l, F - 1),rgs.emplace_back(0, r);
}
sort(rgs.begin(), rgs.end());
for(auto [l,r]: rgs){
if(rgx.empty() || rgx.back().second < l ) rgx.emplace_back(l, r);
else rgx.back().second = max(rgx.back().second, r);
}
for(auto [l,r]: rgx) ans += r - l + 1;
return ans;
}
}solution;
int main() {
cin.tie(0) -> sync_with_stdio(0);
int lengthOfL;
long long A, B;
cin >> lengthOfL >> A >> B;
vector<long long> L(lengthOfL),R(lengthOfL);
for(int i = 0; i < lengthOfL; i++ ){
cin >> L[i] >> R[i];
}
cout << solution.count(A, B, L, R) << '\n';
return 0;
}
/*
这类题目可以考虑找循环节
t1=xB+y,t2=x1B+y
x1B+y+[(x1B+y)/B]=x2B+y+[(x2B+y)/B]
x1B+y+x1=x2B+y+x2
(x1-x2)(B+1)=0 (mod A)
x1-x2=K* A/gcd(A,B+1)
t1-t2=K* AB/gcd(A,B+1)
∴循环节(最小正周期) F=AB/gcd(A,B+1)
转化成:有一些区间,求区间的并 %F 意义下有多少个不同的数
len>=F: ans=F
否则转化成了区间覆盖操作
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
4 ms |
1496 KB |
Output is correct |
3 |
Correct |
4 ms |
1496 KB |
Output is correct |
4 |
Correct |
1 ms |
344 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 |
1 ms |
348 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 |
4 ms |
1620 KB |
Output is correct |
17 |
Correct |
44 ms |
11884 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
228 ms |
74456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
338 ms |
112252 KB |
Output is correct |
3 |
Correct |
319 ms |
109848 KB |
Output is correct |
4 |
Correct |
316 ms |
111828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
338 ms |
112252 KB |
Output is correct |
3 |
Correct |
319 ms |
109848 KB |
Output is correct |
4 |
Correct |
316 ms |
111828 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
350 ms |
110424 KB |
Output is correct |
7 |
Correct |
342 ms |
111700 KB |
Output is correct |
8 |
Correct |
336 ms |
109804 KB |
Output is correct |
9 |
Correct |
368 ms |
111948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
338 ms |
112252 KB |
Output is correct |
3 |
Correct |
319 ms |
109848 KB |
Output is correct |
4 |
Correct |
316 ms |
111828 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
32 ms |
11752 KB |
Output is correct |
7 |
Correct |
35 ms |
11720 KB |
Output is correct |
8 |
Correct |
37 ms |
11828 KB |
Output is correct |
9 |
Correct |
33 ms |
11720 KB |
Output is correct |
10 |
Correct |
32 ms |
11724 KB |
Output is correct |
11 |
Correct |
34 ms |
11720 KB |
Output is correct |
12 |
Correct |
32 ms |
11728 KB |
Output is correct |
13 |
Correct |
34 ms |
11724 KB |
Output is correct |
14 |
Correct |
32 ms |
11960 KB |
Output is correct |
15 |
Correct |
36 ms |
11832 KB |
Output is correct |
16 |
Correct |
37 ms |
11856 KB |
Output is correct |
17 |
Correct |
33 ms |
11724 KB |
Output is correct |
18 |
Correct |
362 ms |
109228 KB |
Output is correct |
19 |
Correct |
340 ms |
110560 KB |
Output is correct |
20 |
Correct |
369 ms |
111540 KB |
Output is correct |
21 |
Correct |
35 ms |
11744 KB |
Output is correct |
22 |
Correct |
37 ms |
11736 KB |
Output is correct |
23 |
Correct |
113 ms |
32144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
39 ms |
11740 KB |
Output is correct |
3 |
Correct |
36 ms |
11724 KB |
Output is correct |
4 |
Correct |
370 ms |
110580 KB |
Output is correct |
5 |
Correct |
37 ms |
11728 KB |
Output is correct |
6 |
Correct |
39 ms |
11732 KB |
Output is correct |
7 |
Correct |
35 ms |
11920 KB |
Output is correct |
8 |
Correct |
35 ms |
11720 KB |
Output is correct |
9 |
Correct |
36 ms |
11864 KB |
Output is correct |
10 |
Correct |
36 ms |
11724 KB |
Output is correct |
11 |
Correct |
35 ms |
11724 KB |
Output is correct |
12 |
Correct |
31 ms |
11844 KB |
Output is correct |
13 |
Correct |
35 ms |
11728 KB |
Output is correct |
14 |
Correct |
356 ms |
109520 KB |
Output is correct |
15 |
Correct |
34 ms |
11720 KB |
Output is correct |
16 |
Correct |
318 ms |
111284 KB |
Output is correct |
17 |
Correct |
334 ms |
111316 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
4 ms |
1496 KB |
Output is correct |
3 |
Correct |
4 ms |
1496 KB |
Output is correct |
4 |
Correct |
1 ms |
344 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 |
1 ms |
348 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 |
4 ms |
1620 KB |
Output is correct |
17 |
Correct |
44 ms |
11884 KB |
Output is correct |
18 |
Correct |
0 ms |
344 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 |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
228 ms |
74456 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
338 ms |
112252 KB |
Output is correct |
31 |
Correct |
319 ms |
109848 KB |
Output is correct |
32 |
Correct |
316 ms |
111828 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
350 ms |
110424 KB |
Output is correct |
35 |
Correct |
342 ms |
111700 KB |
Output is correct |
36 |
Correct |
336 ms |
109804 KB |
Output is correct |
37 |
Correct |
368 ms |
111948 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
32 ms |
11752 KB |
Output is correct |
40 |
Correct |
35 ms |
11720 KB |
Output is correct |
41 |
Correct |
37 ms |
11828 KB |
Output is correct |
42 |
Correct |
33 ms |
11720 KB |
Output is correct |
43 |
Correct |
32 ms |
11724 KB |
Output is correct |
44 |
Correct |
34 ms |
11720 KB |
Output is correct |
45 |
Correct |
32 ms |
11728 KB |
Output is correct |
46 |
Correct |
34 ms |
11724 KB |
Output is correct |
47 |
Correct |
32 ms |
11960 KB |
Output is correct |
48 |
Correct |
36 ms |
11832 KB |
Output is correct |
49 |
Correct |
37 ms |
11856 KB |
Output is correct |
50 |
Correct |
33 ms |
11724 KB |
Output is correct |
51 |
Correct |
362 ms |
109228 KB |
Output is correct |
52 |
Correct |
340 ms |
110560 KB |
Output is correct |
53 |
Correct |
369 ms |
111540 KB |
Output is correct |
54 |
Correct |
35 ms |
11744 KB |
Output is correct |
55 |
Correct |
37 ms |
11736 KB |
Output is correct |
56 |
Correct |
113 ms |
32144 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
39 ms |
11740 KB |
Output is correct |
59 |
Correct |
36 ms |
11724 KB |
Output is correct |
60 |
Correct |
370 ms |
110580 KB |
Output is correct |
61 |
Correct |
37 ms |
11728 KB |
Output is correct |
62 |
Correct |
39 ms |
11732 KB |
Output is correct |
63 |
Correct |
35 ms |
11920 KB |
Output is correct |
64 |
Correct |
35 ms |
11720 KB |
Output is correct |
65 |
Correct |
36 ms |
11864 KB |
Output is correct |
66 |
Correct |
36 ms |
11724 KB |
Output is correct |
67 |
Correct |
35 ms |
11724 KB |
Output is correct |
68 |
Correct |
31 ms |
11844 KB |
Output is correct |
69 |
Correct |
35 ms |
11728 KB |
Output is correct |
70 |
Correct |
356 ms |
109520 KB |
Output is correct |
71 |
Correct |
34 ms |
11720 KB |
Output is correct |
72 |
Correct |
318 ms |
111284 KB |
Output is correct |
73 |
Correct |
334 ms |
111316 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 |
4 ms |
1484 KB |
Output is correct |
80 |
Correct |
367 ms |
111148 KB |
Output is correct |
81 |
Correct |
372 ms |
110256 KB |
Output is correct |
82 |
Correct |
334 ms |
111868 KB |
Output is correct |
83 |
Correct |
349 ms |
109724 KB |
Output is correct |
84 |
Correct |
355 ms |
111800 KB |
Output is correct |
85 |
Correct |
338 ms |
111280 KB |
Output is correct |
86 |
Correct |
94 ms |
32200 KB |
Output is correct |
87 |
Correct |
1 ms |
344 KB |
Output is correct |