HDU5050Divided Land (java large number)
HDU5050Divided Land (java large number)
Question Link
A long and wide rectangle requires you to divide the area into multiple squares with N sides and there is no space left. Require N to be as big as possible. Given the length and width of binary, the output N is also in binary format.
Solution: Find the GCD of L and M. However, because the input of this question is a very large binary number, it is only in the middle of the GCD to convert to decimal to obtain N, and the output N is also in the binary form.
Code:
import java.util.*;import java.io.*;import java.math.*;public class Main { public static BigInteger GCD (BigInteger num1, BigInteger num2) { if (num2.equals(BigInteger.ZERO)) return num1; return GCD(num2, num1.mod(num2)); } public static void main(String args[]) { Scanner cin = new Scanner(new BufferedInputStream(System.in)); PrintWriter cout = new PrintWriter(new BufferedOutputStream(System.out)); int T; T = cin.nextInt(); BigInteger L, W; for (int i = 1; i <= T; i++) { L = cin.nextBigInteger(2); W = cin.nextBigInteger(2); BigInteger ans = GCD(L, W); cout.printf("Case #%d: ", i); cout.println(ans.toString(2)); } cin.close(); cout.close(); }}