Test instructions
01 give a number n, now to divide it into the number of M, the m number must be equal to N, and to make the m number or value of the smallest.
Thinking Analysis:
A simple greedy, from high to low, judging whether the current bit can be 1, if possible, then all the number of this bit all into 1
code example:
Import Java.math.*;import java.util.*;p ublic class Study {public static void main (string[] args) {Scanner cin = new Scanne R (system.in); BigInteger N, M;int t = Cin.nextint (); for (int cas = 0; cas < T; cas++) {n = Cin.nextbiginteger (); m = Cin.nextbiginteger (); BigInteger ans = biginteger.valueof (0); int len = 0; BigInteger SS = N, v = biginteger.valueof (0); while (Ss.compareto (v) >0) {ss = Ss.divide (Biginteger.valueof (2)); len++; } for (int i = len; I >= 0; i--) {BigInteger tem = biginteger.valueof (2); tem = TEM.POW (i). Subtract (biginteger.valueof (1)). Multiply (m);//system.out.println (TEM), if (Tem.compareto (n) < 0) {BigInteger f = biginteger.valueof (2). POW (i); ans = Ans.add (f); BigInteger p = n.divide (f); if (P.compareto (m) >= 0) n = n.subtract (m.multiply (f)); else n = n.subtract (p.multiply (f));// System.out.println (f);//system.out.println (P);}} System.out.println(ANS);}}
Java large number Greedy