G - Numbers
DreamGrid has a nonnegative integer . He would like to divide into nonnegative integers and minimizes their bitwise or (i.e. and should be as small as possible).
Input
There are multiple test cases. The first line of input contains an integer , indicating the number of test cases. For each test case:
The first line contains two integers and ().
It is guaranteed that the sum of the length of does not exceed .
Output
For each test case, output an integer denoting the minimum value of their bitwise or.
Sample Input
5 3 1 3 2 3 3 10000 5 1244 10
Sample Output
3 3 1 2000 125
import java.util.Scanner;
import java.math.*;
public class Main {
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
int t;
t=reader.nextInt();
BigInteger one=BigInteger.ONE;
while(t>0){
t--;
BigInteger n=reader.nextBigInteger();
BigInteger m=reader.nextBigInteger();
BigInteger ans=BigInteger.ZERO;
int x=n.bitLength();
for(int i=x;i>=0;i--) {
///应该让最高位尽量为1
BigInteger sb=one.shiftLeft(i);///(2^k-1)*m>=n
//System.out.print(sb+" ");
///如果
if(sb.subtract(one).multiply(m).compareTo(n)>=0) {
continue;
} else {
ans=ans.add(sb);
BigInteger o=n.divide(sb);
if(o.compareTo(m)>=0) {
n=n.subtract(m.multiply(sb));
} else {
n=n.mod(sb);
}
System.out.println(n+" "+ans+" end");
//System.out.println(n);
//System.out.println(ans);
//System.out.println("end");
}
}
System.out.println(ans);
}
reader.close();
}
}