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到r内没有unlucky数字的最小区间。
解题思路:可以知道a,b最多也不会超过60(2^60>1e18),所以可以直接枚举x^a+y^b的值存到vector里,然后排序,遍历一下vector,找出v[i+1]-v[i]-1(因为两端都是unlucky数字所以要两个端点都不算在长度内)最大的区间即可。要注意vector为空和两个端点的特判。还有数字的溢出问题,这个没办法直接判断是否溢出,可以通过使用一个d=r,比如每次x次方加一的时候,就将d/x,当d==0说明x^a已经超出r的范围了。
代码:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll; vector<ll>v; int main(){
ll x,y,l,r;
cin>>x>>y>>l>>r;
ll tx,ty;
ll d1=r;
for(int i=;i<=;i++){
if(i!=)
d1/=x;
if(d1==)
break;
if(i==)
tx=;
else
tx*=x;
ll d2=r;
for(int j=;j<=;j++){
if(j!=)
d2/=y;
if(d2==)
break;
if(j==)
ty=;
else
ty*=y;
if(tx+ty>=l&&tx+ty<=r){
v.push_back(tx+ty);
}
}
}
//vector为空的情况
if(!v.size()){
cout<<r-l+<<endl;
return ;
} ll ans=;
sort(v.begin(),v.end());
for(int i=;i<v.size();i++){
//如果l不是unlucky数字,那就额外计算v[0]-l这段区间
if(i==&&v[]!=l)
ans=max(ans,v[i]-l);
//如果r不是unlucky数字,就额外计算r-v[v.size()-1]这段区间
if(i==v.size()-){
if(!sign2)
ans=max(ans,r-v[i]);
}
else
ans=max(ans,v[i+]-v[i]-);
}
cout<<ans<<endl;
}
Codeforces 813B The Golden Age(数学+枚举)的更多相关文章
- 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 ...
- 【数学】codeforces B. The Golden Age
http://codeforces.com/contest/813/problem/B [题意] 满足n=x^a+y^b的数字为不幸运数字,a,b都是非负整数: 求闭区间[l,r]上的最长的连续幸运数 ...
- 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 ...
- 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 ...
- Educational Codeforces Round 22 B. The Golden Age(暴力)
题目链接:http://codeforces.com/contest/813/problem/B 题意:就是有一个数叫做不幸运数,满足题目的 n = x^a + y^b,现在给你一个区间[l,r],让 ...
- CF思维联系–CodeForces - 222 C Reducing Fractions(数学+有技巧的枚举)
ACM思维题训练集合 To confuse the opponents, the Galactic Empire represents fractions in an unusual format. ...
- Codeforces 626D Jerry's Protest(暴力枚举+概率)
D. Jerry's Protest time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...
- Codeforces gym101612 L.Little Difference(枚举+二分)
传送:http://codeforces.com/gym/101612 题意:给定一个数n(<=1e18),将n分解为若干个数的成绩.要求这些数两两之间的差值不能大于1. 分析: 若n==2^k ...
- Codeforces 734C Anton and Making Potions(枚举+二分)
题目链接:http://codeforces.com/problemset/problem/734/C 题目大意:要制作n个药,初始制作一个药的时间为x,魔力值为s,有两类咒语,第一类周瑜有m种,每种 ...
随机推荐
- Unity3D for VR 学习(10): Unity LOD Group 组件
LOD (Level of Detail), 远小近大思想. LOD,在Unity中是用到了空间换时间的优化方法:即程序加载2套模型,导致包会增大:在运行时刻,远处的用面数少的模型–模糊一些,近处用面 ...
- python基础----实现上下文管理协议__enter__和__exit__
我们知道在操作文件对象的时候可以这么写 with open('a.txt') as f: '代码块' 上述叫做上下文管理协议,即with语句,为了让一个对象兼容with语句,必须在这个对象的类中声明_ ...
- 使用 openssl 生成证书(转)
一.openssl 简介 openssl 是目前最流行的 SSL 密码库工具,其提供了一个通用.健壮.功能完备的工具套件,用以支持SSL/TLS 协议的实现.官网:https://www.openss ...
- OpenCV中响应鼠标消息 (转)
#include <cv.h> #include <highgui.h> #include <stdio.h> #pragma comment(lib," ...
- Python中scatter()函数--转载
原博地址:http://blog.csdn.net/anneqiqi/article/details/64125186 最近开始学习Python编程,遇到scatter函数,感觉里面的参数不知道什么意 ...
- angular2 获取到的数据无法实时更新的问题
在修改完组件数据之后调用下面两句: this.changeDetectorRef.markForCheck(); this.changeDetectorRef.detectChanges(); 注入到 ...
- hdu 2608 (数论)
hdu2608 0 or 1 题意:给你一个数N(N < 2^31), 问从 1--N 所有数的因子和S(N),求 S(N)%2 的值. 链接:http://acm.hdu.edu.cn/sh ...
- Maven手动添加依赖的jar文件到本地Maven仓库
原文出处:https://www.iteblog.com/archives/646.html 寫得很好,轉走僅供學習,望諒解! Apache Maven,是一个软件(特别是Java软件)项目管理及自动 ...
- [Ahoi2008]Meet 紧急集合
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MBhttp://www.lydsy.com/JudgeOnline/p ...
- HTML入门(二)表格_字体_超链接_布局
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...