#include <bits/stdc++.h>
using namespace std;
#define lll __int128
#define ll lll
#define pll pair<ll, ll>
#define f first
#define s second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)((x).size())
#define pb push_back
#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<" "<<#__VA_ARGS__<<": ", _do(__VA_ARGS__)
template<typename T> void _do(T && x ){ cerr<<x<<endl; }
template<typename T, typename ...S> void _do(T && x , S&&...y){ cerr<<x<<", "; _do(y...);}
#define IOS()
#else
#define IOS() ios::sync_with_stdio(0),cin.tie(0)
#define endl '\n'
#define bug(...)
#endif // BALBIT
ll n,A,B,gg;
ll non_prime_inv(ll a, ll b)
{
ll b0 = b, t, q;
ll x0 = 0, x1 = 1;
if (b == 1) return 1;
while (a > 1) {
q = a / b;
t = b, b = a % b, a = t;
t = x0, x0 = x1 - q * x0, x1 = t;
}
if (x1 < 0) x1 += b0;
return x1;
}
pair<ll, ll> pos(ll a, ll b) {
// get the group for this time
ll an = ((a - b ) % A + A)%A;
ll grp = an % gg;
an /= gg;
ll K = an * non_prime_inv((B+1)/gg, (A)/gg) %(A/gg);
return {grp, K * B + b};
}
pair<ll, ll> gt(ll t) {
return {(t+t/B)%A, t%B};
}
void RD(ll & x) {
long long int t; cin>>t;
x = t;
}
signed main(){
IOS();
bug(1,2,3);
RD(n); RD(A); RD(B);
gg = __gcd(A,B+1);
vector<pll> mp;
lll GS = A/gg*(lll)B;
ll start = 0;
for (int i = 0; i<n; ++i) {
ll L, R; RD(L); RD(R);
if (R-L+1 >= GS) {
cout<<(long long int )GS<<endl; return 0;
}
++R;
pll lp=pos((L+L/B)%A, L%B);
ll Lto = lp.s%GS;
mp.pb({Lto % GS,1});
pll rp = pos((R+R/B)%A, R%B);
ll Rto = rp.s%GS;
mp.pb({Rto % GS,-1});
assert(lp.f == rp.f);
// bug(lp.f, Lto, Rto);
if (Lto > Rto) {
++start;
}
}
sort(ALL(mp));
mp.pb({GS, 0});
ll re = 0;
ll prev = 0;
for (auto &p : mp) {
if (start > 0) re += p.f - prev;
prev = p.f;
start += p.s;
}
cout<<(long long int)re<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
11 ms |
1916 KB |
Output is correct |
3 |
Correct |
10 ms |
1916 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
10 ms |
1916 KB |
Output is correct |
17 |
Correct |
113 ms |
11360 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
779 ms |
73252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
995 ms |
83356 KB |
Output is correct |
3 |
Correct |
998 ms |
83484 KB |
Output is correct |
4 |
Correct |
991 ms |
83228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
995 ms |
83356 KB |
Output is correct |
3 |
Correct |
998 ms |
83484 KB |
Output is correct |
4 |
Correct |
991 ms |
83228 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
982 ms |
83412 KB |
Output is correct |
7 |
Correct |
965 ms |
83532 KB |
Output is correct |
8 |
Correct |
883 ms |
83996 KB |
Output is correct |
9 |
Correct |
1154 ms |
83952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
995 ms |
83356 KB |
Output is correct |
3 |
Correct |
998 ms |
83484 KB |
Output is correct |
4 |
Correct |
991 ms |
83228 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
91 ms |
11412 KB |
Output is correct |
7 |
Correct |
97 ms |
11360 KB |
Output is correct |
8 |
Correct |
91 ms |
11360 KB |
Output is correct |
9 |
Correct |
94 ms |
11360 KB |
Output is correct |
10 |
Correct |
96 ms |
11360 KB |
Output is correct |
11 |
Correct |
94 ms |
11356 KB |
Output is correct |
12 |
Correct |
97 ms |
11364 KB |
Output is correct |
13 |
Correct |
112 ms |
11364 KB |
Output is correct |
14 |
Correct |
90 ms |
11364 KB |
Output is correct |
15 |
Correct |
119 ms |
11368 KB |
Output is correct |
16 |
Correct |
114 ms |
11488 KB |
Output is correct |
17 |
Correct |
110 ms |
11360 KB |
Output is correct |
18 |
Correct |
1022 ms |
74732 KB |
Output is correct |
19 |
Correct |
1070 ms |
77728 KB |
Output is correct |
20 |
Correct |
1286 ms |
77852 KB |
Output is correct |
21 |
Correct |
112 ms |
11360 KB |
Output is correct |
22 |
Correct |
88 ms |
11360 KB |
Output is correct |
23 |
Correct |
306 ms |
43200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
109 ms |
11396 KB |
Output is correct |
3 |
Correct |
115 ms |
11360 KB |
Output is correct |
4 |
Correct |
1348 ms |
81024 KB |
Output is correct |
5 |
Correct |
120 ms |
11360 KB |
Output is correct |
6 |
Correct |
116 ms |
11360 KB |
Output is correct |
7 |
Correct |
118 ms |
11412 KB |
Output is correct |
8 |
Correct |
122 ms |
11364 KB |
Output is correct |
9 |
Correct |
113 ms |
11356 KB |
Output is correct |
10 |
Correct |
116 ms |
11356 KB |
Output is correct |
11 |
Correct |
103 ms |
11364 KB |
Output is correct |
12 |
Correct |
90 ms |
11360 KB |
Output is correct |
13 |
Correct |
114 ms |
11364 KB |
Output is correct |
14 |
Correct |
1261 ms |
80856 KB |
Output is correct |
15 |
Correct |
119 ms |
11404 KB |
Output is correct |
16 |
Correct |
946 ms |
77084 KB |
Output is correct |
17 |
Correct |
1020 ms |
77088 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
11 ms |
1916 KB |
Output is correct |
3 |
Correct |
10 ms |
1916 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
10 ms |
1916 KB |
Output is correct |
17 |
Correct |
113 ms |
11360 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
0 ms |
384 KB |
Output is correct |
23 |
Correct |
0 ms |
384 KB |
Output is correct |
24 |
Correct |
0 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
512 KB |
Output is correct |
26 |
Correct |
1 ms |
512 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
28 |
Correct |
779 ms |
73252 KB |
Output is correct |
29 |
Correct |
0 ms |
384 KB |
Output is correct |
30 |
Correct |
995 ms |
83356 KB |
Output is correct |
31 |
Correct |
998 ms |
83484 KB |
Output is correct |
32 |
Correct |
991 ms |
83228 KB |
Output is correct |
33 |
Correct |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
982 ms |
83412 KB |
Output is correct |
35 |
Correct |
965 ms |
83532 KB |
Output is correct |
36 |
Correct |
883 ms |
83996 KB |
Output is correct |
37 |
Correct |
1154 ms |
83952 KB |
Output is correct |
38 |
Correct |
0 ms |
384 KB |
Output is correct |
39 |
Correct |
91 ms |
11412 KB |
Output is correct |
40 |
Correct |
97 ms |
11360 KB |
Output is correct |
41 |
Correct |
91 ms |
11360 KB |
Output is correct |
42 |
Correct |
94 ms |
11360 KB |
Output is correct |
43 |
Correct |
96 ms |
11360 KB |
Output is correct |
44 |
Correct |
94 ms |
11356 KB |
Output is correct |
45 |
Correct |
97 ms |
11364 KB |
Output is correct |
46 |
Correct |
112 ms |
11364 KB |
Output is correct |
47 |
Correct |
90 ms |
11364 KB |
Output is correct |
48 |
Correct |
119 ms |
11368 KB |
Output is correct |
49 |
Correct |
114 ms |
11488 KB |
Output is correct |
50 |
Correct |
110 ms |
11360 KB |
Output is correct |
51 |
Correct |
1022 ms |
74732 KB |
Output is correct |
52 |
Correct |
1070 ms |
77728 KB |
Output is correct |
53 |
Correct |
1286 ms |
77852 KB |
Output is correct |
54 |
Correct |
112 ms |
11360 KB |
Output is correct |
55 |
Correct |
88 ms |
11360 KB |
Output is correct |
56 |
Correct |
306 ms |
43200 KB |
Output is correct |
57 |
Correct |
0 ms |
384 KB |
Output is correct |
58 |
Correct |
109 ms |
11396 KB |
Output is correct |
59 |
Correct |
115 ms |
11360 KB |
Output is correct |
60 |
Correct |
1348 ms |
81024 KB |
Output is correct |
61 |
Correct |
120 ms |
11360 KB |
Output is correct |
62 |
Correct |
116 ms |
11360 KB |
Output is correct |
63 |
Correct |
118 ms |
11412 KB |
Output is correct |
64 |
Correct |
122 ms |
11364 KB |
Output is correct |
65 |
Correct |
113 ms |
11356 KB |
Output is correct |
66 |
Correct |
116 ms |
11356 KB |
Output is correct |
67 |
Correct |
103 ms |
11364 KB |
Output is correct |
68 |
Correct |
90 ms |
11360 KB |
Output is correct |
69 |
Correct |
114 ms |
11364 KB |
Output is correct |
70 |
Correct |
1261 ms |
80856 KB |
Output is correct |
71 |
Correct |
119 ms |
11404 KB |
Output is correct |
72 |
Correct |
946 ms |
77084 KB |
Output is correct |
73 |
Correct |
1020 ms |
77088 KB |
Output is correct |
74 |
Correct |
0 ms |
384 KB |
Output is correct |
75 |
Correct |
0 ms |
384 KB |
Output is correct |
76 |
Correct |
0 ms |
384 KB |
Output is correct |
77 |
Correct |
0 ms |
384 KB |
Output is correct |
78 |
Correct |
0 ms |
384 KB |
Output is correct |
79 |
Correct |
10 ms |
1916 KB |
Output is correct |
80 |
Correct |
1316 ms |
76956 KB |
Output is correct |
81 |
Correct |
1317 ms |
76956 KB |
Output is correct |
82 |
Correct |
1297 ms |
76956 KB |
Output is correct |
83 |
Correct |
1296 ms |
77084 KB |
Output is correct |
84 |
Correct |
1290 ms |
77012 KB |
Output is correct |
85 |
Correct |
1328 ms |
77084 KB |
Output is correct |
86 |
Correct |
306 ms |
43200 KB |
Output is correct |
87 |
Correct |
0 ms |
384 KB |
Output is correct |