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. PowerShell和Bash的介绍

    PowerShell是运行在windows平台的脚本,而Bash是运行在linux平台的脚本 现在bash能做的事情,PowerShell也能做,PowerShell的强大之处是它可以管理window ...

  2. poj3252Round Numbers

    链接 也算是组合 以前按组合做过一次 忘记怎么做的了 这次按dp写的 dp[i][j][g][k] 表示第i位为k(0|1)而且有j个1,g个0的情况数 貌似写的麻烦了...这一类的题,进行逐位计算就 ...

  3. hash 【模板】

    hash 功能: hash一般用于快速判断两个或多个字符串是否匹配. 实现 :    想一想,如果比较两个数子的话是很方便的很快,那么我们把整个字符串看成一个大数.  它是base进制的len位数.但 ...

  4. springboot项目启动问题

    在调试项目的时候有遇到这样一个问题: 项目启动后访问不通,编译没有任何问题,启动也没有报错,日志在打,但是访问不通.而且之前一直可以正常访问,在没改任何代码的情况下不能访问了. 尝试很多次偶然发现,点 ...

  5. PetStore项目总结

    数据库(MySQL): account(用户表:没有外键), profile(用户侧面信息表:有两个外键:catid,username), category(宠物总分类表--鱼:没有外键), prod ...

  6. cordova应用使用手机调试

    对于cordova应用的调试,最方便调试方式还是作为h5应用在浏览器来调试,调试好了再打包cordova应用和打包apk.然而h5应用时的效果跟最终在安卓手机运行还有少数情况会不一样,因此,也需要有能 ...

  7. android studio MQTT测试成功

    package myself.mqtt.wenzheng.studio.mqtt; import android.app.Notification; import android.app.Notifi ...

  8. Redis为什么这么快

    Redis为什么这么快 1.完全基于内存,绝大部分请求是纯粹的内存操作,非常快速.数据存在内存中,类似于HashMap,HashMap的优势就是查找和操作的时间复杂度都是O(1): 2.数据结构简单, ...

  9. 递归的可视化(Fibonacci)

    递归的可视化 修改递归函数,使其能够显示打印出每次函数递归调用的形参的值. 每一级调用的输出都带有一级缩进,就是使得程序的输出清晰.有趣并且有含义. 思路 以斐波那契数列为例,假设n=5,递归的形参如 ...

  10. Java后端学习路线_备战

    SpringCloud项目实战 Dubbo项目实战 项目实战应涵括哪些技术: 缓存.消息队列 WEB应用服务器(Weblogic.Jetty.JBoss.WebSphere) NoSQL(MongoD ...