# 【LG5444】[APIO2019]奇怪装置

## 题面

$1\leq n\leq 10^6,1\leq A,B\leq 10^{18},0\leq l_i\leq r_i\leq 10^{18}$

## 题解

$t_1<t_2$所对应的二元组完全相同，那么

$\begin{cases} t_1+\lfloor\frac{t_1}{B} \rfloor \equiv t_2 + \lfloor \frac{t_2}{B} \rfloor(\bmod A)\\ t_1\equiv t_2(\bmod B) \end{cases}$

$t_1+\lfloor\frac{t_1}{B}\rfloor\equiv t_1+kB+k+\lfloor\frac{t_1}{B}\rfloor(\bmod A)$

$\therefore \frac{A}{gcd(A,B+1)}|k$，即$k$最小为$\frac{A}{gcd(A,B+1)}$

## 代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
inline ll gi() {
register ll data = 0, w = 1;
register char ch = 0;
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar();
return w * data;
}
const int MAX_N = 1e6 + 5;
ll N, A, B, l[MAX_N], r[MAX_N], T, ans;
multiset<pair<ll, int> > st;
void Add(ll l, ll r) { st.insert(make_pair(l, 1)), st.insert(make_pair(r + 1, -1)); }
int main () {
#ifndef ONLINE_JUDGE
freopen("cpp.in", "r", stdin);
#endif
N = gi(), A = gi(), B = gi(); ll d = __gcd(A, B + 1), sum = 0;
for (int i = 1; i <= N; i++) l[i] = gi(), r[i] = gi(), sum += r[i] - l[i] + 1;
if (1.0 * A / d * B > 1e18) return printf("%lld\n", sum) & 0;
T = A / d * B;
for (int i = 1; i <= N; i++) {
if (r[i] - l[i] + 1 >= T) return printf("%lld\n", T) & 0;
l[i] %= T, r[i] %= T;
}