#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define vll vector<pll>
#define FOR(i,N) for(i=0;i<(N);++i)
#define FORe(i,N) for(i=1;i<=(N);++i)
#define FORr(i,a,b) for(i=(a);i<(b);++i)
#define FORrev(i,N) for(i=(N);i>=0;--i)
#define F0R(i,N) for(int i=0;i<(N);++i)
#define F0Re(i,N) for(int i=1;i<=(N);++i)
#define F0Rr(i,a,b) for(ll i=(a);i<(b);++i)
#define F0Rrev(i,N) for(int i=(N);i>=0;--i)
#define all(v) (v).begin(),(v).end()
#define dbgLine cerr<<" LINE : "<<__LINE__<<"\n"
#define ldd long double
using namespace std;
const int Alp = 26;
const int __PRECISION = 9;
const int inf = 1e9 + 8;
const ldd PI = acos(-1);
const ldd EPS = 1e-7;
const ll MOD = 1e9 + 7;
const ll MAXN = 2e5 + 5;
const ll ROOTN = 320;
const ll LOGN = 18;
const ll INF = 1e18 + 1022;
ll n, A, B, l, r, sm, mxDiff, T;
vll V, ranges;
ll gcd(ll a, ll b)
{
if(a > b)
swap(a, b);
if(b % a == 0)
return a;
return gcd(b%a, a);
}
inline void check_period()
{
ll tmp = gcd(A, B+1);
if(log2(A*1.0) + log(B*1.0) - log(1.0 * tmp) > 59.99)
{
cout<<sm<<'\n';
exit(0);
}
T = B * (A / tmp);
if(mxDiff >= T)
{
cout<<T<<'\n';
exit(0);
}
}
signed main()
{
/*
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
*/
sm = mxDiff = 0;
cin>>n>>A>>B;
F0R(i, n)
{
cin>>l>>r;
sm += r - l + 1;
mxDiff = max(mxDiff, r-l+1);
ranges.pb(mp(l, r));
}
check_period();
sm = 0;
F0R(i, n)
{
l = ranges[i].ff % T, r = ranges[i].ss % T;
V.pb(mp(l, 0));
V.pb(mp(r, 1));
if(l > r)
V.pb(mp(T-1, 1)), V.pb(mp(0, 0));
}
// cerr<<"period = "<<T<<'\n';
sort(all(V));
// for(pll x : V)
// cout<<"("<<x.ff<<','<<x.ss<<")\n";
ll lst = -1, act = 0;
for(pll pp : V)
{
if(act == 0 and pp.ss == 0)
++sm;
if(act > 0)
sm += pp.ff - lst;
if(pp.ss == 0)
++act;
else if(pp.ss == 1)
--act;
lst = pp.ff;
}
cout<<sm<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
34 ms |
1524 KB |
Output is correct |
3 |
Correct |
33 ms |
1524 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
288 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
1 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
32 ms |
1528 KB |
Output is correct |
17 |
Correct |
324 ms |
9832 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
2251 ms |
72664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
3167 ms |
65880 KB |
Output is correct |
3 |
Correct |
3178 ms |
61496 KB |
Output is correct |
4 |
Correct |
3158 ms |
55328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
3167 ms |
65880 KB |
Output is correct |
3 |
Correct |
3178 ms |
61496 KB |
Output is correct |
4 |
Correct |
3158 ms |
55328 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
3256 ms |
55400 KB |
Output is correct |
7 |
Correct |
3167 ms |
55568 KB |
Output is correct |
8 |
Correct |
3147 ms |
55812 KB |
Output is correct |
9 |
Correct |
3409 ms |
55296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
3167 ms |
65880 KB |
Output is correct |
3 |
Correct |
3178 ms |
61496 KB |
Output is correct |
4 |
Correct |
3158 ms |
55328 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
317 ms |
9848 KB |
Output is correct |
7 |
Correct |
341 ms |
9956 KB |
Output is correct |
8 |
Correct |
330 ms |
9956 KB |
Output is correct |
9 |
Correct |
329 ms |
9832 KB |
Output is correct |
10 |
Correct |
314 ms |
9828 KB |
Output is correct |
11 |
Correct |
320 ms |
9828 KB |
Output is correct |
12 |
Correct |
316 ms |
9828 KB |
Output is correct |
13 |
Correct |
325 ms |
9956 KB |
Output is correct |
14 |
Correct |
320 ms |
9852 KB |
Output is correct |
15 |
Correct |
329 ms |
9832 KB |
Output is correct |
16 |
Correct |
329 ms |
9828 KB |
Output is correct |
17 |
Correct |
317 ms |
9828 KB |
Output is correct |
18 |
Correct |
3237 ms |
58668 KB |
Output is correct |
19 |
Correct |
3214 ms |
57776 KB |
Output is correct |
20 |
Correct |
3447 ms |
59052 KB |
Output is correct |
21 |
Correct |
334 ms |
9828 KB |
Output is correct |
22 |
Correct |
311 ms |
9968 KB |
Output is correct |
23 |
Correct |
1056 ms |
34768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
325 ms |
9996 KB |
Output is correct |
3 |
Correct |
321 ms |
9828 KB |
Output is correct |
4 |
Correct |
3365 ms |
58892 KB |
Output is correct |
5 |
Correct |
320 ms |
9960 KB |
Output is correct |
6 |
Correct |
322 ms |
9852 KB |
Output is correct |
7 |
Correct |
322 ms |
9828 KB |
Output is correct |
8 |
Correct |
321 ms |
9828 KB |
Output is correct |
9 |
Correct |
333 ms |
9828 KB |
Output is correct |
10 |
Correct |
332 ms |
10036 KB |
Output is correct |
11 |
Correct |
322 ms |
9828 KB |
Output is correct |
12 |
Correct |
316 ms |
9956 KB |
Output is correct |
13 |
Correct |
323 ms |
9960 KB |
Output is correct |
14 |
Correct |
3268 ms |
58784 KB |
Output is correct |
15 |
Correct |
337 ms |
9852 KB |
Output is correct |
16 |
Correct |
3166 ms |
50148 KB |
Output is correct |
17 |
Correct |
3168 ms |
60772 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
34 ms |
1524 KB |
Output is correct |
3 |
Correct |
33 ms |
1524 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
288 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
1 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
32 ms |
1528 KB |
Output is correct |
17 |
Correct |
324 ms |
9832 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
1 ms |
256 KB |
Output is correct |
21 |
Correct |
1 ms |
256 KB |
Output is correct |
22 |
Correct |
1 ms |
256 KB |
Output is correct |
23 |
Correct |
0 ms |
256 KB |
Output is correct |
24 |
Correct |
1 ms |
256 KB |
Output is correct |
25 |
Correct |
4 ms |
384 KB |
Output is correct |
26 |
Correct |
4 ms |
384 KB |
Output is correct |
27 |
Correct |
4 ms |
384 KB |
Output is correct |
28 |
Correct |
2251 ms |
72664 KB |
Output is correct |
29 |
Correct |
1 ms |
256 KB |
Output is correct |
30 |
Correct |
3167 ms |
65880 KB |
Output is correct |
31 |
Correct |
3178 ms |
61496 KB |
Output is correct |
32 |
Correct |
3158 ms |
55328 KB |
Output is correct |
33 |
Correct |
1 ms |
256 KB |
Output is correct |
34 |
Correct |
3256 ms |
55400 KB |
Output is correct |
35 |
Correct |
3167 ms |
55568 KB |
Output is correct |
36 |
Correct |
3147 ms |
55812 KB |
Output is correct |
37 |
Correct |
3409 ms |
55296 KB |
Output is correct |
38 |
Correct |
1 ms |
256 KB |
Output is correct |
39 |
Correct |
317 ms |
9848 KB |
Output is correct |
40 |
Correct |
341 ms |
9956 KB |
Output is correct |
41 |
Correct |
330 ms |
9956 KB |
Output is correct |
42 |
Correct |
329 ms |
9832 KB |
Output is correct |
43 |
Correct |
314 ms |
9828 KB |
Output is correct |
44 |
Correct |
320 ms |
9828 KB |
Output is correct |
45 |
Correct |
316 ms |
9828 KB |
Output is correct |
46 |
Correct |
325 ms |
9956 KB |
Output is correct |
47 |
Correct |
320 ms |
9852 KB |
Output is correct |
48 |
Correct |
329 ms |
9832 KB |
Output is correct |
49 |
Correct |
329 ms |
9828 KB |
Output is correct |
50 |
Correct |
317 ms |
9828 KB |
Output is correct |
51 |
Correct |
3237 ms |
58668 KB |
Output is correct |
52 |
Correct |
3214 ms |
57776 KB |
Output is correct |
53 |
Correct |
3447 ms |
59052 KB |
Output is correct |
54 |
Correct |
334 ms |
9828 KB |
Output is correct |
55 |
Correct |
311 ms |
9968 KB |
Output is correct |
56 |
Correct |
1056 ms |
34768 KB |
Output is correct |
57 |
Correct |
1 ms |
256 KB |
Output is correct |
58 |
Correct |
325 ms |
9996 KB |
Output is correct |
59 |
Correct |
321 ms |
9828 KB |
Output is correct |
60 |
Correct |
3365 ms |
58892 KB |
Output is correct |
61 |
Correct |
320 ms |
9960 KB |
Output is correct |
62 |
Correct |
322 ms |
9852 KB |
Output is correct |
63 |
Correct |
322 ms |
9828 KB |
Output is correct |
64 |
Correct |
321 ms |
9828 KB |
Output is correct |
65 |
Correct |
333 ms |
9828 KB |
Output is correct |
66 |
Correct |
332 ms |
10036 KB |
Output is correct |
67 |
Correct |
322 ms |
9828 KB |
Output is correct |
68 |
Correct |
316 ms |
9956 KB |
Output is correct |
69 |
Correct |
323 ms |
9960 KB |
Output is correct |
70 |
Correct |
3268 ms |
58784 KB |
Output is correct |
71 |
Correct |
337 ms |
9852 KB |
Output is correct |
72 |
Correct |
3166 ms |
50148 KB |
Output is correct |
73 |
Correct |
3168 ms |
60772 KB |
Output is correct |
74 |
Correct |
1 ms |
256 KB |
Output is correct |
75 |
Correct |
1 ms |
288 KB |
Output is correct |
76 |
Correct |
0 ms |
256 KB |
Output is correct |
77 |
Correct |
1 ms |
256 KB |
Output is correct |
78 |
Correct |
1 ms |
256 KB |
Output is correct |
79 |
Correct |
32 ms |
1524 KB |
Output is correct |
80 |
Correct |
3309 ms |
62240 KB |
Output is correct |
81 |
Correct |
3328 ms |
58844 KB |
Output is correct |
82 |
Correct |
3243 ms |
61372 KB |
Output is correct |
83 |
Correct |
3284 ms |
64892 KB |
Output is correct |
84 |
Correct |
3329 ms |
64800 KB |
Output is correct |
85 |
Correct |
3271 ms |
64656 KB |
Output is correct |
86 |
Correct |
1037 ms |
34924 KB |
Output is correct |
87 |
Correct |
1 ms |
384 KB |
Output is correct |