Educational Codeforces Round 22 B. The Golden Age【Beware of explosion-proof LL】

B. 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 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
Copy
2 3 1 10
output
Copy
1
input
Copy
3 5 10 22
output
Copy
8
input
Copy
2 3 3 5
output
Copy
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].



题意:给L,R , 问【L,R】中最长的不含bad number 数的连续区间最长是多少

思路:枚举所有的n,因为是幂次,2^60≈1e18。 复杂度也就在3600不到的样子。有一点要注意的是枚举可能会爆LONG LONG,看了dalao们1A的代码。发现都有防爆LONG LONG的意识。


#include<bits/stdc++.h>
#define PI acos(-1.0)
#define pb push_back
using namespace std;
typedef long long ll;

const int N=1e5+5;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;

ll x,y,l,r;
set<ll> st;
vector<ll> te;
ll mypow(ll x,int b){
    ll ans=1;
    while(b){
        if(b&1) ans*=x;
        b/=2;
        x*=x;
    }
    return ans;
}

int main(void){
    cin >>x>>y>>l>>r;
    for(int a=0;a<=100;a++){
        ll t1=mypow(x,a);
        if(t1>r) break;
        for(int b=0;b<=100;++b){
            ll t2=mypow(y,b);
            if(t2>r) break;
            ll n=mypow(x,a)+mypow(y,b);
            if(n>r) break;
            else if(n>=l)  st.insert(n);
//            cout<<"r="<<r<<" n="<<n<<endl;
            if(1e18/y<t2)   break;
        }
        if(1e18/x<t1)   break;
    }
//    st.insert(l);
//    st.insert(r);
    for(auto t: st) te.pb(t);
    ll mx=0;
    for(int i=0;i<(int)te.size()-1;++i)
        mx = max (te [i + 1] -te [i] -1, mx);
    if(te.size()>0){
        if(st.find(r)==st.end())    mx=max(mx,r-*te.rbegin());
        if(st.find(l)==st.end())    mx=max(mx,te[0]-l);
    }
    else{
        mx=r-l+1;
    }
    cout << mx << endl;

    return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325814021&siteId=291194637