# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
213016 | arman_ferdous | Strange Device (APIO19_strange_device) | C++17 | 607 ms | 53320 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* at time = 0, (x, y) = (0, 0)
* we need to find minimum t > 0, such that (x, y) = 0 again ie. the cycle period.
* now, y = 0 = t % B => t = K * B (for some integer K)
* putting t in x's eqn and simplifying gives us a nice formula
* K * (B + 1) % A = 0
* from here it is easy to find K's value.
* answering queries are just merging intervals
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll oo = 2e18;
vector<pair<ll,ll>> interval;
int main () {
int n;
ll A, B;
scanf("%d %lld %lld", &n, &A, &B);
int inf = 0, all = 0; ll sum = 0;
ll g = __gcd(A, B + 1);
ll K = A / g;
ll T = K * B;
if(K > oo / B) inf = 1;
assert(inf == 0);
while(n--) {
ll l, r;
scanf("%lld %lld", &l, &r);
if(r - l + 1 >= T) {
all = 1;
break;
} sum += r - l + 1;
ll le = l % T;
ll ri = r % T;
if(le <= ri) {
interval.push_back({le, ri});
}
else {
interval.push_back({0, ri});
interval.push_back({le, T - 1});
}
}
if(inf) printf("%lld\n", sum);
else if(all) printf("%lld\n", T);
else {
ll ans = 0;
sort(interval.begin(), interval.end());
for(int i = 0; i < (int)interval.size(); i++) {
ans += interval[i].second - interval[i].first + 1;
if(i) ans -= max(interval[i - 1].second - interval[i].first + 1, 0ll);
}
printf("%lld\n", ans);
}
return 0;
}
Compilation message (stderr)
# | 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... |