1. 程式人生 > >The Golden Age CodeForces - 813B (數學+列舉)

The Golden Age CodeForces - 813B (數學+列舉)

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 unlucky as 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].

 

思路:直接列舉a和b的值,因為2^60大於1E18,所以可以0~61的任意列舉。

把可以得出的數字加入到一個set中,最後從頭到尾遍歷找最大的區間。

注意:由於數字很大,稍微大一點就爆了longlong 所以判斷是否大於R的時候,用自帶函式pow,因為float/double的範圍很大,比LL大多了,所以不會炸精度,可以剔除掉過大的資料,

而加入到set的時候用快速冪來算那個數值,因為浮點數有精度誤差。還讀到了大佬的部落格是把除法改成乘法來防止爆longlong,受益匪淺。

 推薦一個:https://blog.csdn.net/qq_37129433/article/details/81667733

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gg(x) getInt(&x)
using namespace std;
typedef  long long ll;
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll x,y,l,r;
vector<ll> vc;
set<ll> v;
ll quick_pow(ll a,ll b)
{
    ll ans=1;
        while(b)
        {
                if(b&1) ans= ( ans * a);
                a=( a* a);
                b>>=1;
        }
        return ans;
}
int main()
{
    gbtb;
    cin>>x>>y>>l>>r;
    for(ll i=0ll;i<=63ll;i++)
    {
        if(pow(x,i)>r||pow(x,i)<=0)
        {
            break;
        }
        for(ll j=0ll;j<=63ll;j++)
        {
            long long k=1ll*quick_pow(x,i)+1ll*quick_pow(y,j);
//            if(quick_pow(x,i)>r||quick_pow(x,i)<=0)
//            {
//                break;
//            }
            if(pow(y,j)>r||pow(y,j)<=0)
            {
                break;
            }
            if(k<=0||k>r)
            {
                break;
            }else
            {
                if(k>=l&&k<=r)
                {
                    v.insert(k);
                }
            }
        }
    }
    set<ll> ::iterator it=v.begin();
    ll ans=0ll;
    ll last=l-1;
    ll now;
    while(it!=v.end())
    {
//        cout<<*it<<" ";
         now=*it;
        if(now-last-1>ans)
        {
            ans=now-last-1;
        }
        last=now;
        it++;
    }
    r-now;
    now=r+1;
    if(now-last-1>ans)
    {
        ans=now-last-1;
    }
    cout<<ans<<endl;
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}