#include<bits/stdc++.h>
#define taskname "A"
using namespace std;
typedef long long ll;
const int lim = 1e6 + 5;
const ll INF = 1e18;
int n;
ll A, B, S = 0, l[lim], r[lim];
namespace sub1{
void solve(){
map<pair<ll, ll>, bool>cnt;
for(int i = 0; i < n; i++){
for(ll j = l[i]; j <= r[i]; j++){
cnt[make_pair((j + j / B) % A, j % B)] = true;
}
}
cout << cnt.size();
}
}
ll next_mod(ll current, ll d, ll r){
ll R = current % d;
if(R < r){
return current + r - R;
}
return current + d - R + r;
}
ll prev_mod(ll current, ll d, ll r){
ll R = current % d;
if(R > r){
return current - R + r;
}
return current - d + r - R;
}
namespace sub2{
void solve(){
ll cnt_cycle = A / __gcd(A, B + 1), init_cnt = (prev_mod(r[0], B, l[0] % B) - l[0]) / B + 1;
if(l[0] % B < r[0] % B){
cout << min(init_cnt, cnt_cycle) * (r[0] % B - l[0] % B + 1) + min(init_cnt - 1, cnt_cycle) * (B - r[0] % B - 1 + l[0] % B);
}
else{
cout << min(init_cnt, cnt_cycle) * (B - l[0] % B + r[0] % B + 1) + min(init_cnt - 1, cnt_cycle) * (l[0] % B + B - r[0] % B - 1);
}
}
}
namespace sub4{
void solve(){
if(~A & 1){
A >>= 1LL;
}
vector<pair<ll, ll>>range;
for(int i = 0; i < n; i++){
if(r[i] - l[i] + 1 >= A){
return void(cout << A);
}
if((l[i] %= A) <= (r[i] %= A)){
range.emplace_back(l[i], r[i]);
}
else{
range.emplace_back(l[i], A - 1);
range.emplace_back(0, r[i]);
}
}
sort(range.begin(), range.end());
ll ans = range[0].second - range[0].first + 1, pre = range[0].second;
for(int i = 1; i < range.size(); i++){
if(range[i].second > pre){
ans += range[i].second - max(range[i].first, pre + 1) + 1;
pre = range[i].second;
}
}
cout << ans;
}
}
namespace sub35678{
void solve(){
map<ll, int>cnt;
if(INF / A < (B + 1) / __gcd(A, B + 1)){
return void(cout << S);
}
ll cyc = A / __gcd(A, B + 1) * B;
vector<pair<ll, ll>>range;
for(int i = 0; i < n; i++){
if(r[i] - l[i] + 1 >= cyc){
return void(cout << cyc);
}
if((l[i] %= cyc) <= (r[i] %= cyc)){
range.emplace_back(l[i], r[i]);
}
else{
range.emplace_back(l[i], cyc - 1);
range.emplace_back(0, r[i]);
}
}
sort(range.begin(), range.end());
ll ans = range[0].second - range[0].first + 1, pre = range[0].second;
for(int i = 1; i < range.size(); i++){
if(range[i].second > pre){
ans += range[i].second - max(range[i].first, pre + 1) + 1;
pre = range[i].second;
}
}
cout << ans;
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if(fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
}
cin >> n >> A >> B;
for(int i = 0; i < n; i++){
cin >> l[i] >> r[i];
S += r[i] - l[i] + 1;
}
if(S <= 1000000){
sub1::solve();
}
else if(n == 1){
sub2::solve();
}
else if(B == 1){
sub4::solve();
}
else{
sub35678::solve();
}
}
Compilation message
strange_device.cpp: In function 'void sub4::solve()':
strange_device.cpp:65:20: 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]
65 | for(int i = 1; i < range.size(); i++){
| ~~^~~~~~~~~~~~~~
strange_device.cpp: In function 'void sub35678::solve()':
strange_device.cpp:96:20: 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]
96 | for(int i = 1; i < range.size(); i++){
| ~~^~~~~~~~~~~~~~
strange_device.cpp: In function 'int main()':
strange_device.cpp:108:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
108 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
39 ms |
14508 KB |
Output is correct |
3 |
Correct |
66 ms |
20204 KB |
Output is correct |
4 |
Correct |
3 ms |
2904 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2648 KB |
Output is correct |
7 |
Correct |
2 ms |
2648 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
7 ms |
3164 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2556 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
35 ms |
9044 KB |
Output is correct |
16 |
Correct |
22 ms |
8796 KB |
Output is correct |
17 |
Correct |
43 ms |
12948 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2404 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
113 ms |
34388 KB |
Output is correct |
3 |
Correct |
147 ms |
34644 KB |
Output is correct |
4 |
Correct |
123 ms |
32728 KB |
Output is correct |
5 |
Correct |
232 ms |
33472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
342 ms |
32984 KB |
Output is correct |
3 |
Correct |
324 ms |
34024 KB |
Output is correct |
4 |
Correct |
334 ms |
33004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
342 ms |
32984 KB |
Output is correct |
3 |
Correct |
324 ms |
34024 KB |
Output is correct |
4 |
Correct |
334 ms |
33004 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
355 ms |
34120 KB |
Output is correct |
7 |
Correct |
338 ms |
71548 KB |
Output is correct |
8 |
Correct |
368 ms |
69804 KB |
Output is correct |
9 |
Correct |
378 ms |
71000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
342 ms |
32984 KB |
Output is correct |
3 |
Correct |
324 ms |
34024 KB |
Output is correct |
4 |
Correct |
334 ms |
33004 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
34 ms |
8952 KB |
Output is correct |
7 |
Correct |
33 ms |
12496 KB |
Output is correct |
8 |
Correct |
41 ms |
12636 KB |
Output is correct |
9 |
Correct |
45 ms |
12480 KB |
Output is correct |
10 |
Correct |
32 ms |
12692 KB |
Output is correct |
11 |
Correct |
38 ms |
12876 KB |
Output is correct |
12 |
Correct |
32 ms |
12652 KB |
Output is correct |
13 |
Correct |
39 ms |
12632 KB |
Output is correct |
14 |
Correct |
34 ms |
12612 KB |
Output is correct |
15 |
Correct |
41 ms |
12648 KB |
Output is correct |
16 |
Correct |
33 ms |
12512 KB |
Output is correct |
17 |
Correct |
37 ms |
12752 KB |
Output is correct |
18 |
Correct |
378 ms |
69776 KB |
Output is correct |
19 |
Correct |
338 ms |
70388 KB |
Output is correct |
20 |
Correct |
374 ms |
70524 KB |
Output is correct |
21 |
Correct |
40 ms |
13028 KB |
Output is correct |
22 |
Correct |
40 ms |
12500 KB |
Output is correct |
23 |
Correct |
115 ms |
32728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
35 ms |
11260 KB |
Output is correct |
3 |
Correct |
35 ms |
12664 KB |
Output is correct |
4 |
Correct |
396 ms |
70204 KB |
Output is correct |
5 |
Correct |
45 ms |
12752 KB |
Output is correct |
6 |
Correct |
39 ms |
12768 KB |
Output is correct |
7 |
Correct |
43 ms |
12760 KB |
Output is correct |
8 |
Correct |
42 ms |
12648 KB |
Output is correct |
9 |
Correct |
42 ms |
12496 KB |
Output is correct |
10 |
Correct |
39 ms |
12632 KB |
Output is correct |
11 |
Correct |
39 ms |
12624 KB |
Output is correct |
12 |
Correct |
37 ms |
12512 KB |
Output is correct |
13 |
Correct |
39 ms |
12500 KB |
Output is correct |
14 |
Correct |
381 ms |
70512 KB |
Output is correct |
15 |
Correct |
42 ms |
12760 KB |
Output is correct |
16 |
Correct |
364 ms |
70604 KB |
Output is correct |
17 |
Correct |
347 ms |
70728 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
39 ms |
14508 KB |
Output is correct |
3 |
Correct |
66 ms |
20204 KB |
Output is correct |
4 |
Correct |
3 ms |
2904 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2648 KB |
Output is correct |
7 |
Correct |
2 ms |
2648 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
7 ms |
3164 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2556 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
35 ms |
9044 KB |
Output is correct |
16 |
Correct |
22 ms |
8796 KB |
Output is correct |
17 |
Correct |
43 ms |
12948 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2404 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
113 ms |
34388 KB |
Output is correct |
26 |
Correct |
147 ms |
34644 KB |
Output is correct |
27 |
Correct |
123 ms |
32728 KB |
Output is correct |
28 |
Correct |
232 ms |
33472 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
342 ms |
32984 KB |
Output is correct |
31 |
Correct |
324 ms |
34024 KB |
Output is correct |
32 |
Correct |
334 ms |
33004 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
355 ms |
34120 KB |
Output is correct |
35 |
Correct |
338 ms |
71548 KB |
Output is correct |
36 |
Correct |
368 ms |
69804 KB |
Output is correct |
37 |
Correct |
378 ms |
71000 KB |
Output is correct |
38 |
Correct |
1 ms |
2392 KB |
Output is correct |
39 |
Correct |
34 ms |
8952 KB |
Output is correct |
40 |
Correct |
33 ms |
12496 KB |
Output is correct |
41 |
Correct |
41 ms |
12636 KB |
Output is correct |
42 |
Correct |
45 ms |
12480 KB |
Output is correct |
43 |
Correct |
32 ms |
12692 KB |
Output is correct |
44 |
Correct |
38 ms |
12876 KB |
Output is correct |
45 |
Correct |
32 ms |
12652 KB |
Output is correct |
46 |
Correct |
39 ms |
12632 KB |
Output is correct |
47 |
Correct |
34 ms |
12612 KB |
Output is correct |
48 |
Correct |
41 ms |
12648 KB |
Output is correct |
49 |
Correct |
33 ms |
12512 KB |
Output is correct |
50 |
Correct |
37 ms |
12752 KB |
Output is correct |
51 |
Correct |
378 ms |
69776 KB |
Output is correct |
52 |
Correct |
338 ms |
70388 KB |
Output is correct |
53 |
Correct |
374 ms |
70524 KB |
Output is correct |
54 |
Correct |
40 ms |
13028 KB |
Output is correct |
55 |
Correct |
40 ms |
12500 KB |
Output is correct |
56 |
Correct |
115 ms |
32728 KB |
Output is correct |
57 |
Correct |
1 ms |
2396 KB |
Output is correct |
58 |
Correct |
35 ms |
11260 KB |
Output is correct |
59 |
Correct |
35 ms |
12664 KB |
Output is correct |
60 |
Correct |
396 ms |
70204 KB |
Output is correct |
61 |
Correct |
45 ms |
12752 KB |
Output is correct |
62 |
Correct |
39 ms |
12768 KB |
Output is correct |
63 |
Correct |
43 ms |
12760 KB |
Output is correct |
64 |
Correct |
42 ms |
12648 KB |
Output is correct |
65 |
Correct |
42 ms |
12496 KB |
Output is correct |
66 |
Correct |
39 ms |
12632 KB |
Output is correct |
67 |
Correct |
39 ms |
12624 KB |
Output is correct |
68 |
Correct |
37 ms |
12512 KB |
Output is correct |
69 |
Correct |
39 ms |
12500 KB |
Output is correct |
70 |
Correct |
381 ms |
70512 KB |
Output is correct |
71 |
Correct |
42 ms |
12760 KB |
Output is correct |
72 |
Correct |
364 ms |
70604 KB |
Output is correct |
73 |
Correct |
347 ms |
70728 KB |
Output is correct |
74 |
Correct |
1 ms |
2396 KB |
Output is correct |
75 |
Correct |
1 ms |
2396 KB |
Output is correct |
76 |
Correct |
1 ms |
2396 KB |
Output is correct |
77 |
Correct |
1 ms |
2508 KB |
Output is correct |
78 |
Correct |
1 ms |
2396 KB |
Output is correct |
79 |
Correct |
4 ms |
3164 KB |
Output is correct |
80 |
Correct |
438 ms |
70492 KB |
Output is correct |
81 |
Correct |
439 ms |
70592 KB |
Output is correct |
82 |
Correct |
395 ms |
70432 KB |
Output is correct |
83 |
Correct |
363 ms |
70496 KB |
Output is correct |
84 |
Correct |
356 ms |
70516 KB |
Output is correct |
85 |
Correct |
350 ms |
71832 KB |
Output is correct |
86 |
Correct |
93 ms |
33476 KB |
Output is correct |
87 |
Correct |
1 ms |
2396 KB |
Output is correct |