/*
Code written by Talant I.D.
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef __int128 bigInt;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef tree <pii, null_type, less <pii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#define precision(n) fixed << setprecision(n)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define eps (double)1e-9
#define PI 2*acos(0.0)
#define endl "\n"
#define sz(v) int((v).size())
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define OK cout << "OK" << endl;
const int mod = 1e9+7;
ll mode(ll a) {
a %= mod;
if (a < 0) a += mod;
return a;
}
ll subt(ll a, ll b) {
return mode(mode(a)-mode(b));
}
ll add(ll a, ll b) {
return mode(mode(a)+mode(b));
}
ll mult(ll a, ll b) {
return mode(mode(a)*mode(b));
}
ll binpow(ll a, ll b) {
ll res = 1;
while (b) {
if (b&1) res = mult(res, a);
a = mult(a, a);
b >>= 1;
}
return res;
}
void usaco(string s) {
freopen((s+".in").c_str(), "r", stdin);
freopen((s+".out").c_str(), "w", stdout);
}
ll n, a, b;
ll A(ll x) {
return (x+x/b)%a;
}
ll B(ll x) {
return x%b;
}
bool check(bigInt a, bigInt b) {
return a*b > ll(1e18);
}
int main() {
do_not_disturb
//~ usaco("dirtraverse");
cin >> n >> a >> b;
vector <pll> v(n), segs;
for (auto &to : v) cin >> to.first >> to.second;
a = a/__gcd(a, b+1);
ll t = (check(a, b) ? (ll)1e18 : a*b);
bool flag = 0;
for (auto to : v) {
if (to.first/t == to.second/t) {
segs.pb(mp(to.first%t, to.second%t));
}
else if (to.first/t+1 == to.second/t) {
segs.pb(mp(to.first%t, t-1));
segs.pb(mp(0, to.second%t));
}
else {
flag = 1;
}
}
if (flag) cout << t << endl;
else {
ll ans = 0;
sort(all(segs));
for (int i = 0, j = 0; i < sz(segs); i = j) {
auto l = segs[i].first, r = segs[i].second;
for ( ; j < sz(segs) && segs[j].first <= r; j++) {
r = max(r, segs[j].second);
}
ans += r-l+1;
}
cout << ans << endl;
}
return 0;
}
Compilation message
strange_device.cpp: In function 'void usaco(std::string)':
strange_device.cpp:61:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
61 | freopen((s+".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
strange_device.cpp:62:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
62 | freopen((s+".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
5 ms |
876 KB |
Output is correct |
3 |
Correct |
5 ms |
876 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
2 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
2 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
8 ms |
876 KB |
Output is correct |
17 |
Correct |
53 ms |
4092 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
2 ms |
364 KB |
Output is correct |
5 |
Correct |
382 ms |
33480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
465 ms |
37904 KB |
Output is correct |
3 |
Correct |
461 ms |
32604 KB |
Output is correct |
4 |
Correct |
499 ms |
32544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
465 ms |
37904 KB |
Output is correct |
3 |
Correct |
461 ms |
32604 KB |
Output is correct |
4 |
Correct |
499 ms |
32544 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
512 ms |
32544 KB |
Output is correct |
7 |
Correct |
493 ms |
32604 KB |
Output is correct |
8 |
Correct |
456 ms |
48860 KB |
Output is correct |
9 |
Correct |
524 ms |
37904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
465 ms |
37904 KB |
Output is correct |
3 |
Correct |
461 ms |
32604 KB |
Output is correct |
4 |
Correct |
499 ms |
32544 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
46 ms |
6888 KB |
Output is correct |
7 |
Correct |
68 ms |
7784 KB |
Output is correct |
8 |
Correct |
55 ms |
7788 KB |
Output is correct |
9 |
Correct |
63 ms |
7784 KB |
Output is correct |
10 |
Correct |
49 ms |
7856 KB |
Output is correct |
11 |
Correct |
52 ms |
7912 KB |
Output is correct |
12 |
Correct |
47 ms |
7784 KB |
Output is correct |
13 |
Correct |
60 ms |
7784 KB |
Output is correct |
14 |
Correct |
47 ms |
7784 KB |
Output is correct |
15 |
Correct |
57 ms |
7912 KB |
Output is correct |
16 |
Correct |
51 ms |
7784 KB |
Output is correct |
17 |
Correct |
48 ms |
7784 KB |
Output is correct |
18 |
Correct |
467 ms |
70104 KB |
Output is correct |
19 |
Correct |
476 ms |
69748 KB |
Output is correct |
20 |
Correct |
522 ms |
69772 KB |
Output is correct |
21 |
Correct |
50 ms |
7844 KB |
Output is correct |
22 |
Correct |
45 ms |
7844 KB |
Output is correct |
23 |
Correct |
153 ms |
26848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
49 ms |
4064 KB |
Output is correct |
3 |
Correct |
51 ms |
7784 KB |
Output is correct |
4 |
Correct |
582 ms |
69696 KB |
Output is correct |
5 |
Correct |
53 ms |
7784 KB |
Output is correct |
6 |
Correct |
50 ms |
7784 KB |
Output is correct |
7 |
Correct |
50 ms |
7784 KB |
Output is correct |
8 |
Correct |
51 ms |
7784 KB |
Output is correct |
9 |
Correct |
58 ms |
8040 KB |
Output is correct |
10 |
Correct |
51 ms |
7804 KB |
Output is correct |
11 |
Correct |
50 ms |
7784 KB |
Output is correct |
12 |
Correct |
45 ms |
7784 KB |
Output is correct |
13 |
Correct |
50 ms |
7784 KB |
Output is correct |
14 |
Correct |
555 ms |
69140 KB |
Output is correct |
15 |
Correct |
50 ms |
7784 KB |
Output is correct |
16 |
Correct |
478 ms |
69548 KB |
Output is correct |
17 |
Correct |
541 ms |
68844 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
5 ms |
876 KB |
Output is correct |
3 |
Correct |
5 ms |
876 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
2 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
2 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
8 ms |
876 KB |
Output is correct |
17 |
Correct |
53 ms |
4092 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
2 ms |
364 KB |
Output is correct |
28 |
Correct |
382 ms |
33480 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
465 ms |
37904 KB |
Output is correct |
31 |
Correct |
461 ms |
32604 KB |
Output is correct |
32 |
Correct |
499 ms |
32544 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
512 ms |
32544 KB |
Output is correct |
35 |
Correct |
493 ms |
32604 KB |
Output is correct |
36 |
Correct |
456 ms |
48860 KB |
Output is correct |
37 |
Correct |
524 ms |
37904 KB |
Output is correct |
38 |
Correct |
1 ms |
364 KB |
Output is correct |
39 |
Correct |
46 ms |
6888 KB |
Output is correct |
40 |
Correct |
68 ms |
7784 KB |
Output is correct |
41 |
Correct |
55 ms |
7788 KB |
Output is correct |
42 |
Correct |
63 ms |
7784 KB |
Output is correct |
43 |
Correct |
49 ms |
7856 KB |
Output is correct |
44 |
Correct |
52 ms |
7912 KB |
Output is correct |
45 |
Correct |
47 ms |
7784 KB |
Output is correct |
46 |
Correct |
60 ms |
7784 KB |
Output is correct |
47 |
Correct |
47 ms |
7784 KB |
Output is correct |
48 |
Correct |
57 ms |
7912 KB |
Output is correct |
49 |
Correct |
51 ms |
7784 KB |
Output is correct |
50 |
Correct |
48 ms |
7784 KB |
Output is correct |
51 |
Correct |
467 ms |
70104 KB |
Output is correct |
52 |
Correct |
476 ms |
69748 KB |
Output is correct |
53 |
Correct |
522 ms |
69772 KB |
Output is correct |
54 |
Correct |
50 ms |
7844 KB |
Output is correct |
55 |
Correct |
45 ms |
7844 KB |
Output is correct |
56 |
Correct |
153 ms |
26848 KB |
Output is correct |
57 |
Correct |
1 ms |
364 KB |
Output is correct |
58 |
Correct |
49 ms |
4064 KB |
Output is correct |
59 |
Correct |
51 ms |
7784 KB |
Output is correct |
60 |
Correct |
582 ms |
69696 KB |
Output is correct |
61 |
Correct |
53 ms |
7784 KB |
Output is correct |
62 |
Correct |
50 ms |
7784 KB |
Output is correct |
63 |
Correct |
50 ms |
7784 KB |
Output is correct |
64 |
Correct |
51 ms |
7784 KB |
Output is correct |
65 |
Correct |
58 ms |
8040 KB |
Output is correct |
66 |
Correct |
51 ms |
7804 KB |
Output is correct |
67 |
Correct |
50 ms |
7784 KB |
Output is correct |
68 |
Correct |
45 ms |
7784 KB |
Output is correct |
69 |
Correct |
50 ms |
7784 KB |
Output is correct |
70 |
Correct |
555 ms |
69140 KB |
Output is correct |
71 |
Correct |
50 ms |
7784 KB |
Output is correct |
72 |
Correct |
478 ms |
69548 KB |
Output is correct |
73 |
Correct |
541 ms |
68844 KB |
Output is correct |
74 |
Correct |
1 ms |
364 KB |
Output is correct |
75 |
Correct |
1 ms |
364 KB |
Output is correct |
76 |
Correct |
1 ms |
364 KB |
Output is correct |
77 |
Correct |
1 ms |
364 KB |
Output is correct |
78 |
Correct |
2 ms |
364 KB |
Output is correct |
79 |
Correct |
6 ms |
1260 KB |
Output is correct |
80 |
Correct |
572 ms |
69828 KB |
Output is correct |
81 |
Correct |
592 ms |
68840 KB |
Output is correct |
82 |
Correct |
570 ms |
69784 KB |
Output is correct |
83 |
Correct |
535 ms |
69528 KB |
Output is correct |
84 |
Correct |
522 ms |
69724 KB |
Output is correct |
85 |
Correct |
507 ms |
69852 KB |
Output is correct |
86 |
Correct |
168 ms |
26560 KB |
Output is correct |
87 |
Correct |
1 ms |
364 KB |
Output is correct |