1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| #include<bits/stdc++.h> using namespace std;
#define int long long
void read(int &x) { x=0;int f=1;char ch=getchar(); for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f; for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f; }
void print(int x) { if(x<0) putchar('-'),x=-x; if(!x) return ;print(x/10),putchar(x%10+48); } void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}
#define lf double #define ll long long
#define pii pair<int,int > #define vec vector<int >
#define pb push_back #define mp make_pair #define fr first #define sc second
#define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++)
const int maxn = 1e6+10; const int inf = 1e9; const lf eps = 1e-8; const int mod = 1e9+7;
int n,a,b,sum,cnt; struct info {int l,r;}t[maxn];
signed main() { read(n),read(a),read(b);int g=__gcd(a,b+1); int r=a*b/g; if(1.00*a*b/g>1e18) r=1e18; for(int i=1;i<=n;i++) { int x,y;read(x),read(y),sum+=y-x+1; if(y-x+1>=r) t[++cnt]=(info){0,r-1}; else if(y%r>=x%r) t[++cnt]=(info){x%r,y%r}; else t[++cnt]=(info){0,y%r},t[++cnt]=(info){x%r,r-1}; } sort(t+1,t+cnt+1,[](info a,info b) {return a.l<b.l;}); int sum=0,mx=-1; for(int i=1;i<=cnt;i++) sum+=max(0ll,t[i].r-max(t[i].l-1,mx)),mx=max(mx,t[i].r); write(sum); return 0; }
|