#include<bits/stdc++.h>
#define taskname "A"
using namespace std;
typedef long long ll;
const int lim = 1e6 + 5;
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;
}
}
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();
}
}
Compilation message
strange_device.cpp: In function 'void sub4::solve()':
strange_device.cpp:64: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]
64 | for(int i = 1; i < range.size(); i++){
| ~~^~~~~~~~~~~~~~
strange_device.cpp: In function 'int main()':
strange_device.cpp:76:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
36 ms |
14428 KB |
Output is correct |
3 |
Correct |
58 ms |
20164 KB |
Output is correct |
4 |
Correct |
2 ms |
2908 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
6 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 |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
32 ms |
9192 KB |
Output is correct |
16 |
Correct |
22 ms |
8792 KB |
Output is correct |
17 |
Correct |
43 ms |
12976 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 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 |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
105 ms |
34396 KB |
Output is correct |
3 |
Correct |
134 ms |
34132 KB |
Output is correct |
4 |
Correct |
113 ms |
32660 KB |
Output is correct |
5 |
Incorrect |
169 ms |
15956 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
296 ms |
34528 KB |
Output is correct |
3 |
Correct |
285 ms |
34008 KB |
Output is correct |
4 |
Correct |
293 ms |
71116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
296 ms |
34528 KB |
Output is correct |
3 |
Correct |
285 ms |
34008 KB |
Output is correct |
4 |
Correct |
293 ms |
71116 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Incorrect |
232 ms |
53108 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
296 ms |
34528 KB |
Output is correct |
3 |
Correct |
285 ms |
34008 KB |
Output is correct |
4 |
Correct |
293 ms |
71116 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Incorrect |
25 ms |
10340 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2548 KB |
Output is correct |
2 |
Incorrect |
31 ms |
6784 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
36 ms |
14428 KB |
Output is correct |
3 |
Correct |
58 ms |
20164 KB |
Output is correct |
4 |
Correct |
2 ms |
2908 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
6 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 |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
32 ms |
9192 KB |
Output is correct |
16 |
Correct |
22 ms |
8792 KB |
Output is correct |
17 |
Correct |
43 ms |
12976 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2392 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
105 ms |
34396 KB |
Output is correct |
26 |
Correct |
134 ms |
34132 KB |
Output is correct |
27 |
Correct |
113 ms |
32660 KB |
Output is correct |
28 |
Incorrect |
169 ms |
15956 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |