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 + 31, 17 = 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 ≤ 1018, 1 ≤ 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=;
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=;
while(b)
{
if(b&) ans= ( ans * a);
a=( a* a);
b>>=;
}
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)<=)
{
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)<=)
{
break;
}
if(k<=||k>r)
{
break;
}else
{
if(k>=l&&k<=r)
{
v.insert(k);
}
}
}
}
set<ll> ::iterator it=v.begin();
ll ans=0ll;
ll last=l-;
ll now;
while(it!=v.end())
{
// cout<<*it<<" ";
now=*it;
if(now-last->ans)
{
ans=now-last-;
}
last=now;
it++;
}
r-now;
now=r+;
if(now-last->ans)
{
ans=now-last-;
}
cout<<ans<<endl;
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}

The Golden Age CodeForces - 813B (数学+枚举)的更多相关文章

  1. CodeForce-813B The Golden Age(数学+枚举)

    The Golden Age CodeForces - 813B 题目大意:如果一个数t=x^a+y^b(a,b都是大于等于0的整数)那就是一个unlucky数字.给你x,y,l,r(2 ≤ x, y ...

  2. Why The Golden Age Of Machine Learning is Just Beginning

    Why The Golden Age Of Machine Learning is Just Beginning Even though the buzz around neural networks ...

  3. Codeforces 813B The Golden Age(数学+枚举)

    题目大意:如果一个数t=x^a+y^b(a,b都是大于等于0的整数)那就是一个unlucky数字.给你x,y,l,r(2 ≤ x, y ≤ 10^18, 1 ≤ l ≤ r ≤ 10^18),求出l到 ...

  4. 【数学】codeforces B. The Golden Age

    http://codeforces.com/contest/813/problem/B [题意] 满足n=x^a+y^b的数字为不幸运数字,a,b都是非负整数: 求闭区间[l,r]上的最长的连续幸运数 ...

  5. Educational Codeforces Round 22 B. The Golden Age(暴力)

    题目链接:http://codeforces.com/contest/813/problem/B 题意:就是有一个数叫做不幸运数,满足题目的 n = x^a + y^b,现在给你一个区间[l,r],让 ...

  6. Sonya and Matrix CodeForces - 1004D (数学,构造)

    http://codeforces.com/contest/1004/problem/D 题意:网格图给定到中心点的曼哈顿距离数组, 求该图n,m及中心点位置 首先可以观察到距离最大值mx一定在某个角 ...

  7. codeforces 1183F 离散化枚举 约数定理

    codeforces1183F 有技巧的暴力 传送门:https://codeforces.com/contest/1183/problem/F 题意: 给你n个数,要你从中选出最多三个数,使得三个数 ...

  8. bzoj 1257: [CQOI2007]余数之和sum 数学 && 枚举

    1257: [CQOI2007]余数之和sum Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 1779  Solved: 823[Submit][Sta ...

  9. 2-08. 用扑克牌计算24点(25) (ZJU_PAT 数学 枚举)

    题目链接:http://pat.zju.edu.cn/contests/ds/2-08 一副扑克牌的每张牌表示一个数(J.Q.K分别表示11.12.13,两个司令都表示6).任取4张牌.即得到4个1~ ...

随机推荐

  1. CSS3系列教程:HSL 和HSL

    使用CSS3 HSL声明同样是用来设置颜色的.下一个呢? HSLA? 是的,这个和RGBA的效果是一样的. HSL声明使用色调Hue(H).饱和度Saturation(s)和亮度Lightness(L ...

  2. Burp - Attack type

    有时间不用难免忘记,做个总结 1. Sniper (狙击手) 它使用一组Payload集合,依次替换Payload位置上(一次攻击只能使用一个Payload位置)被§标志的文本(而没有被§标志的文本将 ...

  3. docker swarm 实例

    1.配置三台虚拟机 先在virtualbox上生成三个Linux主机,一个manager1(管理节点),两个工作节点worker1和worker2 1)manager1 userdeMacBook-P ...

  4. oracle 迁移

    一.创建逻辑目录,该命令不会在操作系统创建真正的目录,最好以system等管理员创建. create directory exp_shengchan as '/home/oracle/exp_shen ...

  5. Qt5中的lambda表达式和使用lambda来写connect

    c11新特性中加入了lambda表达式,所以Qt 也支持 需在.pro文件中加入 CONFIG += c++11 例子: QString program = "C:/Windows/Syst ...

  6. C++ new/malloc、delete/free

    1.new和delete是运算符,可以被重载:malloc和free是库函数,不能被重载. 2.new会调用对象的构造函数,delete会调用对象的析构函数:malloc和free不会.

  7. MySQL 基础十一 事件

    1.查看事件 2.创建事件 3.执行事件,并查看执行结果是否正确 一 查看事件 -- 1.查看所有事件(显示执行频率(按年.月.日).创建日期.最后执行事件等)SELECT * FROM mysql. ...

  8. java中使用阻塞队列实现生产这与消费这之间的关系

    需求如下: 有一个生产者和一个消费者,生产者不断的生产产品,消费这不断的消费产品.产品总数为N. 1.生产顺序按队列的方式,先进先出. 2.生产者和消费这可以同时进行. 3.当生产者生产了N个产品后不 ...

  9. 查看Orcale数据里的表是否有变化

    由于我们公司一个数据库两个应用在使用,导致一个应用修改了数据库,另一个应用用的缓存而不知道有更新还是原来的结果.原来的处理方式是采用session缓存的方式,用户登出了就清空缓存,这样只需要重新登录一 ...

  10. Image Restoration[Deep Image Prior]

    0.背景 这篇论文是2017年11月29号第一次提交到arxiv并紧接着30号就提交了V2版本的. 近些年DCNN模型在图像生成和修复上面表现很好,大部分人认为好的原因主要是由于网络基于大量的图片训练 ...