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. java死锁详解

    进程死锁及解决办法: 一:死锁的概念:    死锁是进程死锁的简称    什么是死锁:    死锁是指多个进程循环等待他方占有的资源而无限的僵持下去的局面.很显然,没有外力作用,那么死锁涉及到的各个进 ...

  2. Spring 自动清除缓存的配置

    - spring.resources.chain.strategy.content.enabled=true - spring.resources.chain.strategy.content.pat ...

  3. c++11の的左值、右值以及move,foward

    左值和右值的定义 在C++中,可以放到赋值操作符=左边的是左值,可以放到赋值操作符右边的是右值.有些变量既可以当左值又可以当右值.进一步来讲,左值为Lvalue,其实L代表Location,表示在内存 ...

  4. HDU - 5078 水题

    题意:最大困难=距离 / 相邻时间 #include<cstring> #include<cstdio> #include<cmath> #define ll do ...

  5. centos断网调试

    保存命令 按ESC键 跳到命令模式,然后: :w 保存文件但不退出vi:w file 将修改另外保存到file中,不退出vi:w! 强制保存,不推出vi:wq 保存文件并退出vi:wq! 强制保存文件 ...

  6. Qt+QGIS二次开发:自定义类实现查询矢量数据的属性字段值(图查属性)

    在GIS领域,有两种重要的查询操作,图查属性和属性查图. 本文主要介绍如何在QGIS中通过从QgsMapToolIdentify中派生自定义类实现查询矢量数据的属性字段值(图查属性). 重点参考资料: ...

  7. 深入浅出的webpack4构建工具--webpack4+react构建环境(二十)

    下面我们来配置下webpack4+react的开发环境,之前都是针对webpack4+vue的.下面我们也是在之前项目结构的基础之上进行配置下. 首先看下如下是我为 webpack4+react 基本 ...

  8. MySQL 基础十一 事件

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

  9. FreeRTOS的任务非运行态

    当FreeRTOS启动任务调度器以后,任务调度器会在心跳中断函数中确定下一个要运行的任务,如果任务调度器仅仅依靠任务优先级来判断该运行哪个任务,这样会造成低优先级的任务根本没法运行,因为FreeRTO ...

  10. Omi框架学习之旅 - 插件机制之omi-transform及原理说明

    给omi-transform插件做个笔记,使用起来也很爽. transform.js这个库,一直想写一篇帖子的,可是,数学不好,三维矩阵和二位矩阵理解的不好,所以迟迟没写了, 这也是一个神库,反正我很 ...