#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=2e18+1,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 GCD(int x, int y)
{
if (y == 0) return x;
return GCD(y, x%y);
}
int li[mxn+10],ri[mxn+10];
int32_t main(){
fastio
int n;cin>>n;
int a,b;cin>>a>>b;
int k;
int sum=0;
for(int i=0;i<n;i++)cin>>li[i]>>ri[i],sum+=ri[i]-li[i]+1;
k=(a/GCD(a,b+1));
int c=k/inf;
if(c>b){
cout<<sum;
return 0;
}
else k=k*b;
mp[0]=0;
mp[k-1]=0;
//if(k<=0)assert(0);
for(int i=0;i<n;i++){
int l=li[i],r=ri[i];
l=(l%k);
r=(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;
}
sum=0;
int ans=0,last=-1;
for(auto i:mp){
//if(i.f>k)break;
sum+=i.s;
if(sum>0)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%(k)
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 |
3676 KB |
Output is correct |
3 |
Correct |
6 ms |
3676 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2548 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
2 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
6 ms |
3676 KB |
Output is correct |
17 |
Correct |
58 ms |
12932 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2392 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 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 |
190 ms |
15952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
615 ms |
78544 KB |
Output is correct |
3 |
Correct |
862 ms |
141396 KB |
Output is correct |
4 |
Correct |
987 ms |
141316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
615 ms |
78544 KB |
Output is correct |
3 |
Correct |
862 ms |
141396 KB |
Output is correct |
4 |
Correct |
987 ms |
141316 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
811 ms |
141260 KB |
Output is correct |
7 |
Correct |
658 ms |
78724 KB |
Output is correct |
8 |
Correct |
832 ms |
141396 KB |
Output is correct |
9 |
Correct |
1112 ms |
141356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
615 ms |
78544 KB |
Output is correct |
3 |
Correct |
862 ms |
141396 KB |
Output is correct |
4 |
Correct |
987 ms |
141316 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
62 ms |
19316 KB |
Output is correct |
7 |
Correct |
65 ms |
19284 KB |
Output is correct |
8 |
Correct |
65 ms |
19216 KB |
Output is correct |
9 |
Correct |
80 ms |
19212 KB |
Output is correct |
10 |
Correct |
65 ms |
19332 KB |
Output is correct |
11 |
Correct |
62 ms |
19232 KB |
Output is correct |
12 |
Correct |
64 ms |
19324 KB |
Output is correct |
13 |
Correct |
72 ms |
19072 KB |
Output is correct |
14 |
Correct |
62 ms |
19280 KB |
Output is correct |
15 |
Correct |
73 ms |
19280 KB |
Output is correct |
16 |
Correct |
75 ms |
19332 KB |
Output is correct |
17 |
Correct |
70 ms |
19232 KB |
Output is correct |
18 |
Correct |
778 ms |
141384 KB |
Output is correct |
19 |
Correct |
966 ms |
141520 KB |
Output is correct |
20 |
Correct |
1060 ms |
141448 KB |
Output is correct |
21 |
Correct |
66 ms |
19280 KB |
Output is correct |
22 |
Correct |
68 ms |
19076 KB |
Output is correct |
23 |
Correct |
81 ms |
10876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
84 ms |
19320 KB |
Output is correct |
3 |
Correct |
82 ms |
19148 KB |
Output is correct |
4 |
Correct |
977 ms |
141648 KB |
Output is correct |
5 |
Correct |
81 ms |
19280 KB |
Output is correct |
6 |
Correct |
76 ms |
19284 KB |
Output is correct |
7 |
Correct |
82 ms |
19228 KB |
Output is correct |
8 |
Correct |
77 ms |
19280 KB |
Output is correct |
9 |
Correct |
71 ms |
19280 KB |
Output is correct |
10 |
Correct |
71 ms |
19208 KB |
Output is correct |
11 |
Correct |
72 ms |
19284 KB |
Output is correct |
12 |
Correct |
68 ms |
19280 KB |
Output is correct |
13 |
Correct |
74 ms |
19328 KB |
Output is correct |
14 |
Correct |
1200 ms |
141236 KB |
Output is correct |
15 |
Correct |
69 ms |
19280 KB |
Output is correct |
16 |
Correct |
771 ms |
141188 KB |
Output is correct |
17 |
Correct |
803 ms |
141176 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 |
7 ms |
3676 KB |
Output is correct |
3 |
Correct |
6 ms |
3676 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2548 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
2 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
6 ms |
3676 KB |
Output is correct |
17 |
Correct |
58 ms |
12932 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Incorrect |
2 ms |
2392 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |