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. Git使用简析

    推送本地操作 初始化一个本地Git仓库,在需要添加版本控制的文件夹根目录中使用git init命令. 添加文件到本地Git仓库: git add 文件名 # 添加文件到暂存区 git add . # ...

  2. Android掌中游斗地主游戏源码完整版

    源码大放送-掌中游斗地主(完整版),集合了单机斗地主.网络斗地主.癞子斗地主等,有史以来最有参考价值的源码,虽然运行慢了一点但是功能正常,用的是纯java写的. 项目详细说明:http://andro ...

  3. Mac OSX简单使用中会用到的

    选择操作系统(例如选择BootCamp分区的Windows):开机按住Option键直到磁盘图标出现后选择. 忘记本地账号密码:按着Command+R开机选择Recovered启动打开终端输入re ...

  4. Hbase数据库简介

    Hbase是基于Hadoop下分布式存储 数据库,列式存储.(https://www.imooc.com/video/17202) 动态的增加列,不像关系数据库需要提前定义好列. 关系数据库     ...

  5. vue 组件相互传值

    Part.1  传值几种方式 在写项目时,遇到组件传值问题,特此记录一波~~~ (1)  父传子 (2)  子传父 (2)  兄弟组件传值 Part.2  父传子 顾名思义,就是父组件传值给子组件 子 ...

  6. largest rectangle in histogram leetcode

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  7. MySQL-04 高级特性

    学习要点 MySQL系统函数 MySQL视图 MySQL存储过程 MySQL自定义函数 MySQL触发器 MySQL事务 MySQL事件 MySQL系统函数 数学函数 获取整数的函数:CEIL(x). ...

  8. git 添加外部项目地址

    github 提交第三方模块流程   // git config --global user.name 'your name' 可以设置全局用户名,在commit记录里显示的是这个配置设置的名称. / ...

  9. 时间函数datetime time

    time模块 time翻译过来就是时间,有我们其实在之前编程的时候有用到过. #常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行.单位为秒. 2.time.time() 获取 ...

  10. 7. 配置undo表空间

    7. 配置undo表空间 undo日志可以存储在一个或多个undo表空间中,无需存储在系统表空间中. 要为MySQL实例配置单独的undo表空间,请执行以下步骤 [重要]: 只能在初始化新MySQL实 ...