#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<cassert>
#include<limits.h>
#include<cmath>
#include<set>
#include<numeric> //gcd(a,b)
#include<algorithm>
#include<bitset>
#include<stack>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define pppiiii pair<pii,pii>
#define ppii pair<int,pii>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pb push_back
//#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
#define int long long
const int mod=1e9+7,mxn=1e6,lg=30,inf=1e18,minf=-1e18,Mxn=2e6,root=700;
void setIO(string name){
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
map<int,int>mp;
int li[mxn+10],ri[mxn+10];
int32_t main(){
fastio
int n;cin>>n;
int a,b;cin>>a>>b;
int k,sum=0;
for(int i=0;i<n;i++)cin>>li[i]>>ri[i],sum+=ri[i]-li[i]+1;
k=(gcd(a,b+1));
if((double)(ri[n-1]*1.0/(a*1.0/k))<=(double)(b))return cout<<sum,0;
k=a/k;
k=k*b;
bool yes=false;
mp[k-1]=0;
mp[0]=0;
for(int i=0;i<n;i++){
int l=li[i],r=ri[i];
if(r-l+1>=k)yes=1;
else{
l%=k,r%=k;
mp[l-1]+=0;
mp[l]+=1;
if(r+1<k)mp[r+1]-=1;
mp[r]+=0;
if(l>r)mp[0]+=1;
}
}
if(yes)return cout<<k<<'\n',0;
sum=0;
int ans=0,last=-1;
for(auto i:mp){
sum+=i.s;
if(sum)ans+=i.f-last;
last=i.f;
}
cout<<ans;
/*
observation:
y=t mod b
x=(t+(k*b)+[t/b]+k)%a
x=t+t[t/b]%a
(k*(b+1))%a==0
case 1 : (b+1)%a==0 -> k can be anything -> if y equal is equal ->k==1
case 2 : k%a==0 -> k=g(a) -> k is a multiple of a ->val jump every k*b
*|k=lcm(a,b+1)/(b+1)|*
considering 1 range l,r
first occurance of y=t mod b
if r-l+1>k then it will cover all k
block of b (range where all y value will be distinct)
| | | |
if(k==2)
block will be (range where all (x,y) value will be distinct)
| | |
find ways to count x%(a*b) or x%b
in a given set of range l,r
l,r will cover a range of block
can use prefix sum??
*/
}
Compilation message
strange_device.cpp: In function 'void setIO(std::string)':
strange_device.cpp:37:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen((name+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
strange_device.cpp:38:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | freopen((name+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
7 ms |
3744 KB |
Output is correct |
3 |
Correct |
7 ms |
3676 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2508 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
6 ms |
3676 KB |
Output is correct |
17 |
Correct |
62 ms |
12924 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
199 ms |
35972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
647 ms |
108168 KB |
Output is correct |
3 |
Correct |
842 ms |
178312 KB |
Output is correct |
4 |
Correct |
913 ms |
178460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
647 ms |
108168 KB |
Output is correct |
3 |
Correct |
842 ms |
178312 KB |
Output is correct |
4 |
Correct |
913 ms |
178460 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
836 ms |
178572 KB |
Output is correct |
7 |
Correct |
694 ms |
115812 KB |
Output is correct |
8 |
Correct |
857 ms |
178280 KB |
Output is correct |
9 |
Correct |
1099 ms |
178512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
647 ms |
108168 KB |
Output is correct |
3 |
Correct |
842 ms |
178312 KB |
Output is correct |
4 |
Correct |
913 ms |
178460 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
66 ms |
22896 KB |
Output is correct |
7 |
Correct |
73 ms |
22868 KB |
Output is correct |
8 |
Correct |
68 ms |
22872 KB |
Output is correct |
9 |
Correct |
75 ms |
23016 KB |
Output is correct |
10 |
Correct |
65 ms |
23012 KB |
Output is correct |
11 |
Correct |
67 ms |
22880 KB |
Output is correct |
12 |
Correct |
73 ms |
23256 KB |
Output is correct |
13 |
Correct |
79 ms |
22852 KB |
Output is correct |
14 |
Correct |
67 ms |
22880 KB |
Output is correct |
15 |
Correct |
81 ms |
22972 KB |
Output is correct |
16 |
Correct |
89 ms |
22868 KB |
Output is correct |
17 |
Correct |
78 ms |
22860 KB |
Output is correct |
18 |
Correct |
831 ms |
178704 KB |
Output is correct |
19 |
Correct |
1018 ms |
178644 KB |
Output is correct |
20 |
Correct |
1036 ms |
178292 KB |
Output is correct |
21 |
Correct |
92 ms |
22984 KB |
Output is correct |
22 |
Correct |
75 ms |
22868 KB |
Output is correct |
23 |
Correct |
86 ms |
23268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
79 ms |
19232 KB |
Output is correct |
3 |
Correct |
90 ms |
19276 KB |
Output is correct |
4 |
Correct |
1172 ms |
170656 KB |
Output is correct |
5 |
Correct |
92 ms |
22812 KB |
Output is correct |
6 |
Correct |
97 ms |
22884 KB |
Output is correct |
7 |
Correct |
110 ms |
23028 KB |
Output is correct |
8 |
Correct |
97 ms |
22992 KB |
Output is correct |
9 |
Correct |
81 ms |
22988 KB |
Output is correct |
10 |
Correct |
78 ms |
22876 KB |
Output is correct |
11 |
Correct |
84 ms |
23264 KB |
Output is correct |
12 |
Correct |
79 ms |
23000 KB |
Output is correct |
13 |
Correct |
106 ms |
22864 KB |
Output is correct |
14 |
Correct |
1316 ms |
178692 KB |
Output is correct |
15 |
Correct |
82 ms |
23012 KB |
Output is correct |
16 |
Correct |
886 ms |
178380 KB |
Output is correct |
17 |
Correct |
835 ms |
178332 KB |
Output is correct |
18 |
Correct |
1 ms |
2648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
7 ms |
3744 KB |
Output is correct |
3 |
Correct |
7 ms |
3676 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2508 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
6 ms |
3676 KB |
Output is correct |
17 |
Correct |
62 ms |
12924 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2392 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2392 KB |
Output is correct |
25 |
Correct |
1 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
199 ms |
35972 KB |
Output is correct |
29 |
Correct |
1 ms |
2392 KB |
Output is correct |
30 |
Correct |
647 ms |
108168 KB |
Output is correct |
31 |
Correct |
842 ms |
178312 KB |
Output is correct |
32 |
Correct |
913 ms |
178460 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
836 ms |
178572 KB |
Output is correct |
35 |
Correct |
694 ms |
115812 KB |
Output is correct |
36 |
Correct |
857 ms |
178280 KB |
Output is correct |
37 |
Correct |
1099 ms |
178512 KB |
Output is correct |
38 |
Correct |
1 ms |
2392 KB |
Output is correct |
39 |
Correct |
66 ms |
22896 KB |
Output is correct |
40 |
Correct |
73 ms |
22868 KB |
Output is correct |
41 |
Correct |
68 ms |
22872 KB |
Output is correct |
42 |
Correct |
75 ms |
23016 KB |
Output is correct |
43 |
Correct |
65 ms |
23012 KB |
Output is correct |
44 |
Correct |
67 ms |
22880 KB |
Output is correct |
45 |
Correct |
73 ms |
23256 KB |
Output is correct |
46 |
Correct |
79 ms |
22852 KB |
Output is correct |
47 |
Correct |
67 ms |
22880 KB |
Output is correct |
48 |
Correct |
81 ms |
22972 KB |
Output is correct |
49 |
Correct |
89 ms |
22868 KB |
Output is correct |
50 |
Correct |
78 ms |
22860 KB |
Output is correct |
51 |
Correct |
831 ms |
178704 KB |
Output is correct |
52 |
Correct |
1018 ms |
178644 KB |
Output is correct |
53 |
Correct |
1036 ms |
178292 KB |
Output is correct |
54 |
Correct |
92 ms |
22984 KB |
Output is correct |
55 |
Correct |
75 ms |
22868 KB |
Output is correct |
56 |
Correct |
86 ms |
23268 KB |
Output is correct |
57 |
Correct |
0 ms |
2396 KB |
Output is correct |
58 |
Correct |
79 ms |
19232 KB |
Output is correct |
59 |
Correct |
90 ms |
19276 KB |
Output is correct |
60 |
Correct |
1172 ms |
170656 KB |
Output is correct |
61 |
Correct |
92 ms |
22812 KB |
Output is correct |
62 |
Correct |
97 ms |
22884 KB |
Output is correct |
63 |
Correct |
110 ms |
23028 KB |
Output is correct |
64 |
Correct |
97 ms |
22992 KB |
Output is correct |
65 |
Correct |
81 ms |
22988 KB |
Output is correct |
66 |
Correct |
78 ms |
22876 KB |
Output is correct |
67 |
Correct |
84 ms |
23264 KB |
Output is correct |
68 |
Correct |
79 ms |
23000 KB |
Output is correct |
69 |
Correct |
106 ms |
22864 KB |
Output is correct |
70 |
Correct |
1316 ms |
178692 KB |
Output is correct |
71 |
Correct |
82 ms |
23012 KB |
Output is correct |
72 |
Correct |
886 ms |
178380 KB |
Output is correct |
73 |
Correct |
835 ms |
178332 KB |
Output is correct |
74 |
Correct |
1 ms |
2648 KB |
Output is correct |
75 |
Correct |
1 ms |
2652 KB |
Output is correct |
76 |
Correct |
0 ms |
2396 KB |
Output is correct |
77 |
Correct |
1 ms |
2396 KB |
Output is correct |
78 |
Correct |
1 ms |
2392 KB |
Output is correct |
79 |
Correct |
7 ms |
4188 KB |
Output is correct |
80 |
Correct |
1000 ms |
178520 KB |
Output is correct |
81 |
Correct |
1072 ms |
178452 KB |
Output is correct |
82 |
Correct |
1303 ms |
178732 KB |
Output is correct |
83 |
Correct |
1286 ms |
178572 KB |
Output is correct |
84 |
Correct |
1184 ms |
178372 KB |
Output is correct |
85 |
Correct |
1029 ms |
178460 KB |
Output is correct |
86 |
Correct |
90 ms |
23404 KB |
Output is correct |
87 |
Correct |
1 ms |
2516 KB |
Output is correct |