- 解决方法,贪心。
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
long L, R;
L = rd.nextLong();
R = rd.nextLong();
PrintWriter out = new PrintWriter(System.out);
List<long[]> ans = new ArrayList<>();
if (L == 0) {
ans.add(new long[]{0,1L << max2(R)});
L = (1L<<max2(R));
}
long curr = L;
while (curr < R) {
int h = maxHighBit(curr);
long diff = R - curr;
long next = 0;
if (max2(diff) >= h) {
next = curr + (1L << h);
} else {
next = curr + (1L << max2(diff));
}
ans.add(new long[]{curr,next});
curr = next;
}
out.println(ans.size());
for (long[] tmp:ans) {
out.println(tmp[0] + " " + tmp[1]);
}
out.flush();
out.close();
}
public static int max2(long n) {
for (int i = 0; i <= 61; i++) {
if( (1L<<i) > n) {
return i - 1;
}
}
return 0;
}
public static int maxHighBit(long n) {
int num = 0;
while (n % 2 == 0) {
num++;
n = n / 2;
}
return num;
}
}
class rd {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokenizer = new StringTokenizer("");
// nextLine()读取字符串
static String nextLine() throws IOException {
return reader.readLine();
}
// next()读取字符串
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
// 读取一个int型数值
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
// 读取一个double型数值
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
// 读取一个long型数值
static long nextLong() throws IOException {
return Long.parseLong(next());
}
// 读取一个BigInteger
static BigInteger nextBigInteger() throws IOException {
BigInteger d = new BigInteger(rd.nextLine());
return d;
}
}
标签:IOException,return,Interval,next,static,abc349,new,atcode,throws
From: https://www.cnblogs.com/fishcanfly/p/18133549