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 + 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 x, y, l 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
2 3 1 10
1
3 5 10 22
8
2 3 3 5
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 (数学+枚举)的更多相关文章
- 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 ...
- 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 ...
- 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到 ...
- 【数学】codeforces B. The Golden Age
http://codeforces.com/contest/813/problem/B [题意] 满足n=x^a+y^b的数字为不幸运数字,a,b都是非负整数: 求闭区间[l,r]上的最长的连续幸运数 ...
- Educational Codeforces Round 22 B. The Golden Age(暴力)
题目链接:http://codeforces.com/contest/813/problem/B 题意:就是有一个数叫做不幸运数,满足题目的 n = x^a + y^b,现在给你一个区间[l,r],让 ...
- Sonya and Matrix CodeForces - 1004D (数学,构造)
http://codeforces.com/contest/1004/problem/D 题意:网格图给定到中心点的曼哈顿距离数组, 求该图n,m及中心点位置 首先可以观察到距离最大值mx一定在某个角 ...
- codeforces 1183F 离散化枚举 约数定理
codeforces1183F 有技巧的暴力 传送门:https://codeforces.com/contest/1183/problem/F 题意: 给你n个数,要你从中选出最多三个数,使得三个数 ...
- bzoj 1257: [CQOI2007]余数之和sum 数学 && 枚举
1257: [CQOI2007]余数之和sum Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 1779 Solved: 823[Submit][Sta ...
- 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~ ...
随机推荐
- one_code=soup.find('a',href=re.compile(r"ill")) NameError: name 're' is not defined
啊啊啊啊我又来了,真的是万事开头难啊,一个问题刚解决,又来了一个问题..依旧跟着视频教学,说“re"这里按Ctrl+e导入正则表达式,可我弄了半天也没有反应..以至于最后的运行结果就是这样. ...
- Handler实现线程间的通信1
通过Handler实现线程间的通信,在主线程当中实现Handler的handlerMessage()方法,在WorkerThread中通过Handler发送消息 Handler实现线程间的通信实例: ...
- MySQL基本教程(一)
创建数据库 mysql -u root -p CREATE DATABASE Hank; 删除数据库 drop database Hank; 选择数据库 use Hank; MySQL 数据类型 My ...
- Scala 上下文界定
上下文界定的类型参数形式为T:M的形式,其中M是一个泛型,这种形式要求存在一个M[T]类型的隐式值: /** * 上下文界定 */ @Test def testOrdering_Class_Conte ...
- 离线安装Cloudera Manager 5和CDH5(最新版5.9.3) 完全教程(七)界面安装
一.安装过程 1.1 登录 1.2 接受许可协议 1.3 选择免费版本 1.4 选择下一步 1.5 选择当前管理的主机 1.6 选择使用Parcel安装,选择CDH版本,点击继续 1.7 等待安装 此 ...
- activemq5.14+zookeeper3.4.9实现高可用
一.activeMQ主要的几类部署方式比较1.默认的单机部署(kahadb)activeMQ的默认存储的单机方式,以本地kahadb文件的方式存储,所以性能指标完全依赖本地磁盘IO,不能提供高可用. ...
- SQL 公用表达式CTE
一 基本用法 with mywith as(select * from Student ) select * from mywith 二 递归调用 with mywith as( select ID, ...
- ESP32 environment ubuntu
ubuntu官方用的是16.04版本的,阿里源下载地址:http://mirrors.aliyun.com/ubuntu-releases/16.04/ 用官方版本的好处就是省的搞一堆各种错误 ...
- Mac安装LNMP环境,升级php7
Mac安装nginx+mysql+php 安装nginx比较麻烦,要安装pcre ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre ...
- Vue与Element走过的坑。。。。带上Axios
1.Axios中post传参数组(java后端接收数组) 虽然源数据本身就是数组,但是传参时会自动变成key:数值或者服务器无法接收的对象,如下 如果不仔细看,很容易认为这两种情况没毛病..(后端不背 ...