Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5050
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 were both in binary form. As the mayor, you must segment the land into multiple squares of the equal size for the villagers. What was required is there must was no any waste and each of the segmented square land have as large area as possible. The width of the segmented square land is also binary.
Inputthe first line of the input was T (1≤T≤100), which stands for the number of test cases you need to solve.
Each case contains-binary number represents the length L and the width W of given land. (0 < L, w≤21000)
Outputfor, 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 the can is divided from input data. And it'll be show in binary. Do not has 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
Ps:
The idea is very simple, is to convert the input binary length and width into decimal to find a gcd and then in the conversion to binary output can, but because the data is too large, need to use Java to achieve, here stick a teammate knocking Java;
The code is as follows:
Import Java.math.*;import Java.util.Scanner; public class main{public static BigInteger gcd (BigInteger a,biginteger b) { if (b.equals (Biginteger.zero) ) return A; Return gcd (B,a.mod (b)); public static void Main (string[] args) { Scanner input = new Scanner (system.in); int t,i,j; String S=null; Char str[]; BigInteger A, B; T=input.nextint (); for (i=1;i<=t;i++) { A=input.nextbiginteger (2); B=input.nextbiginteger (2); A=GCD (A, b); System.out.println ("Case #" +i+ ":" +a.tostring (2));}}}
HDU 5050 Divided Land (greatest common divisor Java)