#include <bits/stdc++.h>
using namespace std;
template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl
typedef long long ll;
const ll mod = 1e9+7;
const int maxn = 1e6 + 5;
ll n, a, b;
ll getx(ll t) {
return (t+t/b)%a;
}
ll gety(ll t) {
return t%b;
}
ll merge(vector<pair<ll,ll>> v) {
sort(v.begin(), v.end());
// for (auto p: v) {
// cout<<p.first<<" "<<p.second<<endl;
// }
ll res = 0;
n = v.size();
auto cur = v.front();
for (int i=1; i<n; i++) {
ll x=v[i].first;
ll y=v[i].second;
if (x>cur.second) {
res += cur.second-cur.first+1;
cur = v[i];
} else {
cur.second=max(cur.second,y);
}
}
res += cur.second-cur.first+1;
return res;
}
vector<pair<ll,ll>> v;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n>>a>>b;
v.resize(n);
for (int i=0; i<n; i++) {
cin>>v[i].first>>v[i].second;
}
auto safe_lcm = [](ll x, ll y) {
ll tmp = x / __gcd(x,y+1);
if (tmp > 1e18/y) return (ll)1e18 + 1;
return tmp*y;
};
ll lcm = safe_lcm(a, b);
if (lcm>1e18) {
//merge intervals
ll res = merge(v);
out(res);
}
vector<pair<ll,ll>> cv;
for (int i=0; i<n; i++) {
v[i].first %= lcm;
v[i].second %= lcm;
if (v[i].second>=v[i].first) {
cv.push_back(v[i]);
} else {
assert(v[i].first>v[i].second);
cv.push_back({v[i].first,lcm-1});
cv.push_back({0,v[i].second});
}
}
ll ans = merge(cv);
out(ans);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
10 ms |
1280 KB |
Output is correct |
3 |
Correct |
10 ms |
1280 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
10 ms |
1280 KB |
Output is correct |
17 |
Correct |
62 ms |
8940 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
394 ms |
48708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
543 ms |
49560 KB |
Output is correct |
3 |
Correct |
518 ms |
49748 KB |
Output is correct |
4 |
Correct |
509 ms |
67936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
543 ms |
49560 KB |
Output is correct |
3 |
Correct |
518 ms |
49748 KB |
Output is correct |
4 |
Correct |
509 ms |
67936 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
533 ms |
67796 KB |
Output is correct |
7 |
Correct |
540 ms |
67780 KB |
Output is correct |
8 |
Correct |
519 ms |
67780 KB |
Output is correct |
9 |
Correct |
570 ms |
67936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
543 ms |
49560 KB |
Output is correct |
3 |
Correct |
518 ms |
49748 KB |
Output is correct |
4 |
Correct |
509 ms |
67936 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
58 ms |
8916 KB |
Output is correct |
7 |
Correct |
63 ms |
8940 KB |
Output is correct |
8 |
Correct |
62 ms |
8812 KB |
Output is correct |
9 |
Correct |
57 ms |
8940 KB |
Output is correct |
10 |
Correct |
58 ms |
8940 KB |
Output is correct |
11 |
Correct |
61 ms |
8940 KB |
Output is correct |
12 |
Correct |
56 ms |
8940 KB |
Output is correct |
13 |
Correct |
58 ms |
8812 KB |
Output is correct |
14 |
Correct |
56 ms |
8940 KB |
Output is correct |
15 |
Correct |
60 ms |
8812 KB |
Output is correct |
16 |
Correct |
64 ms |
8812 KB |
Output is correct |
17 |
Correct |
56 ms |
8812 KB |
Output is correct |
18 |
Correct |
520 ms |
67780 KB |
Output is correct |
19 |
Correct |
534 ms |
67836 KB |
Output is correct |
20 |
Correct |
577 ms |
67804 KB |
Output is correct |
21 |
Correct |
60 ms |
8940 KB |
Output is correct |
22 |
Correct |
57 ms |
8812 KB |
Output is correct |
23 |
Correct |
182 ms |
29248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
59 ms |
8940 KB |
Output is correct |
3 |
Correct |
59 ms |
8940 KB |
Output is correct |
4 |
Correct |
592 ms |
49860 KB |
Output is correct |
5 |
Correct |
58 ms |
8816 KB |
Output is correct |
6 |
Correct |
66 ms |
8940 KB |
Output is correct |
7 |
Correct |
60 ms |
8812 KB |
Output is correct |
8 |
Correct |
61 ms |
8812 KB |
Output is correct |
9 |
Correct |
60 ms |
8812 KB |
Output is correct |
10 |
Correct |
64 ms |
8972 KB |
Output is correct |
11 |
Correct |
59 ms |
8812 KB |
Output is correct |
12 |
Correct |
57 ms |
8940 KB |
Output is correct |
13 |
Correct |
60 ms |
8812 KB |
Output is correct |
14 |
Correct |
565 ms |
49860 KB |
Output is correct |
15 |
Correct |
59 ms |
8812 KB |
Output is correct |
16 |
Correct |
543 ms |
77508 KB |
Output is correct |
17 |
Correct |
526 ms |
68932 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
10 ms |
1280 KB |
Output is correct |
3 |
Correct |
10 ms |
1280 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
10 ms |
1280 KB |
Output is correct |
17 |
Correct |
62 ms |
8940 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
4 ms |
384 KB |
Output is correct |
20 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |