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. v-bind和v-on

    v-bind指令用于设置HTML属性:v-bind:href  缩写为 :href <a :href="{{url}}">aa</a> v-on 指令用于绑 ...

  2. AJPFX总结java 中类的创建和使用

    //面向对象中类的创建和 调用 ============================================================= 类的使用: 类的使用分为两个动作:创建对象与 ...

  3. 迭代器模式及php实现

    迭代器模式: 迭代器模式是遍历集合的成熟模式,迭代器模式的关键是将遍历集合的任务交给一个叫做迭代器的对象,它的工作时遍历并选择序列中的对象,而客户端程序员不必知道或关心该集合序列底层的结构. 角色: ...

  4. HTTP 方法:GET 对比 POST 转自w3school

    两种最常用的 HTTP 方法是:GET 和 POST. 什么是 HTTP? 超文本传输协议(HTTP)的设计目的是保证客户机与服务器之间的通信. HTTP 的工作方式是客户机与服务器之间的请求-应答协 ...

  5. Elasticsearch搜索含有数字标签的处理

    {"tag_id":“12345”} 在search的时候是完全匹配,因为Elasticsearch在处理这个的过程中把“123456”字符当成一个整体的数据,因此折腾了好久就是找 ...

  6. 一行命令杀掉defunct进程

    一行命令杀掉defunct进程 今天在杀掉defunc过程中一直搞不完,索性写一行命令,注意先看懂谨慎使用 ps -ef|grep defunct|awk '{print " ps -ef| ...

  7. 前端什么是BFC

    什么是BFC? 全称块级格式化上下文?什么意思不懂.看了好多博客,基本都是抄的,真心都不是大白话.我今天来总结一下,用菜鸟级别的语言来描述. BFC 应该可以抽象成一个 独立的个体,出淤泥而不染的白莲 ...

  8. Comparator.comparing比较排序

    使用外部比较器Comparator进行排序 当我们需要对集合的元素进行排序的时候,可以使用java.util.Comparator 创建一个比较器来进行排序.Comparator接口同样也是一个函数式 ...

  9. html upload_file 对象(2018/02/26)工作收获

    php.ini中可以设置上传文件的大小,如果超过设置大小,上传失败.$_File 数组当中接受到的文件对象size为0

  10. django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module

    pip3 install mysqlclient try again python manage.py makemigrations python manage.py migrate