码迷,mamicode.com
首页 > 其他好文 > 详细

The Golden Age CodeForces - 813B (数学+枚举)

时间:2019-01-11 22:14:10      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:ems   longest   函数   code   推荐一个   ble   blog   audio   contains   

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;
        }
    }
}

 

The Golden Age CodeForces - 813B (数学+枚举)

标签:ems   longest   函数   code   推荐一个   ble   blog   audio   contains   

原文地址:https://www.cnblogs.com/qieqiemin/p/10257353.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!