#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <bitset>
#include <random>
#include <stack>
#include <deque>
#include <chrono>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define REP(i, a, b) for (int i = int(a); i <= int(b); i++)
#define REPb(j, d, c) for (int j = int(d); j >= int(c); j--)
#define ff first
#define ss second
#define pb push_back
#define len(x) int((x).size())
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//ifstream cin("input.txt");
//ifstream cin("test.in");
//ofstream cout("test.out");
ll n,a,b;
cin>>n>>a>>b;
vector<pll> segs;
ll s=0;
REP(i,1,n)
{
ll l,r;
cin>>l>>r;
segs.pb({l,r});
s+=r-l+1;
}
// if(s<=1e6 && false) // REMOVE THIS!!!
// {
// // brute force
// vector<pll> pairs;
// for(auto seg:segs)
// {
// for(ll i=seg.ff; i<=seg.ss; i++)
// {
// pairs.pb({(i+ll(i/b))%a,i%b});
// }
// }
// sort(pairs.begin(),pairs.end());
// ll ans=1;
// REP(i,1,len(pairs)-1)
// {
// if(pairs[i]!=pairs[i-1]) ans++;
// }
// cout << ans << endl;
// return 0;
// }
ll l=segs[0].ff;
ll r=segs[0].ss;
ll k = a/__gcd(a,b+1);
// cout << k << endl;
// we want to know when kB >= 1e18
if(log(k) + log(b) >= 18*log(10))
{
cout << s << endl;
return 0;
}
else
{
k*=b;
vector<pll> intervals;
for(auto seg:segs)
{
if(seg.ss-seg.ff+1 >= k)
{
cout << k << endl;
return 0;
}
if(seg.ff%k > seg.ss%k)
{
intervals.pb({0,seg.ss%k});
intervals.pb({seg.ff%k,k-1});
}
else
{
intervals.pb({seg.ff%k,seg.ss%k});
}
}
sort(intervals.begin(),intervals.end());
ll cur_start=intervals[0].ff;
ll cur_end=intervals[0].ss;
ll ans=0;
REP(i,1,len(intervals)-1)
{
if(intervals[i].ff > cur_end+1)
{
ans += cur_end-cur_start+1;
cur_start=intervals[i].ff;
cur_end=intervals[i].ss;
}
else
{
cur_end=max(cur_end,intervals[i].ss);
}
}
ans += cur_end-cur_start+1;
cout << ans << endl;
}
}
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:82:5: warning: unused variable 'l' [-Wunused-variable]
ll l=segs[0].ff;
^
strange_device.cpp:83:5: warning: unused variable 'r' [-Wunused-variable]
ll r=segs[0].ss;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
9 ms |
1308 KB |
Output is correct |
3 |
Correct |
9 ms |
1272 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
380 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
9 ms |
1272 KB |
Output is correct |
17 |
Correct |
72 ms |
7272 KB |
Output is correct |
18 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
380 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
483 ms |
42228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
645 ms |
43396 KB |
Output is correct |
3 |
Correct |
639 ms |
42188 KB |
Output is correct |
4 |
Correct |
638 ms |
41696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
645 ms |
43396 KB |
Output is correct |
3 |
Correct |
639 ms |
42188 KB |
Output is correct |
4 |
Correct |
638 ms |
41696 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
639 ms |
41668 KB |
Output is correct |
7 |
Correct |
640 ms |
41592 KB |
Output is correct |
8 |
Correct |
643 ms |
41668 KB |
Output is correct |
9 |
Correct |
701 ms |
41492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
645 ms |
43396 KB |
Output is correct |
3 |
Correct |
639 ms |
42188 KB |
Output is correct |
4 |
Correct |
638 ms |
41696 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
64 ms |
5848 KB |
Output is correct |
7 |
Correct |
69 ms |
5940 KB |
Output is correct |
8 |
Correct |
66 ms |
5896 KB |
Output is correct |
9 |
Correct |
68 ms |
5868 KB |
Output is correct |
10 |
Correct |
65 ms |
5708 KB |
Output is correct |
11 |
Correct |
67 ms |
5624 KB |
Output is correct |
12 |
Correct |
64 ms |
5608 KB |
Output is correct |
13 |
Correct |
72 ms |
5764 KB |
Output is correct |
14 |
Correct |
65 ms |
5608 KB |
Output is correct |
15 |
Correct |
71 ms |
5632 KB |
Output is correct |
16 |
Correct |
70 ms |
5644 KB |
Output is correct |
17 |
Correct |
67 ms |
5608 KB |
Output is correct |
18 |
Correct |
646 ms |
41280 KB |
Output is correct |
19 |
Correct |
634 ms |
41076 KB |
Output is correct |
20 |
Correct |
705 ms |
40912 KB |
Output is correct |
21 |
Correct |
70 ms |
5096 KB |
Output is correct |
22 |
Correct |
63 ms |
5096 KB |
Output is correct |
23 |
Correct |
210 ms |
18100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
70 ms |
4972 KB |
Output is correct |
3 |
Correct |
69 ms |
5020 KB |
Output is correct |
4 |
Correct |
719 ms |
40584 KB |
Output is correct |
5 |
Correct |
71 ms |
6544 KB |
Output is correct |
6 |
Correct |
73 ms |
6464 KB |
Output is correct |
7 |
Correct |
71 ms |
6548 KB |
Output is correct |
8 |
Correct |
73 ms |
6464 KB |
Output is correct |
9 |
Correct |
69 ms |
6252 KB |
Output is correct |
10 |
Correct |
70 ms |
6260 KB |
Output is correct |
11 |
Correct |
71 ms |
6504 KB |
Output is correct |
12 |
Correct |
64 ms |
6636 KB |
Output is correct |
13 |
Correct |
71 ms |
6348 KB |
Output is correct |
14 |
Correct |
692 ms |
41740 KB |
Output is correct |
15 |
Correct |
69 ms |
6396 KB |
Output is correct |
16 |
Correct |
635 ms |
42040 KB |
Output is correct |
17 |
Correct |
646 ms |
42060 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
9 ms |
1308 KB |
Output is correct |
3 |
Correct |
9 ms |
1272 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
380 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
9 ms |
1272 KB |
Output is correct |
17 |
Correct |
72 ms |
7272 KB |
Output is correct |
18 |
Correct |
2 ms |
256 KB |
Output is correct |
19 |
Correct |
2 ms |
256 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
380 KB |
Output is correct |
26 |
Correct |
3 ms |
376 KB |
Output is correct |
27 |
Correct |
3 ms |
504 KB |
Output is correct |
28 |
Correct |
483 ms |
42228 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
645 ms |
43396 KB |
Output is correct |
31 |
Correct |
639 ms |
42188 KB |
Output is correct |
32 |
Correct |
638 ms |
41696 KB |
Output is correct |
33 |
Correct |
2 ms |
376 KB |
Output is correct |
34 |
Correct |
639 ms |
41668 KB |
Output is correct |
35 |
Correct |
640 ms |
41592 KB |
Output is correct |
36 |
Correct |
643 ms |
41668 KB |
Output is correct |
37 |
Correct |
701 ms |
41492 KB |
Output is correct |
38 |
Correct |
2 ms |
256 KB |
Output is correct |
39 |
Correct |
64 ms |
5848 KB |
Output is correct |
40 |
Correct |
69 ms |
5940 KB |
Output is correct |
41 |
Correct |
66 ms |
5896 KB |
Output is correct |
42 |
Correct |
68 ms |
5868 KB |
Output is correct |
43 |
Correct |
65 ms |
5708 KB |
Output is correct |
44 |
Correct |
67 ms |
5624 KB |
Output is correct |
45 |
Correct |
64 ms |
5608 KB |
Output is correct |
46 |
Correct |
72 ms |
5764 KB |
Output is correct |
47 |
Correct |
65 ms |
5608 KB |
Output is correct |
48 |
Correct |
71 ms |
5632 KB |
Output is correct |
49 |
Correct |
70 ms |
5644 KB |
Output is correct |
50 |
Correct |
67 ms |
5608 KB |
Output is correct |
51 |
Correct |
646 ms |
41280 KB |
Output is correct |
52 |
Correct |
634 ms |
41076 KB |
Output is correct |
53 |
Correct |
705 ms |
40912 KB |
Output is correct |
54 |
Correct |
70 ms |
5096 KB |
Output is correct |
55 |
Correct |
63 ms |
5096 KB |
Output is correct |
56 |
Correct |
210 ms |
18100 KB |
Output is correct |
57 |
Correct |
2 ms |
376 KB |
Output is correct |
58 |
Correct |
70 ms |
4972 KB |
Output is correct |
59 |
Correct |
69 ms |
5020 KB |
Output is correct |
60 |
Correct |
719 ms |
40584 KB |
Output is correct |
61 |
Correct |
71 ms |
6544 KB |
Output is correct |
62 |
Correct |
73 ms |
6464 KB |
Output is correct |
63 |
Correct |
71 ms |
6548 KB |
Output is correct |
64 |
Correct |
73 ms |
6464 KB |
Output is correct |
65 |
Correct |
69 ms |
6252 KB |
Output is correct |
66 |
Correct |
70 ms |
6260 KB |
Output is correct |
67 |
Correct |
71 ms |
6504 KB |
Output is correct |
68 |
Correct |
64 ms |
6636 KB |
Output is correct |
69 |
Correct |
71 ms |
6348 KB |
Output is correct |
70 |
Correct |
692 ms |
41740 KB |
Output is correct |
71 |
Correct |
69 ms |
6396 KB |
Output is correct |
72 |
Correct |
635 ms |
42040 KB |
Output is correct |
73 |
Correct |
646 ms |
42060 KB |
Output is correct |
74 |
Correct |
2 ms |
376 KB |
Output is correct |
75 |
Correct |
2 ms |
376 KB |
Output is correct |
76 |
Correct |
2 ms |
376 KB |
Output is correct |
77 |
Correct |
2 ms |
376 KB |
Output is correct |
78 |
Correct |
2 ms |
376 KB |
Output is correct |
79 |
Correct |
8 ms |
1268 KB |
Output is correct |
80 |
Correct |
718 ms |
41988 KB |
Output is correct |
81 |
Correct |
724 ms |
41924 KB |
Output is correct |
82 |
Correct |
698 ms |
42116 KB |
Output is correct |
83 |
Correct |
688 ms |
42052 KB |
Output is correct |
84 |
Correct |
693 ms |
42096 KB |
Output is correct |
85 |
Correct |
690 ms |
41992 KB |
Output is correct |
86 |
Correct |
207 ms |
20604 KB |
Output is correct |
87 |
Correct |
2 ms |
376 KB |
Output is correct |