#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(r[i], A - 1);
range.emplace_back(0, l[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;
}
}
return void(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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2904 KB |
Output is correct |
2 |
Correct |
38 ms |
14864 KB |
Output is correct |
3 |
Correct |
59 ms |
20412 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 |
7 ms |
3160 KB |
Output is correct |
10 |
Correct |
1 ms |
2516 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
33 ms |
8976 KB |
Output is correct |
16 |
Correct |
22 ms |
9368 KB |
Output is correct |
17 |
Correct |
44 ms |
14940 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2508 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2508 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
114 ms |
34520 KB |
Output is correct |
3 |
Correct |
131 ms |
34296 KB |
Output is correct |
4 |
Correct |
111 ms |
32604 KB |
Output is correct |
5 |
Incorrect |
171 ms |
28796 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
319 ms |
53064 KB |
Output is correct |
3 |
Incorrect |
301 ms |
71364 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
319 ms |
53064 KB |
Output is correct |
3 |
Incorrect |
301 ms |
71364 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
319 ms |
53064 KB |
Output is correct |
3 |
Incorrect |
301 ms |
71364 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
25 ms |
8664 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2904 KB |
Output is correct |
2 |
Correct |
38 ms |
14864 KB |
Output is correct |
3 |
Correct |
59 ms |
20412 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 |
7 ms |
3160 KB |
Output is correct |
10 |
Correct |
1 ms |
2516 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
33 ms |
8976 KB |
Output is correct |
16 |
Correct |
22 ms |
9368 KB |
Output is correct |
17 |
Correct |
44 ms |
14940 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 |
2508 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2508 KB |
Output is correct |
23 |
Correct |
0 ms |
2396 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
114 ms |
34520 KB |
Output is correct |
26 |
Correct |
131 ms |
34296 KB |
Output is correct |
27 |
Correct |
111 ms |
32604 KB |
Output is correct |
28 |
Incorrect |
171 ms |
28796 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |