題目鏈接:傳送門
取兩個不相等的整數,考慮什麼情況下它們的相同:
令,代入式子,大莉化簡一波,發現:
所以是一個週期。
於是問題轉化爲在區間內的條線段的交集大小。
按左端點排序一波,大莉求交集大小即可qwq。
要注意過大時,把它設成即可qwq。
代碼
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<math.h>
#define re register int
#define rl register ll
using namespace std;
typedef long long ll;
ll read() {
rl x=0,f=1;
char ch=getchar();
while(ch<'0' || ch>'9') {
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0' && ch<='9') {
x=10*x+ch-'0';
ch=getchar();
}
return x*f;
}
inline char GetChar() {
char ch=getchar();
while(ch!='t' && ch!='q') ch=getchar();
return ch;
}
const int Size=2000005;
ll n,A,B;
struct Segment {
ll l,r;
} w[Size];
inline bool comp(Segment x,Segment y) {
if(x.l!=y.l) return x.l<y.l;
return x.r<y.r;
}
int main() {
n=read();
A=read();
B=read();
ll len=A/__gcd(A,B+1);
if(2e18/B>=len) {
len=len*B;
} else {
len=2e18;
}
int tot=0;
for(re i=1; i<=n; i++) {
ll l=read();
ll r=read();
ll tmp=r/len-l/len;
if(tmp>1) {
printf("%lld",len);
return 0;
} else if(tmp==1) {
w[++tot].l=l%len; w[tot].r=len-1;
w[++tot].l=0; w[tot].r=r%len;
} else {
w[++tot].l=l%len;
w[tot].r=r%len;
}
}
sort(w+1,w+1+tot,comp);
ll maxr=-1,ans=0;
for(re i=1; i<=tot; i++) {
if(w[i].l<=maxr) {
if(w[i].r<=maxr) continue;
ans+=w[i].r-maxr;
maxr=w[i].r;
} else {
ans+=w[i].r-w[i].l+1;
maxr=w[i].r;
}
}
printf("%lld",ans);
return 0;
}