codeforces round 22-B.the golden age

xiaoxiao2021-02-28  96

The Golden Age time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

Unlucky year in Berland is such a year that its number n can be represented as n = xa + yb, where a and b are non-negative integer numbers.

For example, if x = 2 and y = 3 then the years 4 and 17 are unlucky (4 = 20 + 3117 = 23 + 32 = 24 + 30) and year 18 isn't unluckyas there is no such representation for it.

Such interval of years that there are no unlucky years in it is called The Golden Age.

You should write a program which will find maximum length of The Golden Age which starts no earlier than the year l and ends no later than the year r. If all years in the interval [l, r] are unlucky then the answer is 0.

Input

The first line contains four integer numbers xyl and r (2 ≤ x, y ≤ 10181 ≤ l ≤ r ≤ 1018).

Output

Print the maximum length of The Golden Age within the interval [l, r].

If all years in the interval [l, r] are unlucky then print 0.

Examples input 2 3 1 10 output 1 input 3 5 10 22 output 8 input 2 3 3 5 output 0 Note

In the first example the unlucky years are 2, 3, 4, 5, 7, 9 and 10. So maximum length of The Golden Age is achived in the intervals [1, 1][6, 6] and [8, 8].

In the second example the longest Golden Age is the interval [15, 22].

分别对r以内x的a次方和y的b次方进行枚举,构造两个数组,100即可满足。然后枚举两个数组的和,统计在[l,r]范围内的数,在求最大值。

#include<cstdio> #include<iostream> #include<algorithm> #define maxn 10010 #define maxm 100000 #define ll long long using namespace std; ll a[maxn]; ll b[maxn]; ll c[maxm]; int main() { int la, lb; ll x, y, l, r; cin >> x >> y >> l >> r; ll t = r; la = lb = 1; a[0] = b[0] = 1; while (1) { ll temp = t / x; if (temp >0) { a[la] = a[la-1]*x; t = t / x; la++; } else break; } t = r; while (1) { ll temp = t / y; if (temp > 0) { b[lb] = b[lb-1]*y; t = t / y; lb++; } else break; } int k = 0; for (int i = 0;i < la;i++) { for (int j = 0;j < lb;j++) { if ((a[i] + b[j]) >= l&&(a[i] + b[j]) <= r) { c[k] = a[i] + b[j]; k++; } } } ll ans=0; sort(c, c + k); if (k>0) { if (c[0] - l>ans) ans = c[0] - l; if (r - c[k - 1]>ans) ans = r - c[k - 1]; for (int i = 1;i < k;i++) ans = max(ans, c[i] - c[i - 1] - 1); } else ans = r - l + 1; cout << ans << endl; return 0; }

转载请注明原文地址: https://www.6miu.com/read-58563.html

最新回复(0)