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种,每种 ...
随机推荐
- 【BZOJ4137】火星商店问题(线段树分治,可持久化Trie)
[BZOJ4137]火星商店问题(线段树分治,可持久化Trie) 题面 洛谷 BZOJ权限题 题解 显然可以树套树,外层线段树,内层可持久化Trie来做. 所以我们需要更加优美的做法.--线段树分治. ...
- Insert Node in Sorted Linked List
Insert a node in a sorted linked list. Have you met this question in a real interview? Yes Example ...
- Classical Binary Search
Find any position of a target number in a sorted array. Return -1 if target does not exist. 与题目 Firs ...
- _MSC_VER
https://msdn.microsoft.com/en-us/library/vstudio/b0084kay.aspx Evaluates to an integer literal that ...
- poj3783 Balls
Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1110 Accepted: 721 Description ...
- python常用序列list、tuples及矩阵库numpy的使用
近期开始学习python机器学习的相关知识,为了使后续学习中避免编程遇到的基础问题,对python数组以及矩阵库numpy的使用进行总结,以此来加深和巩固自己以前所学的知识. Section One: ...
- valgrind检查C/C++内存泄漏
valgrind --tool=memcheck --leak-check=full ./httptest valgrind --tool=memcheck --leak-check=full -- ...
- 「CSS」css基础
1. 文字水平居中 将一段文字置于容器的水平中点,只要设置text-align属性即可: text-align:center; 2. 容器水平居中 先该容器设置一个明确宽度,然后将margin的水平值 ...
- duilib 给List表头增加百分比控制宽度的功能
转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/42503147 最近项目里需要用到包含表头列表,而窗体大小改变后,每个列表 ...
- Asp.net mvc 大文件上传 断点续传 进度条
概述 项目中需要一个上传200M-500M的文件大小的功能,需要断点续传.上传性能稳定.突破asp.net上传限制.一开始看到51CTO上的这篇文章,此方法确实很不错,能够稳定的上传大文件,http: ...