http://codeforces.com/contest/813/problem/B

【题意】

满足n=x^a+y^b的数字为不幸运数字,a,b都是非负整数;

求闭区间[l,r]上的最长的连续幸运数字的区间长度。

2 ≤ x, y ≤ 10^18, 1 ≤ l ≤ r ≤ 10^18

【思路】

因为x,y>=2,所以x^a<=10^18,a不超过60

那么我们可以枚举所有的x^i

【注意】

这道题对于数字的处理一定要特别小心。

1.

 while (num <= 1e18)
num = num * x

错误一

 while (num * x <= 1e18)
num = num * x

错误二

这两种写法都不对,都会爆ll,因为num*x已经爆了ll,变成了<1e18的数
正确写法是:
 while(num<=1e18/x)
{
num*=x;
}

写法一

 int p=floor(log(r)/log(x));
int q=floor(log(r)/log(y));

写法二

写法二是用log求出个数后再for循环

2. pow函数精度不够,会WA

一开始我用了pow函数,结果WA了8发.....都是这个原因,后来手写了快速幂......以后不用这个函数了

 ll fpow(ll x,int k)
{
long long res=;
while(k)
{
if(k&)res=res*x;
x=x*x;
k>>=;
}
return res;
}

fastpower

【Accepted】

 #include <iostream>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <bitset>
#include <ctime>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=;
ll a[maxn];
ll b[maxn];
ll x,y,l,r;
ll sum[maxn*maxn];
ll fpow(ll x,int k)
{
long long res=;
while(k)
{
if(k&)res=res*x;
x=x*x;
k>>=;
}
return res;
}
int main()
{
cin>>x>>y>>l>>r;
int p=floor(log(r)/log(x));
int q=floor(log(r)/log(y));
for(int i=;i<=p;i++)
{
a[i]=fpow(x,i);
}
for(int i=;i<=q;i++)
{
b[i]=fpow(y,i);
}
int cnt=;
for(int i=;i<=p;i++)
{
for(int k=;k<=q;k++)
{
if(a[i]+b[k]<=r&&a[i]+b[k]>=l)
sum[cnt++]=a[i]+b[k];
}
}
sort(sum,sum+cnt);
cnt=unique(sum,sum+cnt)-sum;
if(cnt==)
{
cout<<r-l+<<endl;
return ;
}
ll ans=max(sum[]-l,r-sum[cnt-]);
for(int i=;i<=cnt-;i++)
{
ans=max(ans,sum[i]-sum[i-]-);
}
cout<<ans<<endl;
return ;
}

【数学】codeforces B. The Golden Age的更多相关文章

  1. 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到 ...

  2. 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  ...

  3. 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 ...

  4. 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 ...

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

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

  6. 贪心/数学 Codeforces Round #212 (Div. 2) A. Two Semiknights Meet

    题目传送门 /* 贪心/数学:还以为是BFS,其实x1 + 4 * k = x2, y1 + 4 * l = y2 */ #include <cstdio> #include <al ...

  7. 数学 Codeforces Round #219 (Div. 2) B. Making Sequences is Fun

    题目传送门 /* 数学:这题一直WA在13组上,看了数据才知道是计算cost时超long long了 另外不足一个区间的直接计算个数就可以了 */ #include <cstdio> #i ...

  8. 数学 Codeforces Round #282 (Div. 2) B. Modular Equations

    题目传送门 题意:a % x == b,求符合条件的x有几个 数学:等式转换为:a == nx + b,那么设k = nx = a - b,易得k的约数(>b)的都符合条件,比如a=25 b=1 ...

  9. 数学 Codeforces Round #308 (Div. 2) B. Vanya and Books

    题目传送门 /* 水题:求总数字个数,开long long竟然莫名其妙WA了几次,也没改啥又对了:) */ #include <cstdio> #include <iostream& ...

随机推荐

  1. pscp多线程传输文件

    前面说过pscp不支持多线程,所以在此特地实现了一个 程序分三个部分: 1.初始化各种参数,涉及getopt函数的使用 2.重新定义scp,实现传递IP然后远程拷贝 3.启动多线程调用scp,涉及多线 ...

  2. php数组与字符串转换

    1.将字符串转换成数组的几个函数: (1)explode(separate,string) 示例:$str = "Hello world It's a beautiful day" ...

  3. php使用json_decode返回NULL

    php5.2以后自带json_decode函数,但是对json文本串的格式要求非常严格. 很可能使用该函数得到的返回值是NULL 可以使用使用json_last_error()函数获取到的返回值来帮助 ...

  4. Java基础50题test3—水仙花数

    水仙花数 题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例 如:153 是一个"水仙花数", ...

  5. re匹配语法-match、search和findall

    1.re.match() 匹配第一个值 列表里的值可以有多个范围,有一个符合就可以. match只匹配第一个值,所以列表里的范围是第一个值得取值范围.如果第一个值被设定好且存在,那么列表的取值范围变为 ...

  6. 微信小程序 图片加载失败处理方案

    小程序端展示网络资源图片可能会失败,下面介绍一种自己的处理方法 1. js文件中判断图片 url 是否存在,存在则正常显示,不存在则替换url为本地默认图片 2. 当图片 url 存在,但是加载失败时 ...

  7. htm 中 <b>和<strong>的区别

    显示上两者没有任何区别,都是粗体<b>:为了加粗而加粗,推荐使用 css font-weight 属性来创建粗体文字.<strong>:为了强调而加粗,表示十分重要.在网页中使 ...

  8. ag-grid-vue的 行默认选中

    that.$nextTick(() => { that.gridListOptions.api.onGroupExpandedOrCollapsed(); that.$nextTick(() = ...

  9. 架包Error inflating class错误

    当引用架包后,出现Error inflating class错误时通常要检测架包是否正确引用: 1.首先将你所需要的架包拷贝到工程目录下: 2.右击工程,选择Build Path-->confi ...

  10. AIX 11203 ASM RAC安装

    1:查看系统版本 [rac1:root:/hacmp/hacmp5.4/ha5.4/installp/ppc] oslevel -s 6100-06-06-1140 lslpp -al bos.adt ...