题意: n = xa + yb (a,b >= 0)给你x,y和一个区间[l, r],问在这区间内连续最长的不满足n的长度。
思路:枚举a, b 把在[l, r]区间内的都找出来, 排个序,两两相减加一就是连续不满足的长度。(把l-1 和 r+1也放进去更好操作一些)
因为x, y <= 1e18, 容易溢出。。就拿java写了。。。。
代码:
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static BigInteger pp(BigInteger x, int p)
{
BigInteger ans = BigInteger.valueOf(1);
for(int i = 1; i <= p; i++)
ans = ans.multiply(x);
return ans;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
BigInteger a[] = new BigInteger[100005];
BigInteger x, y, l, r;
while(sc.hasNext())
{
int cnt = 0;
x = sc.nextBigInteger();
y = sc.nextBigInteger();
l = sc.nextBigInteger();
r = sc.nextBigInteger();
a[cnt++] = l.subtract(BigInteger.valueOf(1));
a[cnt++] = r.add(BigInteger.valueOf(1));
for(int i = 0; i < 65; i++)
{
BigInteger tmp1 = pp(x, i);
for(int j = 0; j < 65; j++)
{
BigInteger tmp2 = pp(y, j);
if(tmp1.add(tmp2).compareTo(l) >= 0 && tmp1.add(tmp2).compareTo(r) <= 0)
a[cnt++] = tmp1.add(tmp2);
}
}
Arrays.sort(a, 0, cnt);
BigInteger res = BigInteger.valueOf(0);
for(int i = 1; i < cnt; i++)
{
BigInteger tmp = a[i].subtract(a[i-1]).subtract(BigInteger.valueOf(1));
if(tmp.compareTo(res) >= 0)
res = tmp;
}
System.out.println(res);
}
}
}