CodeForces 813B The Golden Age

来源:互联网 发布:如何成为淘宝分销商 编辑:程序博客网 时间:2024/05/20 12:51

B. The Golden Age

time limit per test:1 second
**memory limit per test:**256 megabytes
**input:**standard input
**output:**standard output
Unlucky year in Berland is such a year that its number n can be represented as n = xa + yb, where a and b are non-negative integer numbers.
For example, if x = 2 and y = 3 then the years 4 and 17 are unlucky (4 = 20 + 31, 17 = 23 + 32 = 24 + 30) and year 18 isn’t unlucky as there is no such representation for it.
Such interval of years that there are no unlucky years in it is called The Golden Age.
You should write a program which will find maximum length of The Golden Age which starts no earlier than the year l and ends no later than the year r. If all years in the interval [l, r] are unlucky then the answer is 0.

Input
The first line contains four integer numbers x, y, l and r (2 ≤ x, y ≤ 10的18次方, 1 ≤ l ≤ r ≤ 10的18次方).
Output
Print the maximum length of The Golden Age within the interval [l, r].
If all years in the interval [l, r] are unlucky then print 0.

Examples

Input
2 3 1 10
Output
1

Input
3 5 10 22
Output
8

Input
2 3 3 5
Output
0

Note
In the first example the unlucky years are 2, 3, 4, 5, 7, 9 and 10. So maximum length of The Golden Age is achived in the intervals [1, 1], [6, 6] and [8, 8].
In the second example the longest Golden Age is the interval [15, 22].

我也很想把题目完全翻译过来,可是我,真的不行…

所以我就告诉你们意译简化后直译目标版
所以这道题目就是告诉你一个闭区间[l,r],求[l,r]内最长的没有x的n次方+y的m次方(n和m的值任意)组成的值的子区间的长度。

这道题目很神奇

超级简单
但是
由于我愚蠢地没有使用结构体区间
所以写了一万个特判才过
被卡成傻X了

具体的题解

这道题目由于x和y增长的速度神tm快(毕竟指数函数),所以暴力枚举时间复杂度也不会很高
(2的60次方就大于了10的18次方)
所以就暴力枚举就好了

这道题目很讨厌的是这个区间变化太多了
于是我们选择写特判(正解应该是用结构体区间,那样好像最多两三个特判)
然后我们就==
用我的代码
暴力枚举特判吧

①题目的左右区间是可以被包含的,然而计算当中的不行

eg:区间[l,r]=>[10,20],x=15,y=2    在区间内的黄金年代有16,17,19    其中17-19的可行长度为1,而10-16的可行长度为6

所以我们选择特判,两个边缘区间的长度+1

②左右区间当中没有黄金年代

eg:上面的那个区间改成[10-15],可行长度为6

我们需要+2

③黄金年代正好在区间边缘上

eg:上面那个区间改成[10,16]然后最长长度10-16可行区间为6

这个时候又要+1了

上面这几种特判是可能有包含与被包含的关系的,所以特判也要先分清楚顺序

附上代码

#include <iostream>#include <cstdio>#include <algorithm>using namespace std;typedef long long ll;ll a,b,s,e,ad,ans=0;ll res[5678910],add[5678];int all,t;bool en=0,fi=0,tp=0;int main(){    scanf("%I64d%I64d%I64d%I64d",&a,&b,&s,&e);    for(ad=1,t=1;ad;t++)    {        add[t]=ad;        if(e/ad>=b)ad*=b;        else ad=0;    }    for(ad=1;ad;)    {        for(int i=1;i<t&&ad+add[i]<=e;i++)            res[++all]=ad+add[i];        if(e/ad>=a)ad*=a;        else ad=0;    }    res[++all]=e+1;    sort(res+1,res+all+1);    for(int i=2;i<=all;i++)    {        if(res[i-1]>=s&&res[i]<=e)ans=max(ans,res[i]-res[i-1]-1);        if(res[i]<e&&res[i]>s)            {                if(!fi&&res[i-1]<s)ans=max(ans,res[i]-s),fi=1;                if(!en&&res[i+1]>e)ans=max(ans,e-res[i]),en=1;            }        if(s>res[i-1]&&e<res[i])ans=max(ans,e-s+1);        if(s>=res[i-1]&&e<res[i])ans=max(ans,e-s);        if(s>res[i-1]&&e<=res[i])ans=max(ans,e-s);    }    if(s<res[1]&&e<res[1])ans=max(ans,e-s+1);    cout<<ans;    return 0;}
原创粉丝点击