This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |