题目链接:http://codeforces.com/contest/813/problem/B
题意:就是有一个数叫做不幸运数,满足题目的 n = xa + yb,现在给你一个区间[l,r],让你找一个在这个区间里面一个最长的区间使得这个区间里面的所有数都不是不幸运数,让你输出最长区间的区间长度 ;
#include<cstdio>
#include<algorithm>
#define max_n 1010
#include<cstring>
#include<cmath>
using namespace std;
typedef long long LL;
LL a[max_n],b[max_n],s[max_n];
int main()
{
LL x,y,l,r;
scanf("%lld %lld %lld %lld",&x,&y,&l,&r);
a[0]=b[0]=1;
int cnt1=1,cnt2=1;
while(r/x>=a[cnt1-1]) //避免溢出
{
a[cnt1]=a[cnt1-1]*x;
cnt1++;
}
while(r/y>=b[cnt2-1])
{
b[cnt2]=b[cnt2-1]*y;
cnt2++;
}
int p=0;
for(int i=0;i<cnt1;i++)
for(int j=0;j<cnt2;j++)
if(a[i]+b[j]>=l && a[i]+b[j]<=r)
s[p++]=a[i]+b[j];
sort(s,s+p);
LL ans=0;
if(p>0) //特判端点位置
ans=max(s[0]-l,r-s[p-1]);
else
ans=r-l+1;
for(int i=0;i<p;i++) //滚动相减
{
ans=max(s[i]-l-1,ans);
l=s[i];
}
printf("%lld\n",ans);
return 0;
}