The Golden Age CodeForces - 813B (数学+枚举)
Posted qieqiemin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 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 ≤ 1018, 1 ≤ l ≤ r ≤ 1018).
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
2 3 1 10
1
3 5 10 22
8
2 3 3 5
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].
思路:直接枚举a和b的值,因为2^60大于1E18,所以可以0~61的任意枚举。
把可以得出的数字加入到一个set中,最后从头到尾遍历找最大的区间。
注意:由于数字很大,稍微大一点就爆了longlong 所以判断是否大于R的时候,用自带函数pow,因为float/double的范围很大,比LL大多了,所以不会炸精度,可以剔除掉过大的数据,
而加入到set的时候用快速幂来算那个数值,因为浮点数有精度误差。还读到了大佬的博客是把除法改成乘法来防止爆longlong,受益匪浅。
推荐一个:https://blog.csdn.net/qq_37129433/article/details/81667733
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), ‘ ‘, sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll x,y,l,r;
vector<ll> vc;
set<ll> v;
ll quick_pow(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1) ans= ( ans * a);
a=( a* a);
b>>=1;
}
return ans;
}
int main()
{
gbtb;
cin>>x>>y>>l>>r;
for(ll i=0ll;i<=63ll;i++)
{
if(pow(x,i)>r||pow(x,i)<=0)
{
break;
}
for(ll j=0ll;j<=63ll;j++)
{
long long k=1ll*quick_pow(x,i)+1ll*quick_pow(y,j);
// if(quick_pow(x,i)>r||quick_pow(x,i)<=0)
// {
// break;
// }
if(pow(y,j)>r||pow(y,j)<=0)
{
break;
}
if(k<=0||k>r)
{
break;
}else
{
if(k>=l&&k<=r)
{
v.insert(k);
}
}
}
}
set<ll> ::iterator it=v.begin();
ll ans=0ll;
ll last=l-1;
ll now;
while(it!=v.end())
{
// cout<<*it<<" ";
now=*it;
if(now-last-1>ans)
{
ans=now-last-1;
}
last=now;
it++;
}
r-now;
now=r+1;
if(now-last-1>ans)
{
ans=now-last-1;
}
cout<<ans<<endl;
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ‘ ‘ || ch == ‘
‘);
if (ch == ‘-‘) {
*p = -(getchar() - ‘0‘);
while ((ch = getchar()) >= ‘0‘ && ch <= ‘9‘) {
*p = *p * 10 - ch + ‘0‘;
}
}
else {
*p = ch - ‘0‘;
while ((ch = getchar()) >= ‘0‘ && ch <= ‘9‘) {
*p = *p * 10 + ch - ‘0‘;
}
}
}
以上是关于The Golden Age CodeForces - 813B (数学+枚举)的主要内容,如果未能解决你的问题,请参考以下文章
Zuora's IPO is another step in golden age of enterprise SaaS
HOJ13907 Diana and the Golden Apples
The House with a Golden Window
Codeforces Round #512 (Div. 2, based on Technocup 2019 Elimination Round 1) C. Vasya and Golden Tick