Here are two binary numbers M. N is used to calculate their maximum common approx. 0 <m, n <2 ^ 1000
First, convert the binary value to the decimal value to obtain the maximum common number, and then convert the result to the binary number. A large number is required.
import java.util.*;import java.math.*;public class wl6_9 {static BigInteger two = BigInteger.valueOf(2), one = BigInteger.ONE,zero = BigInteger.ZERO;static BigInteger gcd(BigInteger a, BigInteger b) {while (!(a.mod(b).equals(zero))) {BigInteger t = a.mod(b);a = b;b = t;}return b;}static void bprint(BigInteger x) {if (x.equals(zero))return;bprint(x.divide(two));if (x.mod(two).equals(one))System.out.print(1);elseSystem.out.print(0);}public static void main(String[] args) {Scanner in = new Scanner(System.in);int T = in.nextInt();String a, b;for (int t = 1; t <= T; t++) {a = in.next();b = in.next();int la = a.length(), lb = b.length();BigInteger m = zero, n = zero;for (int i = 0; i < la; ++i) {m = m.multiply(two);if (a.charAt(i) == '1')m = m.add(one);}for (int i = 0; i < lb; ++i) {n = n.multiply(two);if (b.charAt(i) == '1')n = n.add(one);}System.out.printf("Case #%d: ", t);bprint(gcd(m, n));System.out.println();}in.close();}}
Divided land
Problem descriptionit's time to fight the local despots and redistribute the land. there is a rectangular piece of land granted from the Government, whose length and width are both in binary form. as the mayor, you must segment the land into multiple squares of equal size for the specified agers. what are required is there must be no any waste and each single segmented square land has as large area as possible. the width of the segmented square land is also binary.
Inputthe first line of the input is t (1 ≤ T ≤ 100), which stands for the number of test cases you need to solve.
Each case contains two binary number represents the length L and the width W of given land. (0 <L, W ≤21000)
Outputfor each test case, print a line "Case # T:" (without quotes, t means the index of the test case) at the beginning. then one number means the largest width of land that can be divided from input data. and it will be show in binary. do not have any useless number or space.
Sample Input
310 100100 11010010 1100
Sample output
Case #1: 10Case #2: 10Case #3: 110
Source2014 ACM/ICPC Asia Regional Shanghai Online
HDU 5050 divided land (hexadecimal conversion)