1. 对于数据的处理上,while (a.back() <= r / x) a.push_back(a.back() * x),防止越界。
2. 做好预处理,把特殊的l,r节点常规化处理,减少逻辑上的复杂度。
2. 做好预处理,把特殊的l,r节点常规化处理,减少逻辑上的复杂度。
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cctype>
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define LL long long
#define mod 1000000007
#define INF 1000000000
#define eps 1e-5
#define PI 3.1415926535898
using namespace std;
//-------------------------CHC------------------------------//
int main() {
LL x, y, l, r;
while (cin >> x >> y >> l >> r) {
vector<LL> a{ 1 }, b{ 1 }, v;
while (a.back() <= r / x) a.push_back(a.back() * x);
while (b.back() <= r / y) b.push_back(b.back() * y);
for (int i = 0; i < a.size(); ++i)
for (int j = 0; j < b.size(); ++j)
v.push_back(a[i] + b[j]);
v.push_back(l - 1);
v.push_back(r + 1);
sort(v.begin(), v.end());
LL ans = 0;
int i = 0;
while (i < v.size() && v[i] < l - 1) ++i; ++i;
while (i < v.size() && v[i] <= r + 1)
ans = max(ans, v[i] - v[i - 1] - 1), ++i;
cout << ans << endl;
}
return 0;
}